【问题标题】:Send a message to every server's system channel向每个服务器的系统通道发送消息
【发布时间】:2021-08-20 21:21:02
【问题描述】:

我正在尝试让机器人向每个服务器的系统通道发送消息,但它总是最终将其发送到服务器上的顶部通道。到目前为止,这是我的代码:

    @commands.command()
    async def broadcast(self, ctx, *, text):
        for server in self.client.guilds:
            for system_channel in server.text_channels:
                try:
                    await system_channel.send(text)
                except Exception:
                    continue
                else: break

编辑: 谢谢TheUntraceable!这是它的工作版本:

    @commands.command()
    async def broadcast(self, ctx, *, text):
        for guild in self.client.guilds:
            try:
                await guild.system_channel.send(text)
            except Exception:
                continue

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您可以使用discord.Guild 具有的System Channel 属性

    【讨论】:

    • 我不知道如何实现它,因为将它放在 server 之后或 text_channels 之后都不起作用(第 4 行)。
    • 您将它放在任何 Guild 实例(在您的案例服务器中)之后,因为它是 discord.Guild 的一个属性。如果您有任何错误,请随时告诉我。
    • 在第 4 行中使用 for system_channel in server.system_channel: 并运行命令时,没有任何反应。
    • 你可以做类似for guild in client.guilds: await guild.system_channel.send(text) P.S 抱歉所有这些编辑我对 StackOverflow 真的很陌生
    【解决方案2】:

    for循环遍历第一个通道,发送消息,最后break停止循环,那是因为消息只在第一个通道发送

        @commands.command()
        async def broadcast(self, ctx, *, text):
            for server in self.client.guilds:
                for system_channel in server.text_channels:
                    try:
                        await system_channel.send(text)
                    except:
                        pass
    
    

    【讨论】:

    • 它应该只在服务器上发送一条消息,但是如何让它在系统通道而不是在服务器上的第一个通道中发送呢?这是我的主要问题。
    猜你喜欢
    • 2020-12-06
    • 2021-11-03
    • 1970-01-01
    • 2020-05-25
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多