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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With