public string Build()
        {
            var data = new DiscordWebhookData();

            data.Embeds.Add(_embed);
            return(JsonSerializer.Serialize(data, _jsonOptions));
        }
Exemple #2
0
        private async Task SendDiscordMessageWebhooksAsync(DiscordWebhookData webhook, SessionTextConstructor.DiscordEmbeds discordEmbeds, string contentText)
        {
            var jsonContentSuccessFailure = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.SuccessFailure
            });
            var jsonContentSuccess = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.Success
            });
            var jsonContentFailure = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.Failure
            });

            try
            {
                var jsonContent =
                    (webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnSuccessAndFailure)) ? jsonContentSuccessFailure :
                    ((webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnSuccessOnly) ? jsonContentSuccess : jsonContentFailure));
                var uri = new Uri(webhook.URL);
                using var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                using (await mainLink.HttpClientController.PostAsync(uri, content)) { }
            }
            catch
            {
                mainLink.AddToText($">:> Unable to execute webhook \"{webhook.Name}\" with a finished log session.");
            }
        }
Exemple #3
0
        public FormEditDiscordWebhook(FormDiscordWebhooks discordPingLink, DiscordWebhookData data, int reservedId)
        {
            this.discordPingLink = discordPingLink;
            this.data            = data;
            this.reservedId      = reservedId;
            InitializeComponent();
            Icon             = Properties.Resources.AppIcon;
            Text             = (data is null) ? "Add a new webhook" : "Edit an existing webhook";
            textBoxName.Text = data?.Name ?? string.Empty;
            textBoxUrl.Text  = data?.URL ?? string.Empty;
            switch (data?.SuccessFailToggle ?? DiscordWebhookDataSuccessToggle.OnSuccessAndFailure)
            {
            case DiscordWebhookDataSuccessToggle.OnSuccessOnly:
                radioButtonOnlySuccess.Checked = true;
                break;

            case DiscordWebhookDataSuccessToggle.OnFailOnly:
                radioButtonOnlyFail.Checked = true;
                break;

            default:
                radioButtonOnlySuccessAndFail.Checked = true;
                break;
            }
            checkBoxPlayers.Checked = data?.ShowPlayers ?? true;
            var bosses = Bosses.All;

            bosses = bosses
                     .OrderBy(x => x.Value.Type)
                     .ThenBy(x => x.Value.Name)
                     .ToDictionary(x => x.Key, x => x.Value);
            var teams = Teams.Teams.All;

            foreach (var team in teams.Values)
            {
                comboBoxTeam.Items.Add(team);
            }
            comboBoxTeam.SelectedItem = data?.Team ?? teams[0];
            foreach (var bossNumber in bosses.Keys)
            {
                checkedListBoxBossesEnable.Items.Add(new BossesDisableHelperClass()
                {
                    BossID = bosses[bossNumber].BossId, Text = $"{bosses[bossNumber].Type}: {bosses[bossNumber].Name} ({bosses[bossNumber].BossId})"
                }, data?.IsBossEnabled(bosses[bossNumber].BossId) ?? true);
            }
        }
Exemple #4
0
        private async Task SendDiscordMessageToAllActiveWebhooksAsync(SessionTextConstructor.DiscordEmbeds discordEmbeds, string contentText)
        {
            string jsonContentSuccessFailure = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.SuccessFailure
            });
            string jsonContentSuccess = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.Success
            });
            string jsonContentFailure = JsonConvert.SerializeObject(new DiscordAPIJSONContent()
            {
                Content = contentText,
                Embeds  = discordEmbeds.Failure
            });

            try
            {
                foreach (int key in allWebhooks.Keys)
                {
                    DiscordWebhookData webhook = allWebhooks[key];
                    if (!webhook.Active)
                    {
                        continue;
                    }
                    string jsonContent =
                        (webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnSuccessAndFailure)) ? jsonContentSuccessFailure :
                        ((webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnSuccessOnly) ? jsonContentSuccess : jsonContentFailure));
                    Uri uri = new Uri(webhook.URL);
                    using (StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"))
                    {
                        using (await mainLink.HttpClientController.PostAsync(uri, content)) { }
                    }
                }
            }
            catch
            {
                mainLink.AddToText(">:> Unable to execute active webhooks with a finished log session.");
            }
        }
Exemple #5
0
 private void FormEditDiscordWebhook_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (!string.IsNullOrWhiteSpace(textBoxName.Text.Trim()))
     {
         var successFailToggle = DiscordWebhookDataSuccessToggle.OnSuccessAndFailure;
         if (radioButtonOnlySuccess.Checked)
         {
             successFailToggle = DiscordWebhookDataSuccessToggle.OnSuccessOnly;
         }
         else if (radioButtonOnlyFail.Checked)
         {
             successFailToggle = DiscordWebhookDataSuccessToggle.OnFailOnly;
         }
         if (data is null)
         {
             allWebhooks[reservedId] = new DiscordWebhookData()
             {
                 Active = true, Name = textBoxName.Text, URL = textBoxUrl.Text, SuccessFailToggle = successFailToggle, ShowPlayers = checkBoxPlayers.Checked, BossesDisable = ConvertCheckboxListToList(), Team = (Team)comboBoxTeam.SelectedItem
             };
             discordPingLink.listViewDiscordWebhooks.Items.Add(new ListViewItem()
             {
                 Name = reservedId.ToString(), Text = textBoxName.Text, Checked = true
             });
         }
         else
         {
             var webhook = allWebhooks[reservedId];
             webhook.Active            = data.Active;
             webhook.Name              = textBoxName.Text;
             webhook.URL               = textBoxUrl.Text;
             webhook.SuccessFailToggle = successFailToggle;
             webhook.ShowPlayers       = checkBoxPlayers.Checked;
             webhook.BossesDisable     = ConvertCheckboxListToList();
             webhook.Team              = (Team)comboBoxTeam.SelectedItem;
             discordPingLink.listViewDiscordWebhooks.Items[discordPingLink.listViewDiscordWebhooks.Items.IndexOfKey(reservedId.ToString())] = new ListViewItem()
             {
                 Name = reservedId.ToString(), Text = textBoxName.Text, Checked = data.Active
             };
         }
     }
 }
Exemple #6
0
        public async Task ExecuteAllActiveWebhooksAsync(DPSReportJSON reportJSON)
        {
            string   bossName      = reportJSON.Encounter.Boss + (reportJSON.ChallengeMode ? " CM" : "");
            string   successString = (reportJSON.Encounter.Success ?? false) ? ":white_check_mark:" : "❌";
            string   extraJSON     = (reportJSON.ExtraJSON == null) ? "" : $"Recorded by: {reportJSON.ExtraJSON.RecordedBy}\nDuration: {reportJSON.ExtraJSON.Duration}\nElite Insights version: {reportJSON.ExtraJSON.EliteInsightsVersion}\n";
            string   icon          = "";
            BossData bossData      = Bosses.GetBossDataFromId(reportJSON.Encounter.BossId);

            if (bossData != null)
            {
                bossName = bossData.Name + (reportJSON.ChallengeMode ? " CM" : "");
                icon     = bossData.Icon;
            }
            int color = (reportJSON.Encounter.Success ?? false) ? 32768 : 16711680;
            DiscordAPIJSONContentEmbedThumbnail discordContentEmbedThumbnail = new DiscordAPIJSONContentEmbedThumbnail()
            {
                Url = icon
            };
            DateTime timestampDateTime = DateTime.UtcNow;

            if (DateTime.TryParse(reportJSON.ExtraJSON.TimeStart, out DateTime timeStart))
            {
                timestampDateTime = timeStart;
            }
            string timestamp = timestampDateTime.ToString("yyyy'-'MM'-'ddTHH':'mm':'ssZ");
            DiscordAPIJSONContentEmbed discordContentEmbed = new DiscordAPIJSONContentEmbed()
            {
                Title       = bossName,
                Url         = reportJSON.Permalink,
                Description = $"{extraJSON}Result: {successString}\narcdps version: {reportJSON.EVTC.Type}{reportJSON.EVTC.Version}",
                Color       = color,
                TimeStamp   = timestamp,
                Thumbnail   = discordContentEmbedThumbnail
            };
            DiscordAPIJSONContent discordContentWithoutPlayers = new DiscordAPIJSONContent()
            {
                Embeds = new List <DiscordAPIJSONContentEmbed>()
                {
                    discordContentEmbed
                }
            };
            DiscordAPIJSONContentEmbed discordContentEmbedForPlayers = new DiscordAPIJSONContentEmbed()
            {
                Title       = bossName,
                Url         = reportJSON.Permalink,
                Description = $"{extraJSON}Result: {successString}\narcdps version: {reportJSON.EVTC.Type}{reportJSON.EVTC.Version}",
                Color       = color,
                TimeStamp   = timestamp,
                Thumbnail   = discordContentEmbedThumbnail
            };

            if (reportJSON.Players.Values.Count <= 10)
            {
                List <DiscordAPIJSONContentEmbedField> fields = new List <DiscordAPIJSONContentEmbedField>();
                foreach (DPSReportJSONPlayers player in reportJSON.Players.Values)
                {
                    fields.Add(new DiscordAPIJSONContentEmbedField()
                    {
                        Name = player.CharacterName, Value = $"```\n{player.DisplayName}\n\n{Players.ResolveSpecName(player.Profession, player.EliteSpec)}\n```", Inline = true
                    });
                }
                discordContentEmbedForPlayers.Fields = fields;
            }
            DiscordAPIJSONContent discordContentWithPlayers = new DiscordAPIJSONContent()
            {
                Embeds = new List <DiscordAPIJSONContentEmbed>()
                {
                    discordContentEmbedForPlayers
                }
            };

            try
            {
                string jsonContentWithoutPlayers = JsonConvert.SerializeObject(discordContentWithoutPlayers);
                string jsonContentWithPlayers    = JsonConvert.SerializeObject(discordContentWithPlayers);
                foreach (int key in allWebhooks.Keys)
                {
                    DiscordWebhookData webhook = allWebhooks[key];
                    if (!webhook.Active ||
                        (webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnSuccessOnly) && !(reportJSON.Encounter.Success ?? false)) ||
                        (webhook.SuccessFailToggle.Equals(DiscordWebhookDataSuccessToggle.OnFailOnly) && (reportJSON.Encounter.Success ?? false)) ||
                        webhook.BossesDisable.Contains(reportJSON.Encounter.BossId))
                    {
                        continue;
                    }
                    Uri uri = new Uri(webhook.URL);
                    if (webhook.ShowPlayers)
                    {
                        using (StringContent content = new StringContent(jsonContentWithPlayers, Encoding.UTF8, "application/json"))
                        {
                            using (await mainLink.HttpClientController.PostAsync(uri, content)) { }
                        }
                    }
                    else
                    {
                        using (StringContent content = new StringContent(jsonContentWithoutPlayers, Encoding.UTF8, "application/json"))
                        {
                            using (await mainLink.HttpClientController.PostAsync(uri, content)) { }
                        }
                    }
                }
                if (allWebhooks.Count > 0)
                {
                    mainLink.AddToText(">:> All active webhooks successfully executed.");
                }
            }
            catch
            {
                mainLink.AddToText(">:> Unable to execute active webhooks.");
            }
        }