Reputation: 125
I would like it so that when I type a command and a message it will forward that message to a channel in every server the bot is connected to with python discord. I know how to get the message and look for the command but I do not know how to find a channel the bot can post in, in every server and then send it.
Could anyone help me?
Upvotes: 1
Views: 6393
Reputation: 71
UPDATE to patrick's code:
@bot.command(pass_context=True)
async def broadcast(ctx, *, msg):
for server in bot.guilds:
for channel in server.text_channels:
try:
await channel.send(msg)
except Exception:
continue
else:
break
Changes: Servers Are Now Called Guilds, we need to use the send method of channel class , also we want to use only text_channels.
Upvotes: 2
Reputation: 20561
The servers
object of your Client
is an iterable of Server
objects the bot is part of, and each Server
contains a channels
iterable you can use to get the channels you can post in.
client = discord.Client()
await client.wait_until_ready()
channels = []
for server in client.servers:
for channel in server.channels:
channels.append(channel)
# If you have your discord.Message object (message) and
# command you want to send (command_str) you could then:
# await client.send_message(message.channel, command_str)
# channels will now be populated with all the possible channels
Upvotes: 0
Reputation: 60944
You can loop through all the servers the bot can see, then loop through their channels until you find one you can send messages to.
@bot.command(pass_context=True)
async def broadcast(ctx, *, msg):
for server in bot.servers:
for channel in server.channels:
try:
await bot.send_message(channel, msg)
except Exception:
continue
else:
break
Upvotes: 2