为什么我的 discord.py 机器人看不到消息?



我正在制作一个不和谐的.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独有的
  • 为了让机器人程序捕获命令,您需要在on_message事件结束时使用bot.process_commands
  • 您在on_message之上缺少@bot.event
  • 您忘记了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")

最新更新