我的机器人不会正确 运行 on_message/on_ready 事件

My bot wont run the on_message/on_ready event properly

当我尝试启动我的机器人时,它不会 运行 on_message 事件,也不会 运行 on_ready 事件。

这是我的代码:

import os
from discord.ext import commands
from datetime import datetime

client = commands.Bot(command_prefix = ";", help_command = None)

dates = datetime.now()
date = datetime.today()
client.event
async def on_ready():
    print("bot is ready")


client.event
async def on_message(message):
    author = message.author
    text = message.content
    print(author + " said at " + dates + " : " + text)
    client.process_commands(message)

我试图摆脱 on_message 事件以查看是否是问题所在,但并没有解决。有人对此有解决办法吗?

您是否使用以下方式启动您的机器人:

client.run(insert your token in quotes here)

在文件末尾?

您实际上没有为 on_readyon_message 使用正确的 装饰器 。任何装饰器都应该以 @ 开头,注意你是如何只使用 client.event 而不是 @client.event.

还要确保为您的机器人激活一些意图,否则您将无法访问某些特定信息。

intents = discord.Intents.default()
client = commands.Bot(command_prefix = ";", help_command = None, intents = intents)

您更正后的代码应该是这样的:

import os
from discord.ext import commands
from datetime import datetime

intents = discord.Intents.default()
client = commands.Bot(command_prefix = ";", help_command = None, intents = intents)

dates = datetime.now()
date = datetime.today()
@client.event
async def on_ready():
    print("bot is ready")


@client.event
async def on_message(message):
    author = message.author
    text = message.content
    print(author + " said at " + dates + " : " + text)
    await client.process_commands(message)