Discord Bot takes long time on second execute

Viewed 206

I have a weird issue and I'll try to explain it as good as I can, so here we go,

I'm writing a Discord bot using Python. I made a command that can only be executed while a member is in a discord.VoiceChannel. It sends a message into a channel and edits the voice channel:

Event:

@bot.command(name="lft", pass_context=True)
async def lft_command(ctx):
    await lft.lft(ctx, bot)

First method (lft):

async def lft(ctx, bot):
    if ctx.channel == bot.get_channel(806109172336689162):
        dcUser = ctx.author
        if dcUser.voice is not None:
            if dcUser.voice.channel.category == bot.get_channel(809430391177084969).category:
                await methods.set_lft(dcUser, bot)
            else:
                await bot.get_channel(806112383693094942).send(
                    ctx.author.mention + ", you have to be in a temporary channel to use this command.",
                    delete_after=30)
        else:
            await bot.get_channel(806112383693094942).send(
                ctx.author.mention + ", you have to be in a temporary channel to use this command.", delete_after=30)
    else:
        await bot.get_channel(806112383693094942).send(
            ctx.author.mention + ", you can't use this command here, got to " + bot.get_channel(
                806109172336689162).mention, delete_after=30)

Second method(set_lft):

async def set_lft(executor, bot):
    channel = executor.voice.channel
    lft_channel = bot.get_channel(806109172336689162)
    user_role = await get_rank(executor)

    print("b")

    await channel.set_permissions(get(executor.guild.roles, id=806081402407092295), connect=False)
    print("c")
    await channel.edit(name="Looking for mates", user_limit=5)
    msg = await lft_channel.send(
        content=executor.mention + " is looking for teammates for ranked, he is " + user_role.name + ". Join a channel and react to the message to join the channel. There are currently " + str(
            len(executor.voice.channel.members)) + "/5 player in the channel.",
        delete_after=900)
    await msg.add_reaction('✅')
    lft_data[executor.id] = ["placeholder", msg, channel]
    lft_data[msg.id] = [executor, "placeholder", channel]
    lft_data[channel.id] = [executor, msg, "placeholder"]

If the member who executed the command leaves the voice channel, the voice channel gets changed back to a normal voice channel again:

async def set_casual(channel):
    msg = get_msg(channel)
    executor = get_executor(channel)

    await msg.delete()
    if len(channel.members) != 0:
        await channel.set_permissions(get(executor.guild.roles, id=806081402407092295), connect=True)
        await channel.edit(name=channel.members[0].nick + "'s channel", limit=None)
    [lft_data.pop(x, None) for x in [msg.id, channel.id, executor.id]]

and another user can execute the command, but if the command gets executed, it doesn't get called or takes like 5 minutes to execute. Is there a loop that runs too long, or doesn't stop at all?

Thank you in advance

PS: Github if you need any more code:

1 Answers

You are getting rate limited. The issue, the only way to produce an error is buy adding a global cooldown to your command, or creating a new channel whenever someone uses the command.

@discord.ext.commands.cooldown(rate=2, per=600) #bucketType defaults to global
@bot.command()
async def lft_command(ctx):
   # other stuff here

@lft_command.error()
async def lft_error(ctx, error): #handling cooldown errors
 if isinstance(error, commands.CommandOnCooldown):
     await ctx.send("The command is on cooldown") 

References:

Related