我在编写discord机器人时,在接收其他频道的消息时,在控制台上返回的是一个空串,调了好多次还是找不出原因,有没有哪位知道是什么原因?(Intents.message_content
也开了)
@client.event
async def on_message(message: discord.Message):
# 确保机器人不会回复自己的消息,避免造成无限循环
if message.author == client.user:
return
if isinstance(message.channel, discord.DMChannel):# 这是来自私信(DM)的消息
# 将收到的消息输出在控制台
print(f"这是来自私信的消息,用户名: {message.author.name}, 内容: {message.content}")
else:
channel_name = message.channel.name
guild_name = message.guild.name
print(f"该消息来自 {guild_name} 的 {message.author.name}, 内容: {str(message.content)}")
try:
await message.channel.send("这是一条测试消息!")
except discord.Forbidden as e:
print(f"无法发送消息:{e}")
await client.process_commands(message)