使用带有斜杠命令的discord.py purge时:我只能删除1条消息,否则会出现错误

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

我可以删除一条消息,否则我会收到此错误消息:“应用程序没有响应”

或者控制台中的此错误消息:“discord.app_commands.errors.CommandInvokeError:命令'clear'引发异常:NotFound:404 Not Found(错误代码:10062):未知交互”

这是我的代码(导致问题的清除命令位于最后显示的文件中):

src/init.py:

import os

import discord
from discord.ext import commands as discord_commands
from dotenv import load_dotenv

from src import commands


def run_discord_bot():
    def get_token():
        load_dotenv()
        return os.environ.get("TOKEN")

    token = get_token()
    intents = discord.Intents.all()
    intents.message_content = True
    client = discord_commands.Bot(command_prefix="?", intents=intents)

    commands.await_commands(client)

    client.run(token)

src/commands/init.py:

import discord
from discord import app_commands

from src.commands import responses


def await_commands(client):

    # syncing slash commands
    @client.event
    async def on_ready():
        try:
            synced = await client.tree.sync()
            print(f"Synced {len(synced)} commands(s)")
        except Exception as e:
            print(e)

    @client.tree.command(name='clear', description="Delete messages")
        @app_commands.describe(amount="Number of messages to delete")
        async def clear(interaction: discord.Interaction, amount: int):
            await responses.clear(interaction, 'clear', amount)

src/commands/responses.py:

async def clear(interaction, command, amount: int):

    if amount > 100:
        await message(interaction, command, f"The amount of messages to delete is too much", True)
    elif amount > 0:
        await interaction.channel.purge(limit=amount)
        await message(interaction, command, f"{amount} messages have been deleted", True)
    elif amount < 0:
        await message(interaction, command, f"Can't delete a negative amount of messages", True)
    else:
        await message(interaction, command, f"0 Messages have been deleted", True)

我已经尝试了很多方法,但似乎没有任何效果。 我只想使用斜杠命令删除频道中任意数量的消息。

python discord.py command slash purge
1个回答
0
投票

“应用程序没有响应”只是意味着您的机器人在 3 秒后(当您清除消息时)没有响应该命令,并且您必须在 3 秒内回答该命令。因此,如果

amount
较大,则清除时间较长(超过3秒),会导致出现此消息。
解决方案非常简单,首先
defer
交互,然后在清除所有消息后发送
followup

一些想法:

async def clear(interaction, command, amount: int):
  # if amount is between 0 and 100, defer first and purge
  if 0 < amount <= 100:
    await interaction.defer()
    # purge the messages
    await interaction.followup.send(content="purge finished.")
© www.soinside.com 2019 - 2024. All rights reserved.