繁体   English   中英

为什么我的 discord.py bot 看不到消息?

[英]Why can't my discord.py bot see messages?

我正在制作一个 discord.py 机器人,它根本没有“看到”消息,它应该将所有内容打印到终端并带有前缀,但它甚至不打印任何消息。 代码:

import datetime
import os
import discord
from timeit import default_timer as timer
from dotenv import load_dotenv

from discord.ext import commands

load_dotenv()

bot = commands.Bot(command_prefix="!")
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)

TOKEN = ('boop')


client = discord.Client()


@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

async def on_message(message):
    print(f'USER - {message.author} texted - {message.content}')



@bot.command()
async def start(ctx):
    await ctx.channel.send("you have started")
    await ctx.guild.create_role(name= {message.author} + "w")



client.run(TOKEN)

(我计划稍后使用额外的未使用模块)

你的代码有几个问题:

  • 您应该只有一个commands.Bot实例,事件不是discord.Client的。
  • 为了让您的机器人捕捉命令,您需要在bot.process_commands事件结束时使用on_message
  • 您在 on_message 上方缺少@bot.event on_message
  • 您忘记了ctx.guild.create_role(...)中的 fstring。
import discord
from discord.ext import commands

import os
import datetime
from dotenv import load_dotenv
from timeit import default_timer as timer

load_dotenv()

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix="!", intents=intents)


@bot.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')


@bot.event
async def on_message(message):
    print(f'USER - {message.author} texted - {message.content}')
    await bot.process_commands(message)


@bot.command()
async def start(ctx):
    await ctx.channel.send("You have started")
    await ctx.guild.create_role(name=f"{message.author}w")


bot.run("TOKEN")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM