python 皮科尔德|discord.errors.Forbidden:403禁止访问(错误代码:50001):缺少访问权限

iezvtpos  于 2023-02-02  发布在  Python
关注(0)|答案(1)|浏览(203)

我试图使用轮齿向代码中添加持久视图,但它给了我一个403禁止。
扩展代码:

import discord
from discord.ext import commands

"""
Let users assign themselves roles by clicking on Buttons.
The view made is persistent, so it will work even when the bot restarts.
See this example for more information about persistent views:
https://github.com/Pycord-Development/pycord/blob/master/examples/views/persistent.py
Make sure to load this cog when your bot starts!
"""

# This is the list of role IDs that will be added as buttons.
role_ids = [1066139205060804679,1066139281518755941,1066139307951271996,1066139330063634503]

class RoleButton(discord.ui.Button):
    def __init__(self, role: discord.Role):
        """A button for one role. `custom_id` is needed for persistent views."""
        super().__init__(
            label=role.name,
            style=discord.ButtonStyle.primary,
            custom_id=str(role.id),
        )

    async def callback(self, interaction: discord.Interaction):
        """
        This function will be called any time a user clicks on this button.
        Parameters
        ----------
        interaction: :class:`discord.Interaction`
            The interaction object that was created when a user clicks on a button.
        """
        # Get the user who clicked the button.
        user = interaction.user
        # Get the role this button is for (stored in the custom ID).
        role = interaction.guild.get_role(int(self.custom_id))

        if role is None:
            # If the specified role does not exist, return nothing.
            # Error handling could be done here.
            return

        # Add the role and send a response to the user ephemerally (hidden to other users).
        if role not in user.roles:
            # Give the user the role if they don't already have it.
            await user.add_roles(role)
            await interaction.response.send_message(
                f"🎉 You have been given the role {role.mention}!",
                ephemeral=True,
            )
        else:
            # Otherwise, take the role away from the user.
            await user.remove_roles(role)
            await interaction.response.send_message(
                f"❌ The {role.mention} role has been taken from you!",
                ephemeral=True,
            )

class ButtonRoleCog(commands.Cog):
    """
    A cog with a slash command for posting the message with buttons
    and to initialize the view again when the bot is restarted.
    """

    def __init__(self, bot):
        self.bot = bot

    # Pass a list of guild IDs to restrict usage to the supplied guild IDs.
    @commands.slash_command(guild_ids=role_ids, description="Post the button role message")
    async def post(self, ctx: discord.ApplicationContext):
        """Slash command to post a new view with a button for each role."""

        # timeout is None because we want this view to be persistent.
        view = discord.ui.View(timeout=None)

        # Loop through the list of roles and add a new button to the view for each role.
        for role_id in role_ids:
            # Get the role from the guild by ID.
            role = ctx.guild.get_role(role_id)
            view.add_item(RoleButton(role))

        await ctx.respond("Click a button to assign yourself a role", view=view)

    @commands.Cog.listener()
    async def on_ready(self):
        """
        This method is called every time the bot restarts.
        If a view was already created before (with the same custom IDs for buttons),
        it will be loaded and the bot will start watching for button clicks again.
        """
        # We recreate the view as we did in the /post command.
        view = discord.ui.View(timeout=None)
        # Make sure to set the guild ID here to whatever server you want the buttons in!
        guild = self.bot.get_guild(1063291660496293928)
        for role_id in role_ids:
            role = guild.get_role(role_id)
            view.add_item(RoleButton(role))

        # Add the view to the bot so that it will watch for button interactions.
        self.bot.add_view(view)

def setup(bot):
    bot.add_cog(ButtonRoleCog(bot))

下面是我的意图和我如何调用此扩展

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = discord.Bot(intents=intents)
bot.load_extension('cogs.roles')
bot.run(TOKEN)

我试过给我的机器人应用程序命令权限,并让它重新加入,但无济于事。我试过使用其他齿轮,他们也不工作

cs7cruho

cs7cruho1#

我想明白了我把角色ID放在公会ID里了

相关问题