Question3r
Question3r

Reputation: 3832

Discord C# Bot - Delete the latest message

I am using the rogue exception package for my discord bot.

When a user calls the bot by a command, I want the bot delete his message before executing the command.

So in my "MessageReceived"-Event i have this code so far:

private async Task MessageReceived(SocketMessage s)
        {
            var msg = s as SocketUserMessage; // the input

            if (msg == null || !msg.Content.StartsWith("!") || msg.Author.IsBot) // return if null, msg is no command, msg is written by the bot
                return;

            if (msg.Channel is SocketGuildChannel guildChannel) // just delete the command if the msg is written in the guild channel, not a private message channel
                await ??? // Delete the command
}

So does someone know, what to write there =?

Upvotes: 3

Views: 6628

Answers (2)

Leonardo Ferreira
Leonardo Ferreira

Reputation: 11

Example:

(sorry, but some of the code strings are in Portuguese... this is a voting system)

        [Command("Votar")]
        [Summary("Abro uma votação, com opções de Sim e Não.")]
        [RequireBotPermission(ChannelPermission.AddReactions)]
        public async Task NovoVoto([Remainder] string secondPart)
        {
            Log.CMDLog(Context.User.Username, "Votar", Context.Guild.Name);

            if (secondPart.Length >= 200)
            {
                await Context.Channel.SendMessageAsync("Perdão, porém seu voto não deve ter mais de 200 caracteres");
                return;
            }

            var embed = new EmbedBuilder();
            embed.WithColor(new Color(126, 211, 33));
            embed.WithTitle("VOTE AQUI!");
            embed.WithDescription(secondPart);
            embed.WithFooter($"Votação criada por: {Context.User.Username}");

            RestUserMessage msg = await Context.Channel.SendMessageAsync("", embed: embed);
            await msg.AddReactionAsync(new Emoji("✅"));
            await msg.AddReactionAsync(new Emoji("❌"));

            //Delete the command message from the user
            await Context.Message.DeleteAsync();
        }

Upvotes: 1

WQYeo
WQYeo

Reputation: 4071

I can see that you are using Discord.NET API for your bot.
So from this documentation here.

Take a look at the Method list in the properties of SocketMessage. (By the way, take a look at the right side of the webpage, you should be able to see a bar that allows you to navigate easily)

In case you are wondering why we are looking at SocketMessage, that is because your delegate will run whenever a user posts a message, and that message is your SocketMessage, hence that is what you would want to delete.

You should be able to see a method called: DeleteAsync. That would be what you want.
(Screenshot is below)

For the arguments, you can see that its a RequestOption datatype and by default it has been set null for you. You can change that, but I highly recommend using the default.

Also a good thing to take note that is the bot will do nothing (and return an exception) if it does not have the permission to manage messages.

Screenshot of the method

Upvotes: 1

Related Questions