Discord.py 斜杠命令出现 403 禁止错误

Discord.py slash commands are giving 403 Forbidden Error

我正在尝试使用 python 制作一个 discord 机器人,但我在添加斜杠命令时 运行 遇到了问题。

每当我 运行 机器人时,几秒钟后我就会收到错误消息。我能找到的关于此错误的唯一信息是 ,但 none 的回复解决了问题。

Any/all 非常感谢帮助,谢谢!

主要代码:

import discord
from discord.ext import commands

intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", intents = intents)

class myClient(discord.Client):
    @bot.event
    async def on_ready():
        print(f"Bot is now online.")

    @bot.event
    async def on_message(message):
        await bot.process_commands(message)

    @bot.slash_command(
        description="Test.",
        guild_ids=[redacted])
    async def test(self, ctx: discord.ApplicationContext):
        await ctx.respond("Test")

client = myClient()

bot.run(redacted)

堆栈跟踪:

Ignoring exception in on_connect
    Traceback (most recent call last):
      File "C:\Users\ilove\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 382, in _run_event
        await coro(*args, **kwargs)
      File "C:\Users\ilove\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 1025, in on_connect
        await self.sync_commands()
      File "C:\Users\ilove\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 558, in sync_commands
        registered_guild_commands[guild_id] = await self.register_commands(
      File "C:\Users\ilove\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 471, in register_commands
        registered = await register("bulk", data)
      File "C:\Users\ilove\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 351, in request
        raise Forbidden(response, data)
    discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access

我已经为机器人提供了它需要的所有权限:

这是我为解决此问题所做的工作:

在执行以下任何操作之前,请确保您在 slash_command 装饰器中 select 编辑了正确的 guild_id。如果这不起作用,试试这个:

  1. 将机器人从当前所在的服务器中踢出。

  2. 转到:https://discord.com/developers/applications/,转到您喜欢的机器人,然后转到“0Auth2”,最后转到“Url 生成器”。

  3. 在范围 select "bot" AND "applications.commands" 下,然后在 bot 权限下,您可以 select 您的 bot 的所有权限。

  4. 让机器人重新加入正确的服务器并启动您的机器人,现在它应该不会再给您错误消息了。

Reference(我知道是 Discord.js,但这是一个类似的问题)

这修复了我的错误消息,希望对您有所帮助。