Raphiel
Raphiel

Reputation: 388

Discord Python Rewrite - Broadcast

Is it possible to make the bot sends 1 message on every server that the bot is in without sending the message to every channel? I have a working code but it sends to all channel

@client.command()
async def broadcast(ctx, *, message):
    for guild in client.guilds:
        for channel in guild.channels:
            
            if guild.name == ctx.author.guild.name:
                pass

            else:
                try:
                    await channel.send(message)
                    await ctx.send(
                        'Sent to {} (ID: {}, Owner: {}#{} With {} Members)'.format(
                            guild.name,
                            guild.id,
                            guild.owner.name,
                            guild.owner.discriminator,
                            guild.member_count
                            ))

                except:
                await ctx.send(
                    'Could not send at {} (Channel ID: {}). (Owner: {}#{})'.format(
                        guild.name,
                        channel.id,
                        guild.owner.name,
                        guild.owner.discriminator,
                        ))
                    return

                else:
                    break
please help

Upvotes: 0

Views: 146

Answers (1)

Allister
Allister

Reputation: 911

If you use

for guild in client.guilds:
    channel = discord.utils.get(guild.text_channels, name="general")
    if channel != None:
       #do something

you could get the #general channel from each guild. That would be just one channel (Discord allows multiple channels to share names but most people won't do this, and .get() will return the first only, anyway).
This is much better than looping over all channels and slightly better than picking random channels but I still would consider letting guilds set their own broadcast reception channels.

Tested and it works, albeit my bot exists only in one server, I don't know if there are unknown consequences for trying to do this over 100+ servers.

Upvotes: 1

Related Questions