C# 如何等待作者对discord.net作出反应 所以我正在写一个机器人,我在试图确认一个作者必须通过对消息的反应来确认。我试着在别的地方到处寻找,但没有结果。任何帮助都将不胜感激 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"); }

C# 如何等待作者对discord.net作出反应 所以我正在写一个机器人,我在试图确认一个作者必须通过对消息的反应来确认。我试着在别的地方到处寻找,但没有结果。任何帮助都将不胜感激 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"); },c#,discord.net,C#,Discord.net,您需要使用事件 下面是ReactionaAdded事件的基本实现(未测试,可能不正确): 公共类Bot{ 公共DiscordSocketClient; 公共Bot(){ //在这里初始化你的客户端。。。 client=newdiscordsocketclient(/*…*/); client.ReactionAdded+=ReactionAdded\u事件; } public void reaction added_事件(可缓存消息、ISocketMessageChannel、SocketRea

您需要使用事件

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

公共类Bot{
公共DiscordSocketClient;
公共Bot(){
//在这里初始化你的客户端。。。
client=newdiscordsocketclient(/*…*/);
client.ReactionAdded+=ReactionAdded\u事件;
}
public void reaction added_事件(可缓存消息、ISocketMessageChannel、SocketReaction反应)
{
//检查消息是否在正确的频道,是否是您想要的表情符号,等等。。。
}
}

虽然您的链接内容似乎回答了这个问题,但我能否请您回答问题,并添加一个代码片段,说明OP如何修复其代码以使其正常工作?请阅读社区问答,特别注意答案部分的链接当然,但OP没有提供太多代码,因此它将非常通用,不一定适用于他的案例嘿!我希望您的问题得到解决(我猜是因为您接受了我的答案lol),但我刚刚注意到您的代码中有一个错误:“run=false;”必须放在“break”之前;否则它将不会执行,因为break将立即退出当前迭代!是的,我放弃了上面的代码,转而使用事件解决方案。当然,因为休息时间应该放在枪战之后,肯定只是我这边的一个失误:)谢谢你的帮助。不客气!
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...
    }
}