Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

c# - How to wait for an author to react discord.net

So I am in the middle of writing a bot and I'm trying to make a confirmation thing where author has to proceed by reacting to the message as a confirmation. I have tried searching around elsewhere but to no avail. Any help would be appreciated.

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");
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the SocketClient.ReactionAdded event.

Here's a basic implementation of the ReactionAdded event (didn't test, might be incorrect):

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...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...