Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a message on the bot startup to every server it is in?

I want to send an announcement to all the servers my bot is in. I found this on GitHub, but it requires a server ID and a channel ID.

@bot.event
async def on_ready():
    server = bot.get_server("server id")
    await bot.send_message(bot.get_channel("channel id"), "Test")

I found a similar question, but it is in discord.js. It says something with default channel, but when I tried this:

@bot.event
async def on_ready():
    await bot.send_message(discord.Server.default_channel, "Hello everyone")

it gave me this error:

Destination must be Channel, PrivateChannel, User, or Object
like image 687
Spinningjenny Avatar asked Jan 03 '18 00:01

Spinningjenny


1 Answers

First, to answer your question about default_channel: Since about June 2017, Discord no longer defines a "default" channel, and as such, the default_channel element of a server is usually set to None.

Next, by saying discord.Server.default_channel, you're asking for an element of the class definition, not an actual channel. To get an actual channel, you need an instance of a server.

Now, to answer the original question, which is to send a message to every channel, you need to find a channel in the server that you can actually post messages in:

python
    @bot.event
    async def on_ready():
        for server in bot.servers: 
            # Spin through every server
            for channel in server.channels: 
                # Channels on the server
                if channel.permissions_for(server.me).send_messages:
                    await bot.send_message(channel, "...")
                    # So that we don't send to every channel:
                    break
like image 85
Sam Rockett Avatar answered Nov 14 '22 22:11

Sam Rockett