Xavee
Xavee

Reputation: 3

How can I send a message to every server the bot is in (discord.py)

So I want my bot to send a message in every server my bot is in but it will send a message in only a channel name like

Example:

//broadcast hello

And the bot searches for the channel name general

And the bot sends a message to that channel and it will continue sending "hello" to other servers my bot is in.

So can anyone give me an example? Because of I do not know how. So I ask here for help

Upvotes: 0

Views: 1649

Answers (1)

EpicRaisin
EpicRaisin

Reputation: 336

The best way to do this is to loop through every server the bot is in, then loop through each channel in each server, testing if that channel's name is #general, if so, send a message to that channel. You can use bot.guilds to get a list of all servers a bot is in, then use guild.channels to get all channels in a server, then you can use channel.name to check the name of the channel. Here's the full command:

@bot.command()
@commands.has_permissions(administrator=True)
async def broadcast(ctx, message):
    for guild in bot.guilds:
        for channel in guild.channels:
            if(channel.name == 'general'):
                await channel.send(message)

bot.run(token_here)

Upvotes: 1

Related Questions