I’m having issues with my Discord bot not responding to any commands I send. I’m pretty new to making bots and I can’t figure out what’s going wrong. The bot shows as online but when I type commands nothing happens. I’ve been looking at tutorials but still stuck. Any ideas what might be broken? Here’s my code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='?')
@bot.event
async def on_ready():
print('Bot is ready')
@bot.command()
async def assign_class(ctx, class_name):
available_classes = ['MATH101', 'PHYS201', 'CHEM150', 'BIOL120', 'HIST101', 'ENG101', 'PSYC101', 'ECON201', 'ART101', 'MUS101']
requested_class = class_name.upper()
target_role = discord.utils.get(ctx.guild.roles, name=requested_class)
if target_role is None or target_role.name not in available_classes:
await ctx.send('That class role does not exist')
return
if target_role in ctx.author.roles:
await ctx.send('You are already enrolled in this class')
else:
try:
await ctx.author.add_roles(target_role)
await ctx.send(f'Successfully enrolled in {target_role.name}')
except discord.Forbidden:
await ctx.send('Missing permissions to assign roles')
@bot.command()
async def clear_classes(ctx):
available_classes = ['MATH101', 'PHYS201', 'CHEM150', 'BIOL120', 'HIST101', 'ENG101', 'PSYC101', 'ECON201', 'ART101', 'MUS101']
removal_success = True
for class_role in available_classes:
role_obj = discord.utils.get(ctx.guild.roles, name=class_role)
if role_obj in ctx.author.roles:
try:
await ctx.author.remove_roles(role_obj)
except discord.Forbidden:
await ctx.send('Cannot remove roles due to permissions')
removal_success = False
break
if removal_success:
await ctx.send('All class enrollments have been cleared')
bot.run('your_token_here')