【问题标题】:How to wait for an author to react discord.net如何等待作者做出反应 discord.net
【发布时间】:2020-01-27 17:53:38
【问题描述】:

所以我正在编写一个机器人,我正在尝试做一个确认事情,作者必须通过对消息做出反应作为确认来继续。我曾尝试在其他地方四处寻找,但无济于事。任何帮助将不胜感激。

if (File.Exists(Program.channelLocation))
        {
            var msgR = await msg.Channel.SendMessageAsync($"Channel is currently set to: {File.ReadAllText(Program.channelLocation)}. Are you sure you want to change this?");

            var emoji = new Discord.Emoji("✅");

            await msgR.AddReactionAsync(emoji);

            var reactedUsers = await msgR.GetReactionUsersAsync(emoji, 100).FlattenAsync();

            IUser user = msg.Author;

            bool run = true;

            Console.WriteLine("loop started");

            while (run)
            {
                if (reactedUsers.Contains(user))
                {
                    run = false;
                    break;
                }
            }

            Console.WriteLine("loop ended");
        }

【问题讨论】:

  • 嘿!我希望你的问题得到解决(我猜是因为你接受了我的回答,哈哈)但我只是注意到你的代码中有一个错误:“run = false;”必须放在“break;”之前否则它不会被执行,因为 break 会立即退出当前迭代!
  • 是的,我放弃了上面的代码并改用事件解决方案。当然,休息应该放在布尔之后,绝对只是我的一个错误:)谢谢你的帮助。
  • 不客气!

标签: c# discord.net


【解决方案1】:

您需要使用SocketClient.ReactionAdded 事件。

这是 ReactionAdded 事件的基本实现(未测试,可能不正确):

public class Bot {
    public DiscordSocketClient client;

    public Bot() {
        //Initialize your client here...
        client = new DiscordSocketClient(/* ... */);

        client.ReactionAdded += ReactionAdded_Event;
    }

    public void ReactionAdded_Event(Cacheable<IUserMessage, UInt64> message, ISocketMessageChannel channel, SocketReaction reaction)
    {
        //Check if the message is in the right channel, if it's the emoji you want, etc...
    }
}

【讨论】:

  • 虽然您的链接内容似乎回答了这个问题,但我可以请您 edit 您的回答并添加代码 sn-p 显示 OP 如何修复其代码以使其工作吗?另请阅读社区问答about flagging answers,尤其要密切关注答案链接部分
  • 当然,但是 OP 没有提供太多代码,所以它会非常通用,不一定适用于他的案例
猜你喜欢
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 2018-12-09
  • 2021-01-27
  • 1970-01-01
  • 2021-12-30
  • 2020-08-25
  • 2020-10-16
相关资源
最近更新 更多