【问题标题】:Leave selected channels out of discord.py loop将选定的频道排除在 discord.py 循环之外
【发布时间】:2021-02-13 22:19:40
【问题描述】:

我创建了一个机器人,它会在我输入命令时锁定每个频道,但是,我不希望它锁定员工频道和公告频道。 到目前为止,这是我的代码:

@bot.command(aliases=['r'])
@commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
    for guild in bot.guilds:
        for channel in guild.text_channels:
            channel = channel
            overwrite = channel.overwrites_for(ctx.guild.default_role)
            overwrite.send_messages = False
            await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
            await channel.send('Lockdown has started')

我该怎么做?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您可以使用阻塞语句排除您不想按名称锁定的频道。像这样:

    @bot.command(aliases=['r'])
    @commands.has_permissions(manage_channels=True)
    async def lockdown(ctx):
        for guild in bot.guilds:
            for channel in guild.text_channels:
                if channel.name in ['dont_lock_ch_name_1', 'dont_lock_ch_name_2']:
                    continue
                overwrite = channel.overwrites_for(ctx.guild.default_role)
                overwrite.send_messages = False
                await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
                await channel.send('Lockdown has started')
    

    【讨论】:

      猜你喜欢
      • 2015-06-18
      • 2021-04-04
      • 2021-10-04
      • 2021-01-25
      • 2021-11-08
      • 1970-01-01
      • 2020-12-07
      • 2021-12-21
      • 1970-01-01
      相关资源
      最近更新 更多