EckigerLuca
EckigerLuca

Reputation: 163

Send a message to every server's system channel

I'm trying the bot to send a message to every server's system channel, but it always ends up sending it to the top channel on a server. Here's my code so far:

    @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

Edit: Thank you to TheUntraceable! Here is a working version of it:

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

Upvotes: 0

Views: 77

Answers (2)

TheUntraceable
TheUntraceable

Reputation: 81

You can use the System Channel attribute that a discord.Guild has

Upvotes: 1

FoxGames01
FoxGames01

Reputation: 67

The for loop iterate the first channel, send the message and break at the end stops the loop, thats because the message only sends in the first channel

    @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

Upvotes: 0

Related Questions