2

我正在尝试使用 DSharpPlus 库创建一个不和谐的机器人,如果您对带有特定表情符号的消息做出反应,您将获得特定的角色。这个概念非常简单,但我无法弄清楚一个相当重要的概念。也就是说,我如何让机器人一直监听现有消息的反应。

我尝试通过命令来完成它并且我让它工作,但是据我所知,这种方法的问题是机器人只在我输入命令后监听反应并且它只持续一分钟左右(基于配置)。

public class RoleCommands : BaseCommandModule
{
    [Command("join")]
    public async Task Join(CommandContext ctx)
    {
        var joinEmbed = new DiscordEmbedBuilder
        {
            Title = "Reaction with thumbs up!",
            Color = DiscordColor.Green
        };

        var joinMessage = await ctx.Channel.SendMessageAsync(embed: joinEmbed).ConfigureAwait(false);

        var thumbsUpEmoji = DiscordEmoji.FromName(ctx.Client, ":+1:");
        var thumbsDownEmoji = DiscordEmoji.FromName(ctx.Client, ":-1:");

        await joinMessage.CreateReactionAsync(thumbsUpEmoji).ConfigureAwait(false);
        await joinMessage.CreateReactionAsync(thumbsDownEmoji).ConfigureAwait(false);

        var interactivity = ctx.Client.GetInteractivity();

        var reactionResult = await interactivity.WaitForReactionAsync(x =>
            x.Message == joinMessage
            && x.User == ctx.User
            && x.Emoji == thumbsUpEmoji);

        if (reactionResult.Result.Emoji == thumbsUpEmoji)
        {
            var role = ctx.Guild.GetRole(773965440913375282);
            await ctx.Member.GrantRoleAsync(role).ConfigureAwait(false);

            await joinMessage.DeleteAsync().ConfigureAwait(false);
        }
    }
}

我如何在命令之外执行此操作,我可以将消息 Id 传递给它,然后它一直在听该消息以进行反应,而不是在有限的时间内?

4

2 回答 2

2

我的问题的完整答案是这样使用DiscordClient.MessageReactionAdded += OnReactionAdded;和实现该方法:

private async Task OnReactionAdded(DiscordClient sender, MessageReactionRemoveEventArgs e)
{
    var messageId = e.Message.Id;
    var guild = e.Message.Channel.Guild;
    var reactionName = e.Emoji.GetDiscordName();

    var reactionDetail = ReactionDetails.FirstOrDefault(x =>
        x.MessageId == messageId
        && x.GuildId == guild.Id
        && x.ReactionName == reactionName);

    if (reactionDetail != null)
    {
        var member = e.User as DiscordMember;
        if (member != null)
        {
            var role = guild.Roles.FirstOrDefault(x => x.Value.Id == reactionDetail.RoleId).Value;
            await member.GrantRoleAsync(role).ConfigureAwait(false);
        }
    }
}
于 2020-11-06T18:19:13.870 回答
0

MessageReactionAdded将消息 ID 存储在某处,然后将事件挂在您DiscordClient的身上并在那里执行您的逻辑。

于 2020-11-05T19:23:49.437 回答