自制的 Discord 机器人。为什么我的不和谐“斜线命令”有效,但也给我一个“应用程序没有响应”错误?

问题描述 投票:0回答:1

我用 Python 编写了机器人,并且正在使用这些库:

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

此代码创建 3 个 /commands 1. /privates (统计具有 privates 角色的人数) 2. /sergeants (统计具有 Sergeants 角色的人数)和 3. /officers (统计具有 sergeants 角色的人数)军官角色)

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

当我使用此功能时,它可以工作,但也给我一个错误。 运行命令时发生的情况的图像

为什么会出现这种情况?

任何帮助将不胜感激

我一直在互联网上搜索是否可以找到类似的案例,但他们大多最终都说这是一个权限问题,该命令不起作用,但我的可以。

我已授予它所有权限,所以我认为这不会是问题所在。

我对Python的了解非常有限,因为这是我的第一个Python项目,所以我在那里做不了太多事情。

python discord
1个回答
1
投票

我真的不知道discord.py是如何工作的,但我知道Discord API是如何工作的。使用

await channel.send()
,您只需向给定通道发送消息,但无需执行斜杠命令。这就是不和谐所说的,你永远不会响应斜杠命令,你只是向执行斜杠命令的通道发送消息。

在文档中,您可以看到必须使用

await interaction.response.send_message()
。您可以在这里找到这个。

所以你必须这样写:

@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;
© www.soinside.com 2019 - 2024. All rights reserved.