Discord.py -discord.ext.commands.bot 特权消息内容意图丢失,命令可能无法按预期工作

问题描述 投票:0回答:1

我正在使用discord.py 创建一个机器人。所有特权网关意图在开发人员门户中均设置为 true。我在 Stack Overflow 上看到过其他关于此问题的问题,但他们都说将意图代码更改为

intents = discord.Intents.all()
或添加
intents.message_content = True
或确保在门户中选择所有意图。然而,所有这些我都做过或尝试过。

这是我当前的代码:

import discord
from discord.ext import commands

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

intents = discord.Intents.default()
intents.message_content = True
intents.messages = True

token = "---"

@bot.event
async def on_ready():
  print(f"Ready as {bot.user}")

@bot.event
async def on_message(message):
    if message.author == bot.user:
      return

    await bot.process_commands(message)

@bot.command()
async def ping(ctx):
    await ctx.channel.send("Pong!")
bot.run(token)

机器人应识别 ;ping 命令并响应“Pong!”然而,什么也没有发生。不会引发错误,但是在设置

on_ready()
事件并打印“Ready as...”之前,控制台显示:

WARNING: discord.ext.commands.bot Privileged message content intent is missing, commands may not work as expected.

python discord discord.py
1个回答
0
投票

好吧,首先,您在定义

intents.message_content = True
之后放置了
intents.messages = True
bot
您需要确保在定义机器人之前这两行。另外,请确保您的机器人的开发者门户中的实际开关已打开。[此处][1]

这是修改后的代码:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
intents.messages = True

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


token = "---"

@bot.event
async def on_ready():
  print(f"Ready as {bot.user}")

@bot.event
async def on_message(message):
    if message.author == bot.user:
      return

    await bot.process_commands(message)

@bot.command()
async def ping(ctx):
    await ctx.channel.send("Pong!")
bot.run(token)


  [1]: https://i.stack.imgur.com/bOB0Z.png
© www.soinside.com 2019 - 2024. All rights reserved.