How can I delete a discord message with a Webhook? I already added a Webhook send message but I cant find out how to delete the messages that the Webhook sent without opening the app and deleting it manually.
CodePudding user response:
If you are using the DSharpPlus library, the process of deleting a message from a Webhook is very simple! Just store the value that the method ExecuteAsync(); returns in a variable and then use the method DeleteAsync();
As in this code:
//Here I am creating a Webhook that will send the message "Message to Delete"
DiscordWebhookBuilder WebhookSample = new DiscordWebhookBuilder
{
Username = "Username",
Content = "Message to Delete",
};
//Here I am getting the bot's main webhook in the current channel
DiscordWebhook currentChannelWebhook = await ctx.Channel.CreateWebhookAsync("Sample");
//Here I make the Webhook send a message and save the value
Task<DiscordMessage> result = await currentChannelWebhook.ExecuteAsync(WebhookSample);
//And finally, I delete the message sent
await result.DeleteAsync();
CodePudding user response:
If you're using Discord.Net then deleting a message is handled pretty easily like this:
var sent = await Context.Channel.SendMessageAsync("Hello world!");
await sent.DeleteAsync();
