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

时间:2021-01-23 10:24:19

标签: python discord.py discord.py-rewrite

当我尝试启动我的机器人时,它不会运行 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 事件,看看这是否是问题所在,但它没有解决。有没有人解决这个问题?

2 个答案:

答案 0 :(得分:3)

您实际上没有为 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)

答案 1 :(得分:0)

您是否使用以下工具启动您的机器人:

client.run(insert your token in quotes here)

在文件末尾?