Discord.py 创建文本频道时覆盖动态选项

huangapple go评论71阅读模式
英文:

Discord.py create_text_channel overwrites dynamic options

问题

在设置中,机器人会创建一个类别并添加频道,但我想添加覆盖以使这些频道成为只读。

斜杠命令接受一个参数,'role',因此设置机器人的用户可以选择哪个服务器角色可以访问该机器人。

我的问题是...是否有一种方式可以使每个服务器的所有角色作为选项出现,以便设置的人可以直接选择而不是输入?

我知道app_commands.choices实际上不能动态接收外部数据,但也许因为数据来自Discord服务器,它可以以某种方式实现?

    @bot.tree.command(name="setupBot", description="输入需要访问的角色")
    async def setupBot(ctx, role: str,):

        await ctx.send("设置类别和频道!")
英文:

I have a slash command for people to setup a bot in their server. During setup, the bot creates a category and adds channels but I want to add overwrites to make these channels read only.

The slash command takes in one parameter, 'role', so the user that is setting up the bot can choose which server role will have access to use the bot.

My question is... Is there a way to make all the roles of each individual server come up as a choice so the person setting up can just choose instead of typing it in?

I know app_commands.choices can't really dynamically take in outside data but maybe because the data is coming from the Discord server it can somehow?

    @bot.tree.command(name="setupBot", description="Enter role needed for 
                                                               access")
    async def setupBot(ctx, role: str,):

        await ctx.send("Setting up category and channels!")

答案1

得分: 1

You can assign the object discord.Role to the parameter role. This would prompt for a role instead of text. From there, you can use role as discord.Role right away.

@bot.tree.command(name="setupBot", description="Enter role needed for access")
async def setupBot(ctx, role: discord.Role,):

    await ctx.send("Setting up category and channels!")

The application command also uses discord.Interaction and not Context (ctx).

@bot.tree.command(name="setupBot", description="Enter role needed for access")
async def setupBot(interaction: discord.Interaction, role: discord.Role):

    await interaction.response.send_message("Setting up category and channels!")
英文:

You can assign the object discord.Role to the parameter role. This would prompt for a role instead of text. From there, you can use role as discord.Role right away.

@bot.tree.command(name="setupBot", description="Enter role needed for access")
async def setupBot(ctx, role: discord.Role,):

    await ctx.send("Setting up category and channels!")

The application command also uses discord.Interaction and not Context (ctx).

@bot.tree.command(name="setupBot", description="Enter role needed for access")
async def setupBot(interaction: discord.Interaction, role: discord.Role):

    await interaction.response.send_message("Setting up category and channels!")

huangapple
  • 本文由 发表于 2023年4月17日 15:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定