简体   繁体   English

如何在机器人消息上添加表情符号并能够“投票”?

[英]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.我做了一个 Discord Bot 的 MEME 命令,它发布了来自 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.假设您使用的是 Discord.Net,您需要使用AddReactionAsync方法对您自己的消息做出反应。

Adding Emojis添加表情符号

Emojis are unicode characters.表情符号是 unicode 字符。 To use them, pass the utf-code (eg "\?\?" ) as an argument to the constructor of the Emoji` object.要使用它们,请将 utf 代码(例如"\?\?" )作为参数传递给 Emoji` 对象的构造函数。

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

Adding Emotes添加表情

An Emote is a 'custom Emoji' which can be set by server admins/mods. Emote是一种“ Emote符号”,可以由服务器管理员/模组设置。

As an argument you need to pass a custom Emote string, like this "<:thonkang:282745590985523200>" .作为参数,您需要传递自定义表情字符串,例如"<: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 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.然后您需要跟踪反应并将其与匹配的数据库条目相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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