I’m working with a Discord bot written in Python and I’m experiencing some weird timing issues. The bot has two basic commands and both work fine, but there’s this annoying delay problem.
Whenever I use a command for the first time, it takes around 30 seconds before the bot actually responds in the chat. But if I use the same command again right after, it responds super fast (under 1 second). The weird part is that if I wait about 30 seconds without using any commands, the bot goes back to being slow again on the next command.
I’ve checked a few things but can’t figure out what’s wrong. The logs don’t show any errors or gateway issues. I even removed the bot from the server and added it back, but the same thing happens. Other bots in our server work perfectly fine. When I check the latency with my ping command, it always shows around 0.1 seconds.
Has anyone experienced this kind of behavior before? Is this something related to how Discord’s API works?
import discord
from discord.ext import commands
import datetime
permissions = discord.Intents.default()
permissions.members = True
api_token = my_secret_token
class ChatBot(commands.Bot):
async def on_ready(self):
print('Bot is online!')
print('Name: {0.name}\nUser ID: {0.id}'.format(self.user))
client = ChatBot(command_prefix='!', intents=permissions)
@client.command()
async def latency(ctx):
await ctx.send('Response time: {} ms'.format(round(client.latency * 1000, 1)))
@client.command()
async def countdown(ctx):
target_date = datetime.datetime(2021, 5, 15)
current_time = datetime.datetime.now()
time_diff = target_date - current_time
remaining_days = time_diff.days
remaining_seconds = time_diff.seconds % 60
remaining_minutes = (time_diff.seconds // 60) % 60
remaining_hours = (time_diff.seconds // 3600)
response = 'Days left until the big event: {} days, {} hours, {} minutes, {} seconds'.format(
remaining_days, remaining_hours, remaining_minutes, remaining_seconds)
await ctx.send(response)
client.run(api_token)