'The application did not respond' error while making a slash command using discord.py

Viewed 9585

While making a discord bot in Python using discord-ui and discord.py, I wanted to make a command which does specific stuff for specific roles. Like if I have the att_role and aot_role, it would execute the code for both of these roles, and if I add another role like est_role, it would execute its code too. I am using PyCharm as the IDE here.

I am a novice programmer, so I don't know if this is the best way to do this, but when I do it I get the error of 'The application did not respond'. Here is when it works and when not:

When I have the att_role, it works perfectly even if I add more roles from the list. If I remove the att_role and add two other roles from the list, it would work for those too. If I have the rs_role only, it would work for it too. However, if I have only the aot_role or only the est_role, I get this error after running:- The application did not respond in the discord channel. If I add any other role with these, it would work perfectly. It doesn't show any error in my IDE(PyCharm).

If you don't understand correctly what I mean, just tell me the best way for doing this: Making different stuff for different roles.

Here is my code:

import discord
from discord.ext import commands
from discord_ui import UI, SelectOption, SelectMenu
import asyncio

# Clients
client = commands.Bot(' ')
client.remove_command('help')
ui = UI(client)


@ui.slash.command()
async def log(ctx):
    """Use this command to log trainings/tryouts"""

    # These are the roles allowed to use this command
    allowed_roles = [
        'Advanced Trooper Training Section',
        'Advanced Officer Training Section,',
        'Recruiting Staff',
        'Academy Instructor'
        'Executive Sergeant Training Section',
        'TB Bot Manager'
    ]

    author_roles = ctx.author.roles

    # If statement to check if the user can use this command
    if any(role.name in allowed_roles for role in author_roles):

        att_role = discord.utils.get(author_roles, name='Advanced Trooper Training Section')
        aot_role = discord.utils.get(author_roles, name='Advanced Officer Training Section')
        rs_role = discord.utils.get(author_roles, name='Recruiting Staff')
        ai_role = discord.utils.get(author_roles, name='Academy Instructor')
        est_role = discord.utils.get(author_roles, name='Executive Sergeant Training Section')
        manager_role = discord.utils.get(author_roles, name='TB Bot Manager')

        # Different stuff for different roles
        if att_role is not None and att_role.name == 'Advanced Trooper Training Section':
            await ctx.send('att') # Sample Stuff

        if rs_role is not None and rs_role.name == 'Recruiting Staff':
            await ctx.send('rs') # Sample Stuff

        if ai_role is not None and ai_role.name == 'Academy Instructor':
            await ctx.send('ai') # Sample Stuff

        if est_role is not None and est_role.name == 'Executive Sergeant Training Section':
            await ctx.send('est') # Sample Stuff

        if manager_role is not None and manager_role.name == 'TB Bot Manager':
            await ctx.send('manager') # Sample Stuff

        if aot_role is not None and aot_role.name == 'Advanced Officer Training Section':
            await ctx.send('aot') # Sample Stuff


client.run(TOKEN)
3 Answers

If your bot does not respond to an interaction within 3 seconds, the interaction will fail (although not necessarily an error has occured in your application). You can defer if you know it will take longer time.

In Pycord, it is located under InteractionResponse.defer but since you didn't mention which library you're using, I don't know which method you have to call.

You should use

await ctx.respond()

instead of

await ctx.send()

when dealing with slash commands. If this doesn't work, it'll be due to an error in the code (It should appear in terminal)

A previous answer on here suggests using ctx.respond() instead of ctx.send(), this solution will throw an error in the current version of discord.py:

Instead use:

await ctx.reply()
Related