添加多个反应以嵌入

时间:2020-07-22 04:21:42

标签: c# discord.net

我知道标题听起来有点像其他东西,但是我的问题有点...奇怪吗? Idk。我正在执行民意调查命令,并且一切正常。但是我想做到这一点,而不是仅仅自动添加四个反应选项,而是对选项进行计数,并基于此添加反应。我已经(在一定程度上)工作,但它使错误400'ing。我没有让它反应,而是告诉我它添加了多少反应...它在8个选项​​中添加了36个反应。代码和结果如下。

代码:

        public async Task MakePoll(IMessageChannel channel, string title, string description, [Remainder] string choices)
        {

            string[] options = choices.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            var zero = new Emoji($"{734770204311027712}");
            var one = new Emoji("1️⃣");
            var two = new Emoji("2️⃣");
            var three = new Emoji("3️⃣");

            var emotes = (zero, one, two, three );

            var choicecount = options.Count() - 1;

            int reactioncount = choicecount;

            var poll = new EmbedBuilder();
            poll.WithTitle(title);
            poll.WithDescription(description);
            for (int a = 0; a <= choicecount; a++)
            {
                poll.AddField($"{a}", $"{options[a].ToString()}");
            }
            
            var derp = await channel.SendMessageAsync("", false, poll.Build());

            for (int b = 1; b < reactioncount; b++)
            {
                //await derp.AddReactionsAsync(had an array of emotes, plugged it in here. just haven't added it back after ctrl+z spam.);
                await Context.Channel.SendMessageAsync($"{b} emotes tried to add");
            }

        }

结果:https://cdn.discordapp.com/attachments/689738717589798913/735340372838056026/unknown.png

1 个答案:

答案 0 :(得分:0)

在您当前的实现中,for循环是您的主要问题。如果数组有4个项目,则将循环4次并在每个循环(4x4)中添加4个反应。如果您使用的是AddReactions,则应调用一次,而不是在循环中调用。

public async Task MakePoll(IMessageChannel channel, string title, string description, [Remainder] string choices)
{
    //code to declare emotes, create and send embed

    //Add reactions to the sent message
    for (int b = 1; b < reactioncount; b++)
    {
        await derp.AddReactionsAsync(emotes); //this adds 4 reactions every time you loop, you don't want that
    }
    //ideally, within your loop you would have been selecting the array indexes that
    //should be used. That is, if you created an array with emotes 0-9, but you only 
    //have 6 options you would loop through the options and call AddReaction 
    //not AddReactions... passing the reaction at position 0, position 1 and so on 
    //up to position 6.
}

选项1:动态创建应添加的表情的列表,并调用AddReactionsAsync而不循环

public async Task MakePoll(IMessageChannel channel, string title, string description, [Remainder] string choices)
{
    string[] options = choices.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

    var choicecount = options.Count() - 1;
    var poll = new EmbedBuilder();
    var emotes = new List<Emoji>();
    
    poll.WithTitle(title);
    poll.WithDescription(description);
    
    for (int a = 0; a <= choicecount; a++)
    {
        poll.AddField($"{a}", $"{options[a].ToString()}");
        emotes.Add(new Emoji($"{a}\u20e3")); //add an emoji for each poll option
    }
    
    var derp = await channel.SendMessageAsync("", false, poll.Build());

    //here we do NOT loop the adding of reactions when adding from an array
    await derp.AddReactionsAsync(emotes.ToArray);
}

选项2:循环显示多个选项,然后调用AddReaction添加动态创建的反应

public async Task MakePoll(IMessageChannel channel, string title, string description, [Remainder] string choices)
{
    string[] options = choices.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

    var choicecount = options.Count() - 1;
    var poll = new EmbedBuilder();
    
    poll.WithTitle(title);
    poll.WithDescription(description);
    
    for (int a = 0; a <= choicecount; a++)
    {
        poll.AddField($"{a}", $"{options[a].ToString()}");
    }
    
    var derp = await channel.SendMessageAsync("", false, poll.Build());

    //If you are looping, you should use the singular AddReactionAsync
    for (int b = 1; b = options.count; b++)
    {
        await derp.AddReactionAsync(new Emoji($"{b}\u20e3"));
    }
}