【问题标题】:Discord py Trying to skip over locked channelsDiscord py试图跳过锁定的频道
【发布时间】:2021-10-26 22:57:34
【问题描述】:

我尝试了这么多频道权限组合。我想不出任何办法来跳过锁定的频道。我正在尝试制作一个广播命令,该命令将在第一个打开的权限通道中发送。

    @commands.command()
    @commands.is_owner()
    async def broadcast(self, ctx, *, msg):
        for guild in self.client.guilds:
            for channel in guild.text_channels:
                if channel.permissions == discord.Permissions(permissions=8):
                    continue
                elif channel.permissions == discord.Permissions(permissions=0):
                    await channel.send(msg)
                else:
                    await print("Did not send")
                break

这是我目前所拥有的。

我的问题是我不想指定频道,因为我将其发送到所有服务器。

这是我所做的两个编辑,但到目前为止还没有工作。

    @commands.command()
    @commands.is_owner()
    async def broadcast(self, ctx, *, msg):
        for guild in self.client.guilds:
            for channel in guild.text_channels:
                if channel.overwrites_for(ctx.guild.default_role).send_messages == False:
                    continue
                elif channel.overwrites_for(ctx.guild.default_role).send_messages == None | True:
                    await channel.send(msg)
    @commands.command()
    @commands.is_owner()
    async def broadcast(self, ctx, *, msg):
        for guild in self.client.guilds:
            for channel in guild.text_channels:
                if channel.permissions_for(ctx.guild.default_role).send_messages == False:
                    continue
                elif channel.permissions_for(ctx.guild.default_role).send_messages == None | True:
                    await channel.send(msg)

【问题讨论】:

  • @Taku 我从这个例子中尝试了一些东西,但它并没有改变任何东西。不过谢谢。
  • 该帖子的解决方案是如何检查 discord.py 中的频道权限,这是您问题的答案。您对代码进行了哪些更改以从该答案中实现?
  • @Taku 我将编辑帖子以显示我尝试的更改。
  • 去掉你的elif,python的|不是or

标签: python-3.x discord.py


【解决方案1】:

使用 try except 循环。它会尝试向频道发送消息,如果不能,它会转到下一个频道。

@commands.command()
@commands.is_owner()
async def broadcast(self, ctx, *, msg):
    for guild in self.client.guilds:
        for channel in guild.text_channels:
            try:
                channel.send(msg)
            except:
                pass
            

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 2021-02-06
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2020-10-23
    相关资源
    最近更新 更多