コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDiscordBatchClick(object sender, EventArgs e)
        {
            AddTraceMessage("Sending batch to Discord");
            if (Properties.Settings.Default.WebhookURL == null)
            {
                MessageBox.Show("Set a discord webhook url in settings first");
                return;
            }
            var fullDpsReportLogs = new List <FormOperationController>();

            foreach (FormOperationController operation in OperatorBindingSource)
            {
                if (operation.DPSReportLink != null && operation.DPSReportLink.Contains("https"))
                {
                    fullDpsReportLogs.Add(operation);
                }
            }
            if (!fullDpsReportLogs.Any())
            {
                MessageBox.Show("Nothing to send");
                return;
            }
            BtnDiscordBatch.Enabled = false;
            BtnParse.Enabled        = false;
            // first sort by time
            fullDpsReportLogs.Sort((x, y) =>
            {
                return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart)));
            });
            var fullDpsReportsLogsByDate = fullDpsReportLogs.GroupBy(x => DateTime.Parse(x.BasicMetaData.LogStart).Date).ToDictionary(x => x.Key, x => x.ToList());
            // split the logs so that a single embed does not reach the discord embed limit and also keep a reasonable size by embed
            string message = "";
            bool   start   = true;

            foreach (KeyValuePair <DateTime, List <FormOperationController> > pair in fullDpsReportsLogsByDate)
            {
                if (!start)
                {
                    message += "\r\n";
                }
                start = false;
                var splitDpsReportLogs = new List <List <FormOperationController> >()
                {
                    new List <FormOperationController>()
                };
                message += pair.Key.ToString("yyyy-MM-dd") + " - ";
                List <FormOperationController> curListToFill = splitDpsReportLogs.First();
                foreach (FormOperationController controller in pair.Value)
                {
                    if (curListToFill.Count < 40)
                    {
                        curListToFill.Add(controller);
                    }
                    else
                    {
                        curListToFill = new List <FormOperationController>()
                        {
                            controller
                        };
                        splitDpsReportLogs.Add(curListToFill);
                    }
                }
                foreach (List <FormOperationController> dpsReportLogs in splitDpsReportLogs)
                {
                    EmbedBuilder embedBuilder = ProgramHelper.GetEmbedBuilder();
                    embedBuilder.WithCurrentTimestamp();
                    embedBuilder.WithFooter(pair.Key.ToString("yyyy-MM-dd"));
                    dpsReportLogs.Sort((x, y) =>
                    {
                        var categoryCompare = x.BasicMetaData.FightCategory.CompareTo(y.BasicMetaData.FightCategory);
                        if (categoryCompare == 0)
                        {
                            return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart)));
                        }
                        return(categoryCompare);
                    });
                    string currentSubCategory = "";
                    var    embedFieldBuilder  = new EmbedFieldBuilder();
                    var    fieldValue         = "I can not be empty";
                    foreach (FormOperationController controller in dpsReportLogs)
                    {
                        string subCategory = controller.BasicMetaData.FightCategory.GetSubCategoryName();
                        string toAdd       = "[" + controller.BasicMetaData.FightName + "](" + controller.DPSReportLink + ") " + (controller.BasicMetaData.FightSuccess ? " :white_check_mark: " : " :x: ") + ": " + controller.BasicMetaData.FightDuration;
                        if (subCategory != currentSubCategory)
                        {
                            embedFieldBuilder.WithValue(fieldValue);
                            embedFieldBuilder = new EmbedFieldBuilder();
                            fieldValue        = "";
                            embedBuilder.AddField(embedFieldBuilder);
                            embedFieldBuilder.WithName(subCategory);
                            currentSubCategory = subCategory;
                        }
                        else if (fieldValue.Length + toAdd.Length > 1024)
                        {
                            embedFieldBuilder.WithValue(fieldValue);
                            embedFieldBuilder = new EmbedFieldBuilder();
                            fieldValue        = "";
                            embedBuilder.AddField(embedFieldBuilder);
                        }
                        else
                        {
                            fieldValue += "\r\n";
                        }
                        fieldValue += toAdd;
                    }
                    embedFieldBuilder.WithValue(fieldValue);
                    message += new WebhookController(Properties.Settings.Default.WebhookURL, embedBuilder.Build()).SendMessage() + " - ";
                }
            }

            MessageBox.Show(message);
            //
            BtnDiscordBatch.Enabled = !_anyRunning;
            BtnParse.Enabled        = !_anyRunning;
        }
コード例 #2
0
        private string DiscordBatch(out List <ulong> ids)
        {
            ids = new List <ulong>();
            AddTraceMessage("Discord: Sending batch to Discord");
            if (Properties.Settings.Default.WebhookURL == null)
            {
                AddTraceMessage("Discord: No webhook url given");
                return("Set a discord webhook url in settings first");
            }
            var fullDpsReportLogs = new List <FormOperationController>();

            foreach (FormOperationController operation in OperatorBindingSource)
            {
                if (operation.DPSReportLink != null && operation.DPSReportLink.Contains("https"))
                {
                    fullDpsReportLogs.Add(operation);
                }
            }
            if (!fullDpsReportLogs.Any())
            {
                AddTraceMessage("Discord: Nothing to send");
                return("Nothing to send");
            }
            // first sort by time
            AddTraceMessage("Discord: Sorting logs by time");
            fullDpsReportLogs.Sort((x, y) =>
            {
                return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart)));
            });
            AddTraceMessage("Discord: Splitting logs by start day");
            var fullDpsReportsLogsByDate = fullDpsReportLogs.GroupBy(x => DateTime.Parse(x.BasicMetaData.LogStart).Date).ToDictionary(x => x.Key, x => x.ToList());
            // split the logs so that a single embed does not reach the discord embed limit and also keep a reasonable size by embed
            string message = "";
            bool   start   = true;

            foreach (KeyValuePair <DateTime, List <FormOperationController> > pair in fullDpsReportsLogsByDate)
            {
                if (!start)
                {
                    message += "\r\n";
                }
                start = false;
                var splitDpsReportLogs = new List <List <FormOperationController> >()
                {
                    new List <FormOperationController>()
                };
                message += pair.Key.ToString("yyyy-MM-dd") + " - ";
                List <FormOperationController> curListToFill = splitDpsReportLogs.First();
                AddTraceMessage("Discord: Splitting message to avoid reaching discord's character limit");
                foreach (FormOperationController controller in pair.Value)
                {
                    if (curListToFill.Count < 40)
                    {
                        curListToFill.Add(controller);
                    }
                    else
                    {
                        curListToFill = new List <FormOperationController>()
                        {
                            controller
                        };
                        splitDpsReportLogs.Add(curListToFill);
                    }
                }
                foreach (List <FormOperationController> dpsReportLogs in splitDpsReportLogs)
                {
                    EmbedBuilder embedBuilder = ProgramHelper.GetEmbedBuilder();
                    AddTraceMessage("Discord: Creating embed for " + dpsReportLogs.Count + " logs");
                    var first = DateTime.Parse(dpsReportLogs.First().BasicMetaData.LogStart);
                    var last  = DateTime.Parse(dpsReportLogs.Last().BasicMetaData.LogEnd);
                    embedBuilder.WithFooter(pair.Key.ToString("dd/MM/yyyy") + " - " + first.ToString("T") + " - " + last.ToString("T"));
                    AddTraceMessage("Discord: Sorting logs by category");
                    dpsReportLogs.Sort((x, y) =>
                    {
                        int categoryCompare = x.BasicMetaData.FightCategory.CompareTo(y.BasicMetaData.FightCategory);
                        if (categoryCompare == 0)
                        {
                            return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart)));
                        }
                        return(categoryCompare);
                    });
                    string currentSubCategory = "";
                    var    embedFieldBuilder  = new EmbedFieldBuilder();
                    string fieldValue         = "I can not be empty";
                    AddTraceMessage("Discord: Building embed body");
                    foreach (FormOperationController controller in dpsReportLogs)
                    {
                        string subCategory = controller.BasicMetaData.FightCategory.GetSubCategoryName();
                        string toAdd       = "[" + controller.BasicMetaData.FightName + "](" + controller.DPSReportLink + ") " + (controller.BasicMetaData.FightSuccess ? " :white_check_mark: " : " :x: ") + ": " + controller.BasicMetaData.FightDuration;
                        if (subCategory != currentSubCategory)
                        {
                            embedFieldBuilder.WithValue(fieldValue);
                            embedFieldBuilder = new EmbedFieldBuilder();
                            fieldValue        = "";
                            embedBuilder.AddField(embedFieldBuilder);
                            embedFieldBuilder.WithName(subCategory);
                            currentSubCategory = subCategory;
                        }
                        else if (fieldValue.Length + toAdd.Length > 1024)
                        {
                            embedFieldBuilder.WithValue(fieldValue);
                            embedFieldBuilder = new EmbedFieldBuilder();
                            fieldValue        = "";
                            embedBuilder.AddField(embedFieldBuilder);
                            embedFieldBuilder.WithName(subCategory);
                        }
                        else
                        {
                            fieldValue += "\r\n";
                        }
                        fieldValue += toAdd;
                    }
                    embedFieldBuilder.WithValue(fieldValue);
                    AddTraceMessage("Discord: Sending embed");
                    try
                    {
                        ids.Add(WebhookController.SendMessage(Properties.Settings.Default.WebhookURL, embedBuilder.Build(), out string curMessage));
                        AddTraceMessage("Discord: embed sent " + curMessage);
                        message += curMessage + " - ";
                    }
                    catch (Exception ex)
                    {
                        AddTraceMessage("Discord: couldn't send embed " + ex.Message);
                        message += ex.Message + " - ";
                    }
                }
            }
            return(message);
        }