当我尝试启动我的机器人时,它不会运行 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 事件,看看这是否是问题所在,但它没有解决。有没有人解决这个问题?
答案 0 :(得分:3)
您实际上没有为 on_ready
和 on_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)
在文件末尾?