I created a simple dice rolling bot for my Discord server but I’m having issues with it responding to commands. The bot works perfectly when I send direct messages to it, but when I try to use the same commands in server channels, nothing happens at all.
import discord
import random
BOT_TOKEN = 'YOUR_BOT_TOKEN'
COMMAND_PREFIX = '!'
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot_client = discord.Client(intents=intents)
@bot_client.event
async def on_ready():
print(f'Bot {bot_client.user} is now active!')
@bot_client.event
async def on_message(msg):
if msg.author == bot_client.user:
return
if msg.content.startswith(COMMAND_PREFIX + 'roll'):
await handle_dice_roll(msg)
if msg.content.startswith(COMMAND_PREFIX + 'coin'):
await handle_coin_flip(msg)
async def handle_dice_roll(msg):
dice_options = [1, 2, 3, 4, 5, 6]
bot_roll = random.choice(dice_options)
user_input = msg.content[len(COMMAND_PREFIX + 'roll'):].strip()
try:
user_roll = int(user_input)
if user_roll not in dice_options:
await msg.channel.send("Invalid number. Use 1-6 only.")
return
except ValueError:
await msg.channel.send("Please enter a valid number.")
return
if bot_roll == user_roll:
outcome = 'It\'s a tie!'
elif bot_roll > user_roll:
outcome = f'Bot rolled {bot_roll}. You lose!'
else:
outcome = f'Bot rolled {bot_roll}. You win!'
await msg.channel.send(outcome)
async def handle_coin_flip(msg):
coin_sides = ['Heads', 'Tails']
bot_flip = random.choice(coin_sides)
user_guess = msg.content[len(COMMAND_PREFIX + 'coin'):].strip().capitalize()
if user_guess not in coin_sides:
await msg.channel.send("Invalid choice. Use 'Heads' or 'Tails'.")
return
if bot_flip == user_guess:
result = f'Coin landed on {bot_flip}. You guessed right!'
else:
result = f'Coin landed on {bot_flip}. Better luck next time!'
await msg.channel.send(result)
bot_client.run(BOT_TOKEN)
The weird thing is that the bot shows as online and has all the right permissions in the server. It just seems to completely ignore channel messages while responding fine to private messages. Has anyone encountered this before?