Can't delete bot message discord.py / pycord)

143 views Asked by At

I am having trouble deleting discord bot's message. Here is the code to replicate the problem I am having:

import discord
from tools.config import TOKEN

bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is ready and online!")

@bot.slash_command(name="chat", description="Тут можно добавить описание")
async def chat(ctx, msg):
        bot_msg = await ctx.respond(f'some message with tagging {ctx.author.mention}')
        await bot_msg.delete(delay=5)   

bot.run(TOKEN)

And I get the error:

await bot_msg.delete(delay=5)
AttributeError: 'Interaction' object has no attribute 'delete'

I am using py-cord==2.4.1 but I believe it's the same thing as discord.py

1

There are 1 answers

0
Raymus On BEST ANSWER

Pycord is not the same as discord.py.

discord.py is a modern, easy to use, feature-rich, and async ready API wrapper for Discord.

Pycord is a fork of discord.py.

Slash commands on discord.py

There are two ways of doing this, either by using Client from discord or Bot from discord.ext.commands.

discord.Client method:

import discord
from discord import app_commands

client = discord.Client(intents=discord.Intents.default)
tree = app_commands.CommandTree(client)

@tree.command(name="mycommand", description="command description")
async def mycommand_callback(interaction: discord.Interaction):
    await interaction.response.send_message(content="Hello World")

Meanwhile, commands.Bot has the property tree which is basically the same as app_commands.CommandTree. The main advantage of commands.Bot, is the use of cogs.

Here is how you initialise bot.

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="/", intents=discord.Intents.default())
tree = bot.tree #  Or use bot.tree directly

#  Same command as above

discord.py has resumed development on discord.py after halting it over a year ago. After the announcement of its hold, forks like Pycord started popping up. For the question on which to use, it is up to you. But personally, I recommend sticking with the main discord.py library.

For more information on slash commands in discord.py, discord.Interaction, discord.InteractionResponse.

Deleting the bot's message

After replying to the command user with discord.InteractionResponse (which does not return a message object) you can delete with either the delete_after attribute, or by getting the original_response object and then deleting it.

@tree.command()
async def mycommand(interaction: discord.Interaction):
    await interaction.response.send_message(content=f"some message with tagging {interaction.user.mention}", delete_after=5.0)

Or

@tree.command()
async def mycommand(interaction: discord.Interaction):
    await interaction.response.send_message(content=f"some message with tagging {interaction.user.mention}")
    original_response = await interaction.original_response()
    await asyncio.sleep(5.0)
    await original_response.delete()