pythondiscord

Home made Discord Bot. Why does my discord "Slash command" work but also give me a "The application did not respond" error?


I wrote the bot in Python and I am using these libraries:

import discord
import pytz
import re
from discord.ext import commands
from datetime import datetime, timezone
from pytz import timezone as tz

This code creates 3 /commands 1. /privates (counts the amount of people with the privates role) 2. /sergeants (counts the amount of people with the sergeants role) and 3. /officers (counts the amount of people with the officer role)

client = aclient()
tree = discord.app_commands.CommandTree(client)

@tree.command(description='Count members with "=CALUM= Private" role.')
@commands.check(lambda ctx: max(role.position for role in ctx.author.roles) >= max(
        discord.utils.get(ctx.guild.roles, name='=CALUM= Officers').position,
        discord.utils.get(ctx.guild.roles, name='CÆLUM_on_member_join').position))
async def privates(ctx):
    guild = ctx.guild
    role = discord.utils.get(guild.roles, name='=CALUM= Private')
    channel = discord.utils.get(guild.channels, name='role-counter')
    member_count = len(role.members)
    await channel.send(f"The number of members with the '=CALUM= Private' role is {member_count}.")
    return

@tree.command(description='Count members with "=CALUM= Sergeatns" role.')
@commands.check(lambda ctx: max(role.position for role in ctx.author.roles) >= max(
        discord.utils.get(ctx.guild.roles, name='=CALUM= Officers').position,
        discord.utils.get(ctx.guild.roles, name='CÆLUM_on_member_join').position))
async def sergeants(ctx):
    guild = ctx.guild
    role = discord.utils.get(guild.roles, name='=CALUM= Sergeants')
    channel = discord.utils.get(guild.channels, name='role-counter')
    member_count = len(role.members)
    await channel.send(f"The number of members with the '=CALUM= Sergeants' role is {member_count}.")
    return

@tree.command(description='Count members with "=CALUM= Officers" role.')
@commands.check(lambda ctx: max(role.position for role in ctx.author.roles) >= max(
        discord.utils.get(ctx.guild.roles, name='=CALUM= Officers').position,
        discord.utils.get(ctx.guild.roles, name='CÆLUM_on_member_join').position))
async def officers(ctx):
    guild = ctx.guild
    role = discord.utils.get(guild.roles, name='=CALUM= Officers')
    channel = discord.utils.get(guild.channels, name='role-counter')
    member_count = len(role.members)
    await channel.send(f"The number of members with the '=CALUM= Officers' role is {member_count}.")
    return

When I use this function it works but it also gives me a error. Image of what happens when I run the command

Why does this happen?

Any help would be much appreciated

I have been searching around on the internet to see if I could find similar cases, but they mostly end up with saying that its a permission issue that the command doesn't work, but mine does work.

I have given it all permissions I can so I don't think that could be the issue.

My knowledge in python is very limited as this is my first python project so I have not been able to do much there.


Solution

  • I don't really know how discord.py works, but I know how the Discord API works. With await channel.send() you just send a message to the given channel, but you don't relpy to the slash command. That's what discord says you never respond to the slash command, you just send a message to the channel where the slash command is executed.

    In the docs you can read that you must use await interaction.response.send_message(). You can find this here.

    So you have to write it like this:

    @tree.command(description='Count members with "=CALUM= Officers" role.')
    @commands.check(lambda ctx: max(role.position for role in ctx.author.roles) >= max(
            discord.utils.get(ctx.guild.roles, name='=CALUM= Officers').position,
            discord.utils.get(ctx.guild.roles, name='CÆLUM_on_member_join').position))
    async def officers(interaction: discord.Interaction, ctx):
        guild = ctx.guild
        role = discord.utils.get(guild.roles, name='=CALUM= Officers')
        channel = discord.utils.get(guild.channels, name='role-counter')
        member_count = len(role.members)
        await interaction.response.send_message(f"The number of members with the '=CALUM= Officers' role is {member_count}.")
        return;