private void btnTestDiscord_Click(object sender, RoutedEventArgs e)
 {
     if ((this.gvDiscord.SelectedItems.Count == 1) && (this.gvDiscord.SelectedItem is DiscordObject))
     {
         DiscordObject selectedItem = this.gvDiscord.SelectedItem as DiscordObject;
         new Notificator().Discord(Notificator.NotificationType.Restock, selectedItem);
     }
 }
Example #2
0
        internal void Discord(NotificationType type, DiscordObject rec = null)
        {
            List <DiscordObject> list = null;

            if (rec == null)
            {
                if (type == NotificationType.Restock)
                {
                    list = (from x in Global.SETTINGS.DISCORD
                            where x.DiscordType == DiscordObject.DiscordMessageEnum.Restock
                            select x).ToList <DiscordObject>();
                }
                else if (type == NotificationType.Atc)
                {
                    list = (from x in Global.SETTINGS.DISCORD
                            where x.DiscordType == DiscordObject.DiscordMessageEnum.Atc
                            select x).ToList <DiscordObject>();
                }
                else if (type == NotificationType.Paypal)
                {
                    list = (from x in Global.SETTINGS.DISCORD
                            where x.DiscordType == DiscordObject.DiscordMessageEnum.PayPal
                            select x).ToList <DiscordObject>();
                }
                else
                {
                    list = (from x in Global.SETTINGS.DISCORD
                            where x.DiscordType == DiscordObject.DiscordMessageEnum.Checkout
                            select x).ToList <DiscordObject>();
                }
            }
            else
            {
                list = new List <DiscordObject> {
                    rec
                };
            }
            if (list != null)
            {
                foreach (DiscordObject obj2 in list)
                {
                    try
                    {
                        string webhookId    = obj2.WebhookId;
                        string webhookToken = obj2.WebhookToken;
                        string message      = obj2.Message;
                        if (rec != null)
                        {
                            message = "TESTING MESSAGE";
                        }
                        else
                        {
                            message = message.Replace("#product_url#", this._url).Replace("#product_title#", this._productName).Replace("#time#", DateTime.Now.ToLocalTime().ToString("dd/MM/yy hh:mm:sstt", CultureInfo.InvariantCulture)).Replace("#rnd#", "(" + _rnd.Next(0, 20).ToString() + ")").Replace("#paypal#", this._task.PaypalLink).Replace("#orderno#", this._task.OrderNo).Replace("#profile_name#", this._task.CheckoutProfile).Replace("#size#", this._task.PickedSize).Replace("#website#", this._task.HomeUrl).Replace("#task_name#", this._task.Name);
                        }
                        if (this._task != null)
                        {
                            Global.SETTINGS.PROFILES.First <ProfileObject>(x => x.Id == this._task.CheckoutId);
                            if (this._task.Login)
                            {
                                message = message.Replace("#username#", this._task.Username).Replace("#password#", this._task.Password);
                            }
                        }
                        DiscordWebhookClient client = new DiscordWebhookClient(ulong.Parse(webhookId), webhookToken);
                        CommandServiceConfig config = new CommandServiceConfig {
                            LogLevel = LogSeverity.Info,
                            CaseSensitiveCommands = false
                        };
                        new CommandService(config);
                        if (message.Contains("#img#") && !string.IsNullOrEmpty(this._task.ImgUrl))
                        {
                            message = message.Replace("#img#", "");
                            EmbedBuilder builder = new EmbedBuilder {
                                ImageUrl = this._task.ImgUrl
                            };
                            Embed[] embedArray1 = new Embed[] { builder };
                            client.SendMessageAsync(message, false, embedArray1, null, null, null);
                        }
                        else
                        {
                            message = message.Replace("#img#", "");
                            client.SendMessageAsync(message, false, null, null, null, null);
                        }
                    }
                    catch (Exception exception)
                    {
                        if (this._task != null)
                        {
                            Global.Logger.Error($"Task '{this._task.Name}': error sending discord message", exception);
                        }
                        else
                        {
                            Global.Logger.Error("Error sending discord test message", exception);
                        }
                    }
                }
            }
        }