Discord.NET 2.0.0通过ReactionAdded从反应中获取消息

时间:2018-07-16 18:48:55

标签: c# discord.net

目前,我正在开发一个“建议”系统,您可以在其中提交建议。我正在制作一个ReactionHandler系统,它可以查看机器人发送的消息中有多少反应,如果有足够的反应,它将根据哪个反应最多自动批准或拒绝该反应。

在此系统上工作时,我需要获取添加了反应的消息,以便编辑嵌入。在执行此操作的过程中,当尝试获取添加了反应的消息时,出现错误“对象引用未设置为对象的实例”。

代码如下:

 private async Task ReactionAdded(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction)
    {
        if (suggestionpairs.ContainsKey(reaction.MessageId))
        {
            IUserMessage message = reaction.Message.GetValueOrDefault();

            if (reaction.Emote.Name == "")
                suggestionpairs[reaction.MessageId].agree++;
            if (reaction.Emote.Name == "")
                suggestionpairs[reaction.MessageId].disagree++;

            if (suggestionpairs[reaction.MessageId].agree == 2)
            {
                try
                {
                    var reason = "Majority Upvoted.";

                    if (message != null && message.Embeds.Count == 1)
                    {
                        EmbedBuilder eb = message.Embeds.First().ToEmbedBuilder()
                            .WithColor(Color.Green)
                            .WithTitle("Suggestion Approved");

                        //Check if there is already a reason.
                        if (eb.Fields.Find(x => x.Name == "Reason") == null)
                        {
                            eb.AddField("Reason", reason);
                        }
                        else
                        {
                            foreach (var field in eb.Fields)
                            {
                                if (field.Name == "Reason")
                                {
                                    field.WithValue(reason);
                                }
                            }
                        }

                        await message.ModifyAsync(x => x.Embed = eb.Build());
                        Console.WriteLine($"Suggestion {message.Id} has been Approved!");
                    }
                    else
                    {
                        Console.WriteLine($"Suggestion {message.Id} either is not valid or doesn't have an Embed!");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            if (suggestionpairs[reaction.MessageId].disagree == 2)
            {
                try
                {
                    var reason = "Majority Downvoted.";

                    if (message != null && message.Embeds.Count == 1)
                    {
                        EmbedBuilder eb = message.Embeds.First().ToEmbedBuilder()
                            .WithColor(Color.Red)
                            .WithTitle("Suggestion Denied");

                        //Check if there is already a reason.
                        if (eb.Fields.Find(x => x.Name == "Reason") == null)
                        {
                            eb.AddField("Reason", reason);
                        }
                        else
                        {
                            foreach (var field in eb.Fields)
                            {
                                if (field.Name == "Reason")
                                {
                                    field.WithValue(reason);
                                }
                            }
                        }

                        await message.ModifyAsync(x => x.Embed = eb.Build());
                        Console.WriteLine($"Suggestion {message.Id} has been Approved!");
                    }
                    else
                    {
                        Console.WriteLine($"Suggestion {message.Id} either is not valid or doesn't have an Embed!");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
    }

0 个答案:

没有答案