1

最近,我一直在尝试学习 C#,因此我正在使用 DSharpPlus 来制作 Discord Bot。我对这个机器人的计划是做很多事情,但主要的事情之一是创建一种方法来确保我服务器中的每个人都“同意规则”。我希望它看起来不错,所以我组装了一个 DiscordEmbed 并开始尝试在这个 API 中使用交互性的方法。我想直接向用户(DMChannel)发送一条消息,询问他们是否接受使用反应的规则yesReaction & noReaction。我尝试了很多方法,但由于某种原因WaitForReactionAsync(),一旦做出反应,我就无法继续。下面是我的代码片段。

一般问题:这可能吗?如果可以,我做错了什么,因为这在我的公会中的正常渠道上有效。

        public async Task<Task> OnMemberJoin(GuildMemberAddEventArgs e)
        {
            var thumbnail = new EmbedThumbnail { Url = e.Client.CurrentUser.AvatarUrl, };
            var rulesEmbed = new DiscordEmbedBuilder
            {
                Title = "Do you accept these rules?",
                Description = "By accepting these rules you agree that if any of the above are broken, it could/will result in a perm ban from G Hub.",
                Thumbnail = thumbnail,
                Color = DiscordColor.Red,
            };

            var message = await e.Member.SendMessageAsync(embed: rulesEmbed);

            var yesEmoji = DiscordEmoji.FromName(e.Client, ":white_check_mark:");
            var noEmoji = DiscordEmoji.FromName(e.Client, ":x:");

            await Task.Delay(1000);

            await message.CreateReactionAsync(yesEmoji);
            await message.CreateReactionAsync(noEmoji);

            await Task.Delay(1000);
            var interactivity = e.Client.GetInteractivity();
            var reactionResult = await interactivity.WaitForReactionAsync(x => 
                x.Message == message, 
                e.Member,
                TimeSpan.FromSeconds(60)
            );

            if (reactionResult.TimedOut)
            {
                var reactionMember = e.Member;
                await e.Guild.BanMemberAsync(reactionMember, 0, "Kicked for declining Rules.");
                await e.Guild.UnbanMemberAsync(e.Member);
                await message.DeleteAsync();
                return Task.CompletedTask;
            }

            if (reactionResult.Result.Emoji == yesEmoji)
            {
                //Guest Role
                var role = e.Guild.GetRole(742143094761324565);
                var roleMember = e.Member;
                await roleMember.GrantRoleAsync(role);
                await message.DeleteAsync();
                return Task.CompletedTask;
            }
            else if (reactionResult.Result.Emoji == noEmoji)
            {
                var role = e.Guild.GetRole(742143094761324565);
                var roleMember = e.Member;
                if (roleMember.Roles.Contains(role))
                {
                    await roleMember.RevokeRoleAsync(role);
                    await e.Guild.BanMemberAsync(roleMember, 0, "Kicked for declining Rules.");
                    await e.Guild.UnbanMemberAsync(roleMember);
                    await message.DeleteAsync();
                }
                return Task.CompletedTask;
            }
            else
            {
                var role = e.Guild.GetRole(742143094761324565);
                var roleMember = e.Member;
                await roleMember.RevokeRoleAsync(role);
                await e.Guild.BanMemberAsync(roleMember, 0, "Kicked for using different emoji in Accepting Rules.");
                await e.Guild.UnbanMemberAsync(roleMember);
                await message.DeleteAsync();
                return Task.CompletedTask;
            }
        }
4

2 回答 2

0

e.Member在 DM 中不起作用。请改用 e.Message.Author。

var reactionResult = await interactivity.WaitForReactionAsync(x => 
            x.Message == message, 
            e.Message.Author,
            TimeSpan.FromSeconds(60)
        );
于 2020-11-07T16:33:11.540 回答
0

您必须DiscordClient.UseInteractivity()在创建机器人后使用,像这样

var discord = new DiscordClient();

discord.UseInteractivity(new InteractivityConfiguration() 
{ 
    PollBehaviour = PollBehaviour.KeepEmojis,
    Timeout = TimeSpan.FromSeconds(30)
});
于 2021-08-22T23:24:50.247 回答