I created a Discord bot that plays rock-paper-scissors with users. The bot works perfectly when I send commands through direct messages, but it completely ignores commands when posted in server text channels.
import discord
import random
BOT_TOKEN = 'YOUR_BOT_TOKEN_HERE'
COMMAND_PREFIX = '!'
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = discord.Client(intents=intents)
@bot.event
async def on_ready():
print(f'Bot {bot.user} is now online!')
@bot.event
async def on_message(msg):
if msg.author == bot.user:
return
if msg.content.startswith(COMMAND_PREFIX + 'rps'):
await play_rock_paper_scissors(msg)
if msg.content.startswith(COMMAND_PREFIX + 'rpsb'):
await play_rps_with_nuke(msg)
async def play_rock_paper_scissors(msg):
options = ['Rock', 'Paper', 'Scissors']
bot_choice = random.choice(options)
user_choice = msg.content[len(COMMAND_PREFIX + 'rps'):].strip().title()
if user_choice not in options:
await msg.channel.send("Invalid choice. Please use 'Rock', 'Paper' or 'Scissors'.")
return
if bot_choice == user_choice:
result = 'It\'s a tie!'
elif (bot_choice == 'Rock' and user_choice == 'Scissors') or (bot_choice == 'Scissors' and user_choice == 'Paper') or (bot_choice == 'Paper' and user_choice == 'Rock'):
result = f'I picked {bot_choice}. You lost!'
else:
result = f'I picked {bot_choice}. You won!'
await msg.channel.send(result)
async def play_rps_with_nuke(msg):
options = ['Rock', 'Paper', 'Scissors', 'Nuke']
bot_choice = random.choice(options)
user_choice = msg.content[len(COMMAND_PREFIX + 'rpsb'):].strip().title()
if user_choice not in options:
await msg.channel.send("Invalid choice. Use 'Rock', 'Paper', 'Scissors' or 'Nuke'.")
return
if bot_choice == user_choice:
result = 'It\'s a tie!'
elif (bot_choice == 'Rock' and user_choice == 'Scissors') or (bot_choice == 'Scissors' and user_choice == 'Paper') or (bot_choice == 'Paper' and user_choice == 'Rock'):
result = f'I picked {bot_choice}. You lost!'
elif bot_choice == 'Nuke' and user_choice != 'Paper':
result = f'I picked {bot_choice}. You won!'
else:
result = f'I picked {bot_choice}. You lost!'
await msg.channel.send(result)
bot.run(BOT_TOKEN)
The weird thing is that the bot responds instantly when I message it directly, but when I type the same commands in server channels, nothing happens. The bot shows as online and I gave it administrator permissions just to be sure. What could be causing this issue?