Reputation:
I am attempting to create a bot that has the ability to delete only the bots messages after a command has been done. I am aware of how i know that the command has taken place however I am having issues with only deleting the messages that the bot has sent.
await Message.ModifyAsync(msg => msg.Content = "test [edited]");
However this only occurs to the last message that has been sent (This can be fixed relatively easily and I know how to do this) and importantly would otherwise occur to all the messages in the chat! What i want to do is make it so that I only delete messages that were sent by the bot in the first place. Thanks
Upvotes: 0
Views: 2533
Reputation: 4056
Get and store the message that the bot sent as a variable, and delete it later.
var botMsg = await ReplyAsync("A message!");
await botMsg.DeleteAsync();
If you need to delete multiple messages, create a list and store each message to the list. Then use await Context.Channel.DeleteMessagesAsync(list)
at the end of the command.
There are overload methods for passing both list of message ID and the message itself.
Example of passing a list of message ID:
List<ulong> msgToDel = new List<ulong>();
msgToDel.Add((await ReplyAsync("test1")).Id); //Send a msg, then add the msg ID to the list.
msgToDel.Add((await ReplyAsync("test2")).Id);
//Blah...
await Context.Channel.DeleteMessagesAsync(msgToDel);
You can check the documentation for the DeleteMessagesAsync()
here.
Upvotes: 1