Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

discord.py - How to send a message on the bot startup to every server it is in?

So i want to send a 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 also found a similar question, but it is in discord.js. It says something with default channel, but when i tried:

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

It gave me the error: Destination must be Channel, PrivateChannel, User, or Object

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Firstly 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.

Secondly, 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.

@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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...