The setup: I've made a bot to interact with users on my server. One of the features I've added is the game of Marco-Polo. When a user sends "Marco", the bot responds with "POLO...".
The problem: When the bot responds with "POLO...", it responds in the same channel as the original message. I, however, want the bot to respond in a random channel within the server, that sends the user looking for it.
I've searched online for the answer as well as in discord.py, but I haven't been able to find it. However, I'm new to Python and coding altogether, so it could very well be that I am simply missing something or that it's not possible, LOL.
Here is the code for the Marco-Polo game:
@client.event
async def on_message(message):
if message.author == client.user:
return
msg = message.content
time_seconds = 1
if msg.lower() == 'marco':
time_seconds = 5
await asyncio.sleep(time_seconds)
await message.channel.send('POLO...')
Here's a shorter version, using random again:
import random
#random_channel = random.choice(message.channel.guild.channels)
random_channel = random.choice(message.guild.channels)
await random_channel.send("POLO...")
This will send the message in a random channel from all the channels in the guild, including channels it can't send in, for example, StoreChannel
, CategoryChannel
, etc. (all the channels in the guild: all the GuildChannel
s, whether Messageable
or not)
This probably will work:
random_channel = random.choice(message.guild.textchannels)
await random_channel.send("POLO...")
...as it gets a random channel from all the TextChannel
s in the guild.
Also, you don't need to install random
(like pip install random
) as it's a built-in Python package.