如何等待作者做出反应 discord.net
How to wait for an author to react 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");
}
您需要使用 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...
}
}
所以我正在编写一个机器人,我正在尝试确认作者必须通过对消息做出反应作为确认来继续进行确认。我曾尝试在其他地方搜索但无济于事。任何帮助将不胜感激。
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");
}
您需要使用 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...
}
}