import discord
from discord.ext import commands
import asyncio
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
MAIN_CATEGORY_ID = 123456789
WAITING_CATEGORY_ID = 987654321
SUBJECT_CATEGORIES = {'Science': 111111, 'Math': 222222, 'History': 333333, 'Literature': 444444}
@bot.event
async def on_message(msg):
if msg.author.bot:
return
if msg.channel.category_id == MAIN_CATEGORY_ID:
try:
await msg.channel.edit(name=f'query-{msg.channel.name.split("-")[1]}-{msg.author.name}')
await asyncio.sleep(2)
await msg.channel.edit(category=bot.get_channel(WAITING_CATEGORY_ID))
await msg.channel.send('Choose a subject: 1-Science, 2-Math, 3-History, 4-Literature')
except discord.errors.HTTPException as e:
print(f'Rate limit hit: {e}')
await asyncio.sleep(5)
elif msg.channel.category_id == WAITING_CATEGORY_ID:
subjects = {'1': 'Science', '2': 'Math', '3': 'History', '4': 'Literature'}
if msg.content in subjects:
try:
await msg.channel.edit(category=bot.get_channel(SUBJECT_CATEGORIES[subjects[msg.content]]))
except discord.errors.HTTPException as e:
print(f'Rate limit hit: {e}')
await asyncio.sleep(5)
await bot.process_commands(msg)
@bot.command()
async def end(ctx):
if ctx.channel.name.startswith('query-') and ctx.author.name in ctx.channel.name:
try:
await ctx.channel.edit(name=f'query-{ctx.channel.name.split("-")[1]}')
await ctx.channel.edit(category=bot.get_channel(MAIN_CATEGORY_ID))
except discord.errors.HTTPException as e:
print(f'Rate limit hit: {e}')
await asyncio.sleep(5)
bot.run('YOUR_TOKEN_HERE')
I’m having trouble with my Discord bot hitting API rate limits when moving channels. I’ve tried adding delays between actions, but it’s still happening. Any ideas on how to fix this? Should I use a queue system or implement exponential backoff? Maybe there’s a way to batch these API calls?