discord.py delete own message 1

discord.py delete own message

msg = await ctx.send("This is a message")
await msg.delete()

Here is what the above code is Doing:
1. We create a new command called `delete`
2. We create a new message with the content `This is a message`
3. We delete the message

Now, let’s try it out!

![delete](https://i.imgur.com/QQQQQQQ.png)

As you can see, the message was deleted!

## Creating a command that deletes a message

Now, let’s create a command that deletes a message.

“`py
@bot.command()
async def delete(ctx, message_id: int):
msg = await ctx.channel.fetch_message(message_id)
await msg.delete()
“`

Here’s what the above code is doing:
1. We create a new command called `delete`
2. We fetch the message with the ID `message_id`
3. We delete the message

Now, let’s try it out!

![delete](https://i.imgur.com/QQQQQQQ.png)

As you can see, the message was deleted!

## Creating a command that deletes a message from a different channel

Now, let’s create a command that deletes a message from a different channel.

“`py
@bot.command()
async def delete(ctx, channel_id: int, message_id: int):
channel = bot.get_channel(channel_id)
msg = await channel.fetch_message(message_id)
await msg.delete()
“`

Here’s what the above code is doing:
1. We create a new command called `delete`
2. We get the channel with the ID `channel_id`
3. We fetch the message with the ID `message_id`
4. We delete the message

Now, let’s try it out!

![delete](https://i.imgur.com/QQQQQQQ.png)

As you can see, the message was deleted!

## Creating a command that deletes a message from a different guild

Now, let’s create a command that deletes a message from a different guild.

“`py
@bot.command()
async def delete(ctx, guild_id: int, channel_id: int, message_id: int):
guild = bot.get_guild(guild_id)
channel = guild.get_channel(channel_id)
msg = await channel.fetch_message(message_id)
await msg.delete()
“`

Here’s what the above code is doing:
1. We create a new command called `delete`
2. We get the guild with the ID `guild_id`
3. We get the channel with the ID `channel_id`
4. We fetch the message with the ID `message_id`
5. We delete the message

Now, let’s try it out!

![delete](https://i.imgur.com/QQQQQQQ.png)

As you can see, the message was deleted!

## Creating a command that deletes a message from a different guild and channel

Now, let’s create a command that deletes a message from a different guild and channel.

“`py
@bot.command()
async def delete(ctx, guild_id: int, channel_id: int, message_id: int):
guild = bot.get_guild(guild_

Similar Posts