I’m building a Discord bot in Python and running into a performance issue. I created a command that only works when someone is connected to a voice channel. The command posts a message and modifies the voice channel settings.
The first time someone uses the command, it works fine. But when another user tries to run it after the first person leaves the voice channel, the bot either doesn’t respond or takes around 5 minutes to process the request.
Here’s my command handler:
@client.command(name="findteam", pass_context=True)
async def findteam_command(ctx):
await teamfinder.start_search(ctx, client)
Main function:
async def start_search(ctx, client):
target_channel_id = 123456789012345678
if ctx.channel == client.get_channel(target_channel_id):
user = ctx.author
if user.voice is not None:
temp_category = client.get_channel(987654321098765432).category
if user.voice.channel.category == temp_category:
await helpers.activate_search(user, client)
else:
error_channel = client.get_channel(111222333444555666)
await error_channel.send(
user.mention + ", please join a temporary voice channel first.",
delete_after=25)
else:
error_channel = client.get_channel(111222333444555666)
await error_channel.send(
user.mention + ", you must be in a voice channel to use this feature.",
delete_after=25)
Channel modification function:
async def activate_search(user, client):
voice_channel = user.voice.channel
announcement_channel = client.get_channel(123456789012345678)
player_rank = await fetch_user_rank(user)
restricted_role = get(user.guild.roles, id=777888999000111222)
await voice_channel.set_permissions(restricted_role, connect=False)
await voice_channel.edit(name="Team Search Active", user_limit=4)
notification = await announcement_channel.send(
content=user.mention + " wants to form a team! Rank: " + player_rank.name + ". React to join. Current players: " + str(
len(user.voice.channel.members)) + "/4",
delete_after=600)
await notification.add_reaction('👍')
search_data[user.id] = ["temp", notification, voice_channel]
search_data[notification.id] = [user, "temp", voice_channel]
search_data[voice_channel.id] = [user, notification, "temp"]
When the original user leaves, everything resets:
async def reset_channel(channel):
message = find_message(channel)
original_user = find_original_user(channel)
await message.delete()
if len(channel.members) > 0:
restricted_role = get(original_user.guild.roles, id=777888999000111222)
await channel.set_permissions(restricted_role, connect=True)
new_owner = channel.members[0]
await channel.edit(name=new_owner.display_name + "'s Room", user_limit=None)
for key in [message.id, channel.id, original_user.id]:
search_data.pop(key, None)
Could there be an infinite loop or blocking operation causing this delay? Any ideas what might be causing the slow response time?