【问题标题】:Discord.Py bot will receive messages but doesn't respondDiscord.Py 机器人将收到消息但不响应
【发布时间】:2022-12-07 15:30:39
【问题描述】:

Discord.Py 机器人将收到消息但不响应

这是我的代码:

import discord
import os
from discord.ext import commands 

intents = discord.Intents.default()
client = discord.Client(intents=intents)

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    content = message.content
    user = message.author

    if user == client.user:
        return

    print("Received a message:", content)

    if content.strip() == "$hello":
        await message.channel.send("Hi!")

    if content.strip() == "$die":
        await message.channel.send("No you")
        client.close()


      
client.run(os.environ['Token'])

我尝试了多种不同形式的代码,但都没有用。

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    您需要message_content 意图才能访问消息内容;要启用此功能,您需要通过以下方式启用意图:

    developer portal > 选择机器人 > “机器人”部分 > 启用“消息内容”意图

    然后您还需要在代码中启用它以告诉 Discord API 您的机器人需要使用它。

    intents = discord.Intents.default()
    intents.message_content = True # set the "message_content" intent to "True"
    
    client = discord.Client(intents=intents)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      相关资源
      最近更新 更多