Example #1
0
        static public void email(string names, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
        {
            if (string.IsNullOrEmpty(SMTPHost))
            {
                if (ChBotAlert != null)
                {
                    ChBotAlert.SendMessageAsync($"No SMTP host set up");
                }
                return;
            }

            string alarmMessage = $"{Program.BotName} Alarm (manual Discord request)";

            foreach (var nameItem in names.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var name = nameItem.ToLower();
                if (!name.EndsWith("mail"))
                {
                    DiscordUser user;
                    if (!Program.UserList.TryGetValue(name, out user))
                    {
                        user = Program.UserList.Values.FirstOrDefault(x => x.DiscordName.ToLower() == name || x.Name.ToLower() == name);
                    }

                    if (user != null)
                    {
                        SendEmail(user.email, alarmMessage, alarmMessage, ChBotAlert);
                    }
                    else if (ChBotAlert != null)
                    {
                        ChBotAlert.SendMessageAsync("name not found!");
                    }
                }
            }
        }
Example #2
0
        static Task SendEmail(string To, string Subject, string Message, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
        {
            Task task = null;

            if (string.IsNullOrEmpty(SMTPHost))
            {
                if (ChBotAlert != null)
                {
                    ChBotAlert.SendMessageAsync($"No SMTP host set up");
                }
                return(task);
            }

            try
            {
                task = Task.Run(() =>
                {
                    var message = new MimeMessage();
                    message.From.Add(new MailboxAddress(EmailFromAddress));
                    message.To.Add(new MailboxAddress(To));
                    message.Subject = Subject;

                    message.Body = new TextPart("plain")
                    {
                        Text = Message
                    };

                    using (var client = new SmtpClient())
                    {
                        // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                        client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                        client.Connect(SMTPHost, SMTPPort, SecureSocketOptions.Auto);

                        // Note: only needed if the SMTP server requires authentication
                        client.Authenticate(SMTPUsername, SMTPPassword);
                        client.Timeout = 10000;
                        client.Send(message);
                        client.Disconnect(true);

                        if (ChBotAlert != null)
                        {
                            ChBotAlert.SendMessageAsync($"Sent e-mail {To}");
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                if (ChBotAlert != null)
                {
                    ChBotAlert.SendMessageAsync($"Send e-mail error {ex.Message}");
                }
            }
            return(task);
        }
        public Task CallAsync(string Name, string Number, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
        {
            Task task;

            try
            {
                if (ChBotAlert == null)
                {
                    ChBotAlert = BotClient.Instance.Our_BotAlert;
                }

                task = Task.Run(() =>
                {
                    string sipp         = "/app/sipp/sipp";
                    string timeout      = "30s";
                    string dialplanPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data/dialplan.xml");

                    string perms = $"{Host} -au {Username} -ap {Password} -l 1 -m 1 -sf {dialplanPath} -timeout {timeout} -s {Number.Replace(" ", "")}";

                    var process = new Process
                    {
                        StartInfo =
                        {
                            FileName        = sipp,
                            Arguments       = perms,
                            UseShellExecute = false
                                              //RedirectStandardOutput = true,
                                              //RedirectStandardError = true
                        },
                    };

                    ChBotAlert.SendMessageAsync($"Calling {Name} {Number}");
                    process.Start();
                    if (!process.WaitForExit(60000))
                    {
                        ChBotAlert.SendMessageAsync($"{Name} Call timed out.");
                    }
                    else
                    {
                        ChBotAlert.SendMessageAsync($"{Name} Call Ended.");
                        process.Dispose();
                    }
                });
                return(task);
            }
            catch (Exception ex)
            {
                if (ChBotAlert != null)
                {
                    ChBotAlert.SendMessageAsync($"Call error: {ex.Message}");
                }
                Console.Write("Call error: " + ex.Message);
            }
            return(null);
        }
        public Task CallAsync(string Name, string Number, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
        {
            try
            {
                return(Task.Run(() =>
                {
                    Number = string.Join("", Number.Split('(', ')', '-', ' '));

                    var client = new RestClient($"https://api.twilio.com/2010-04-01/Accounts/{Username}")
                    {
                        Timeout = 5000,
                        Authenticator = new HttpBasicAuthenticator(Username, Password)
                    };

                    var request = new RestRequest("Calls", Method.POST);
                    request.AddParameter("Url", TwimletURL);
                    request.AddParameter("To", $"{Number}");
                    request.AddParameter("From", $"{CallingNumber}");

                    IRestResponse response = client.Execute(request);

                    if (response.ResponseStatus == ResponseStatus.Completed && ChBotAlert != null)
                    {
                        var content = response.Content;
                        var pt1 = response.Content.IndexOf("<Message>");
                        if (pt1 > 0)
                        {
                            var pt2 = response.Content.IndexOf("</M", pt1);
                            ChBotAlert.SendMessageAsync($"{Name} {response.Content.Substring(pt1 + 9, pt2 - pt1 - 9)}");
                        }
                        else if (response.Content.Contains("AnsweredBy"))
                        {
                            ChBotAlert.SendMessageAsync($"{Name} call in progress");
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                if (ChBotAlert != null)
                {
                    ChBotAlert.SendMessageAsync($"Call error: {ex.Message}");
                }

                Console.Write($"Call error: {ex.Message}");
                return(null);
            }
        }
Example #5
0
        //Incoming Discord Message
        private async Task MessageCreateEvent(MessageCreateEventArgs e)
        {
            try
            {
                if (e.Author.IsBot)
                {
                    return;                  //Reject our own messages
                }
                if (e.Channel == Factom_BotAlert)
                {
                    Our_BotAlert.SendMessageAsync($"#operators-alert:```{e.Message.Content}```");
                    ProcessFactomAlarm(e.Message.Content);
                    return;
                }

                if (e.Channel.GuildId == FactomServerID)
                {
                    return;                 //Ignore Factom's Discord server
                }
                Commands.DiscordMessage(e); //Forward message to commands lookup.

                if (Our_BotAlert == null)
                {
                    e.Channel.SendMessageAsync("WARNING: Your bot alert channel is not found. Check the settings tab for the correct name, and discord permissions");
                }

                Console.Write(e.Message);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }
        static public void call(string names, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
        {
            if (Dialler == null)
            {
                GetSetings();
                if (Dialler == null)
                {
                    if (ChBotAlert == null)
                    {
                        ChBotAlert = BotClient.Instance.Our_BotAlert;
                    }
                    ChBotAlert.SendMessageAsync("SIP not set up");
                    return;
                }
            }

            var dialList = new List <Task>();

            foreach (var nameItem in names.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var name = nameItem.ToLower();
                if (!name.EndsWith("all"))
                {
                    DiscordUser user;
                    if (!Program.UserList.TryGetValue(name, out user))
                    {
                        user = Program.UserList.Values.FirstOrDefault(x => x.DiscordName.ToLower() == name || x.Name.ToLower() == name);
                    }

                    if (user != null)
                    {
                        dialList.Add(Dialler.CallAsync(user.DiscordName, user.Tel, ChBotAlert));
                    }
                    else if (ChBotAlert != null)
                    {
                        ChBotAlert.SendMessageAsync("name not found!");
                    }
                }
            }

            //We had some issues with calling too many numbers at once, so we throttle it to 5 at a time.
            if (dialList.Count > 0)
            {
                StartAndWaitAllThrottledAsync(dialList, 5, 61000);
            }
        }
Example #7
0
        public async Task <ArkLogMsg[]> UpdateGameLog()
        {
            try
            {
                string[] rawString = await FetchNewLogData();

                //Parse each of these
                List <ArkLogMsg> logItems = new List <ArkLogMsg>();
                foreach (string raw in rawString)
                {
                    ArkLogMsg msg = ArkLogParser.ParseMessage(raw);
                    if (msg != null)
                    {
                        logItems.Add(msg);
                    }
                }
                //Check if we got anything
                if (logItems.Count == 0)
                {
                    return(new ArkLogMsg[0]);
                }
                DSharpPlus.Entities.DiscordChannel channel = null;
                if (logItems.Count > 0)
                {
                    //Get Discord server.
                    channel = await Program.discord.GetChannelAsync(notificationChannel);
                }
                //Foreach
                foreach (ArkLogMsg msg in logItems)
                {
                    switch (msg.type)
                    {
                    case ArkLogMsgType.Death_Generic:
                        string header = msg.target.tribe + "'s " + msg.target.dinoClass + " died!";
                        if (msg.target.dinoClass == null)
                        {
                            header = msg.target.name + " of tribe " + msg.target.tribe + " died!";
                        }
                        var embed = Tools.GenerateEmbed(header, "Darn. " + msg.time, msg.target.name + " (Level " + msg.target.level.ToString() + ")", DSharpPlus.Entities.DiscordColor.DarkButNotBlack);
                        await channel.SendMessageAsync(embed : embed);

                        break;

                    case ArkLogMsgType.Death_KilledBy:
                        string headerTwo = msg.target.ToNiceString() + " was killed by " + msg.actioner.ToNiceString();
                        var    embedTwo  = Tools.GenerateEmbed(headerTwo, "Oh darn!", msg.target.ToLongString(), DSharpPlus.Entities.DiscordColor.DarkButNotBlack);
                        await channel.SendMessageAsync(embed : embedTwo);

                        break;

                    case ArkLogMsgType.NewTame:
                        var embedThree = Tools.GenerateEmbed(msg.actioner.tribe + " tamed a " + msg.target.name + "!", "Nice tame!", "Level " + msg.target.level.ToString() + "!", DSharpPlus.Entities.DiscordColor.Yellow);
                        await channel.SendMessageAsync(embed : embedThree);

                        break;

                    case ArkLogMsgType.Chat:
                        //This is a bit of a jank-fix that might break in the future.
                        string msgContent = msg.messageContent.Substring(3);
                        var    embedFour  = Tools.GenerateEmbed("Message from " + msg.messageSteamName, msg.ParseTimeString() + " - " + name, msgContent, DSharpPlus.Entities.DiscordColor.LightGray);
                        await channel.SendMessageAsync(embed : embedFour);

                        break;

                    case ArkLogMsgType.DisconnectJoin:
                        var color = DSharpPlus.Entities.DiscordColor.Red;
                        if (msg.messageContent == "joined")
                        {
                            color = DSharpPlus.Entities.DiscordColor.Green;     //They actually joined.
                        }
                        var embedFive = Tools.GenerateEmbed("Player " + msg.messageIngameName + " " + msg.messageContent + " the game", msg.ParseTimeString(), "Server " + name, color);
                        await channel.SendMessageAsync(embed : embedFive);

                        break;
                    }
                }
                return(logItems.ToArray());
            } catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(new ArkLogMsg[0]);
            }
        }
Example #8
0
        //Discord Server connected
        private Task Client_GuildAvailable(GuildCreateEventArgs e)
        {
            Console.WriteLine($"Discord Server: {e.Guild.Name} {e.Guild.Id}");

            if (e.Guild.Id == FactomServerID)  //Factom's Discord server
            {
                Factom_BotAlert = e.Guild.Channels.FirstOrDefault(x => x.Id == FactomOperatorAlertChannel);
                if (Factom_BotAlert == null)
                {
                    Console.WriteLine("Warning: Factom ID not found");
                    //We will try finding the name instead.
                    Factom_BotAlert = e.Guild.Channels.FirstOrDefault(x => x.Name.Contains(FactomOperatorAlertChannelString));
                }
                if (Factom_BotAlert == null)
                {
                    SendAlert("Warning: Factom operators-alarts not found");
                }
                else
                {
                    Console.WriteLine($"Factom Alert channel: {Factom_BotAlert.Name}");

                    var me = e.Guild.Members.FirstOrDefault(x => x.Id == _client.CurrentUser.Id);
                    if (me != null)
                    {
                        var permissions = Factom_BotAlert.PermissionsFor(me);
                        if (permissions.HasPermission(Permissions.AccessChannels))
                        {
                            AlarmManager.Clear(AlarmNoFactomChannel);
                        }
                    }
                    else
                    {
                        SendAlert("Warning: My user not found in Discord");
                    }
                }
            }
            else
            {
                string alertChannelString;
                if (SettingsList.TryGetValue("Discord-AlertsChannel", out alertChannelString))
                {
                    alertChannelString = alertChannelString.ToLower().Replace("#", "");
#if DEBUG
                    Our_BotAlert = e.Guild.Channels.FirstOrDefault(x => x.Name.Contains("bot-in-debug"));
                    if (Our_BotAlert == null)
                    {
                        Our_BotAlert = e.Guild.Channels.FirstOrDefault(x => x.Name == alertChannelString);
                    }
#else
                    Our_BotAlert = e.Guild.Channels.FirstOrDefault(x => x.Name == alertChannelString);
                    if (Our_BotAlert != null)
                    {
                        Console.WriteLine($"Our Alert channel: {Our_BotAlert.Name}");
                        if (clsVersion.VersionChangeFlag)
                        {
                            Bot.Our_BotAlert.SendMessageAsync($":drum: Welcome! (removing version number because it's causing it not to compile) :trumpet:");
                        }
                        else
                        {
                            Bot.Our_BotAlert.SendMessageAsync("Hello :innocent:");
                        }

                        if (TextBuffer.Length > 0)
                        {
                            Our_BotAlert.SendMessageAsync(TextBuffer.ToString()).ContinueWith((x) => { TextBuffer.Clear(); });
                        }
                    }
                    else
                    {
                        Console.WriteLine($"ERROR: Can't find AlertChannel {alertChannelString}");
                    }
#endif
                }
                else
                {
                    SendAlert("Warning: Factom operators-alarts not found");
                    Console.WriteLine("Warning: Discord-AlertsChannel not set");
                }
            }

            // since this method is not async, let's return
            // a completed task, so that no additional work
            // is done
            return(Task.CompletedTask);
        }