简体   繁体   中英

How to add emojis on a bot message and be able to “vote”?

I made a MEME command of Discord Bot that posts a list of memes from phpMyAdmin DB. But it is not about that. I want to add specific emojis on that bot message that people could press on those emojis and kinda vote.

It is how it posts:

在此处输入图片说明

It is what I want to do:

在此处输入图片说明

    [Command("meme")]
    public async Task meme(CommandContext commandInfo, int id = -1, SocketUserMessage userMsg, string emoteName)
    {
        var description = "";

        MySqlClient.Connect();

        if (id == -1)
        {
            var query = "SELECT * FROM memes;";
            var memeReader = MySqlClient.GetDataReader(query);

            if (memeReader == null) return;

            while (memeReader.Read())
            {
                var memeid = memeReader.GetUInt64("id");
                var title = memeReader.GetString("title");

                description += $"**{memeid}** {title}\n";
            }
        }
        
        var memeEmbed = new DiscordEmbedBuilder()
        {
            Color = DiscordColor.Gold,
            Title = "**The Meme of The Year**",
            Description = description
        };

        var msg = await commandInfo.Channel.SendMessageAsync(embed: memeEmbed);
    } 

Adding Reactions

Assuming you are using Discord.Net, you need to react to your own message using the AddReactionAsync method.

Adding Emojis

Emojis are unicode characters. To use them, pass the utf-code (eg "\?\?" ) as an argument to the constructor of the Emoji` object.

await userMsg.AddReactionAsync(new Emoji("\U0001f643"));

Adding Emotes

An Emote is a 'custom Emoji' which can be set by server admins/mods.

As an argument you need to pass a custom Emote string, like this "<:thonkang:282745590985523200>" .

A safe wrapper to catch non-existing emotes could look like that (taken from the docs).

public async Task ReactWithEmoteAsync(SocketUserMessage userMsg, string escapedEmote)
{
    if (Emote.TryParse(escapedEmote, out var emote))
    {
        await userMsg.AddReactionAsync(emote);
    }
}

https://docs.stillu.cc/guides/emoji/emoji.html

Counting Reactions

As a second step you need to listen to user-events, when they react to your post. This should be done in a separate event handler, as your poll will likely be up for several hours/days.

Here is an example on how to implement an event handler

You then need to keep track of the reactions and need to associate it with the matching database entry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM