机器人事件无法正常工作 discord.py



我正试图为机器人程序制作一个简单的on_message事件,但不知何故,机器人程序已经准备好并在线,但没有任何事件。。。

下面是我的代码示例:

import discord
token = "--My token--"
client = discord.Client()
@client.event
async def on_ready():
print(f"Connected to APPLICATION {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$test"):
await message.channel.send("test")

client.run(token)

U需要在discord.py v.2.0中启用intents才能工作。您可以使用discord.Intents.all()来启用所有意图,如果您希望只启用默认意图,也可以使用discord.Intents.default()

import discord
token = "--My token--"
intents = discord.Intents.all()
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f"Connected to APPLICATION {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$test"):
await message.channel.send("test")

client.run(token)

最新更新