I am coding a Discord bot with Python. The command is working, but after it executes, instead of sending the message to the server, it gives an error

I created a simple Discord bot. The ‘clear’ command works, but instead of saying ‘{len(deleted)} messages have been deleted.’, it shows a warning saying ‘The application did not respond.’Additionally, I’m receiving this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>'discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction'."
</code>
<code>'discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction'." </code>
'discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction'."

I tried using the “interaction.response.defer()” command to delay the process. The “Application did not respond” warning on Discord disappeared, but now it doesn’t show how many messages were deleted; instead, it doesn’t display anything. I also tried sending a message with the “interaction.followup.send” command and editing the original response with “interaction.edit_original_response,” but neither of them worked.

My code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@bot.event
async def on_ready():
await bot.load_extension("admin")
try:
synced= await bot.tree.sync()
print(f"The number of integrated '/' commands is {len(synced)}." )
except ImportError as e:
print(e)
print(f'Bot logged in as {bot.user.name}.')
@bot.tree.command(name="clear", description="It clears the specified number of messages from the chat.")
async def clear(interaction: discord.Interaction, number: int):
max_messages = 100
min_messages = 1
if interaction.user.guild_permissions.administrator:
if min_messages < number <= max_messages:
interaction.response.defer()
deleted = await interaction.channel.purge(limit=number)
await interaction.followup.send(f"{len(deleted)} messages have been deleted.")
else:
await interaction.response.send_message("The number of messages you entered is invalid.", ephemeral=True)
else:
await interaction.response.send_message("You do not have the necessary permissions.", ephemeral=True)
TOKEN = "BOT_TOKEN"
bot.run(TOKEN)
</code>
<code>@bot.event async def on_ready(): await bot.load_extension("admin") try: synced= await bot.tree.sync() print(f"The number of integrated '/' commands is {len(synced)}." ) except ImportError as e: print(e) print(f'Bot logged in as {bot.user.name}.') @bot.tree.command(name="clear", description="It clears the specified number of messages from the chat.") async def clear(interaction: discord.Interaction, number: int): max_messages = 100 min_messages = 1 if interaction.user.guild_permissions.administrator: if min_messages < number <= max_messages: interaction.response.defer() deleted = await interaction.channel.purge(limit=number) await interaction.followup.send(f"{len(deleted)} messages have been deleted.") else: await interaction.response.send_message("The number of messages you entered is invalid.", ephemeral=True) else: await interaction.response.send_message("You do not have the necessary permissions.", ephemeral=True) TOKEN = "BOT_TOKEN" bot.run(TOKEN) </code>
@bot.event
async def on_ready():

    await bot.load_extension("admin")

    try:
        synced= await bot.tree.sync()
        print(f"The number of integrated '/' commands is {len(synced)}." )
    except ImportError as e:
        print(e)
    print(f'Bot logged in as {bot.user.name}.')


@bot.tree.command(name="clear", description="It clears the specified number of messages from the chat.")
async def clear(interaction: discord.Interaction, number: int):
    max_messages = 100
    min_messages = 1

    if interaction.user.guild_permissions.administrator:
        if min_messages < number <= max_messages:
            interaction.response.defer()  
            deleted = await interaction.channel.purge(limit=number)
            await interaction.followup.send(f"{len(deleted)} messages have been deleted.")
        else:
            await interaction.response.send_message("The number of messages you entered is invalid.", ephemeral=True)
    else:
        await interaction.response.send_message("You do not have the necessary permissions.", ephemeral=True)




TOKEN = "BOT_TOKEN"
bot.run(TOKEN)

I just wanted my bot to indicate how many messages were deleted as written in the code.

New contributor

Aktuğ İ. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật