Esempio n. 1
0
        public void Activate(CommandEventArg e)
        {
            int indexSpace = e.AfterCMD.IndexOf(' ');

            if (indexSpace <= 0)
            {
                Coding.Discord.SendMessage(e.ChannelID, DescriptionUsage);
                return;
            }
            if (!bool.TryParse(e.AfterCMD.Substring(0, indexSpace), out bool status))
            {
                Coding.Discord.SendMessage(e.ChannelID, "Could not parse status");
                return;
            }

            string message = null;

            if (e.AfterCMD.Length != indexSpace - 1)
            {
                message = e.AfterCMD.Remove(0, indexSpace + 1);
            }

            using (Database.GAFContext context = new Database.GAFContext())
            {
                int id = context.BotMaintenance.Max(m => m.Id);
                Database.Models.BotMaintenance maint = context.BotMaintenance.FirstOrDefault(m => m.Id == id);

                if (maint == null)
                {
                    context.BotMaintenance.Add(new Database.Models.BotMaintenance()
                    {
                        Enabled      = status,
                        Notification = message
                    });
                }
                else
                {
                    maint.Enabled = status;

                    if (message != null)
                    {
                        maint.Notification = message;
                    }
                }

                context.SaveChanges();
            }
        }
Esempio n. 2
0
        public void Activate(CommandEventArg e)
        {
            if (!string.IsNullOrEmpty(e.AfterCMD))
            {
                ICommand command = (Program.CommandHandler as CommandHandler).ActiveCommands.FirstOrDefault(c => (c.Activator.Equals(e.AfterCMD[0]) &&
                                                                                                                  c.CMD.Equals(e.AfterCMD.Remove(0, 1), StringComparison.CurrentCultureIgnoreCase)) ||
                                                                                                            c.CMD.Equals(e.AfterCMD, StringComparison.CurrentCultureIgnoreCase));

                if (command == null)
                {
                    Coding.Discord.SendMessage(e.ChannelID, "Command not found");
                    return;
                }

                DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                {
                    Title = "Command Info for " + command.Activator + command.CMD
                };

                builder.AddField((!string.IsNullOrEmpty(command.Description) ? command.Description : "null"), (!string.IsNullOrEmpty(command.DescriptionUsage) ? command.DescriptionUsage : "null"));

                Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: builder.Build());
                return;
            }

            ICommand[] commands = (Program.CommandHandler as CommandHandler).ActiveCommands.ToArray();
            string     response = "";

            Database.Models.BotUsers user;

            using (Database.GAFContext context = new Database.GAFContext())
                user = context.BotUsers.First(u => u.DiscordId == (long)e.DUserID);

            AccessLevel access = user == null ? AccessLevel.User : (AccessLevel)user.AccessLevel;

            foreach (ICommand cmd in commands)
            {
                if (cmd.AccessLevel <= access)
                {
                    response += Environment.NewLine + $"{cmd.Activator}{cmd.CMD}: {cmd.Description}";
                }
            }

            Coding.Discord.SendMessage(e.ChannelID, $"```{Environment.NewLine}{response}{Environment.NewLine}```");
        }
Esempio n. 3
0
        public bool ActivateCommand(DSharpPlus.Entities.DiscordMessage message, AccessLevel access)
        {
            try
            {
                if (message == null || string.IsNullOrEmpty(message.Content) || char.IsLetterOrDigit(message.Content[0]))
                {
                    return(false);
                }

                if (ActiveCommands == null)
                {
                    ActiveCommands = new List <ICommand>();
                }

                string[] cmdsplit = message.Content.Split(' ');

                if (cmdsplit == null || cmdsplit.Length == 0)
                {
                    return(false);
                }

                ulong  id        = message.Author.Id;
                string name      = message.Author.Username;
                var    guild     = message.Channel.Guild;
                var    guildid   = (ulong?)message.Channel.GuildId;
                ulong  chid      = message.Channel.Id;
                char   activator = message.Content[0];
                string _cmd      = cmdsplit[0].Substring(1, cmdsplit[0].Length - 1);
                string aftercmd  = "";
                if (message.Content.Length > cmdsplit[0].Length + 1)
                {
                    aftercmd = message.Content.Remove(0, cmdsplit[0].Length + 1);
                }

                CommandEventArg arg = new CommandEventArg(id, name, (guild == null ? (ulong?)null : guildid), chid, activator, _cmd, aftercmd);


                ICommand cmd = null;

                //For now i hardcode this cause I'm lazy
                if (activator.Equals('>'))
                {
                    Console.WriteLine(">");
                    //TextCommand.OnTextCommand(arg);
                    return(true);
                }

                cmd = ActiveCommands.Find(cmd_ => cmd_.Activator.Equals(arg.Activator) && cmd_.CMD.Equals(arg.CMD));

                if (cmd == null)
                {
                    return(false);
                }

                if ((int)cmd.AccessLevel > (int)access)
                {
                    return(false);
                }

                using (Database.GAFContext context = new Database.GAFContext())
                {
                    int dbid  = context.BotMaintenance.Max(m => m.Id);
                    var maint = context.BotMaintenance.FirstOrDefault(m => m.Id == dbid);

                    if (maint.Enabled && access < AccessLevel.Admin)
                    {
                        Coding.Discord.SendMessage(chid, "Bot is currently in maintenance, please try again later" + Environment.NewLine +
                                                   "Info: " + maint.Notification ?? "no information");

                        return(false);
                    }
                }

                cmd.Activate(arg);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), LogLevel.Trace);
                return(false);
            }
        }
Esempio n. 4
0
 public virtual void Activate(CommandEventArg e)
 {
     UseCount++;
 }
Esempio n. 5
0
        public void Activate(CommandEventArg e)
        {
            try
            {
                Team team;
                using (Database.GAFContext context = new Database.GAFContext())
                    team = context.Team.FirstOrDefault(t => t.Name.Equals(e.AfterCMD, StringComparison.CurrentCultureIgnoreCase));

                if (team == null)
                {
                    Coding.Discord.SendMessage(e.ChannelID, "Could not find team " + e.AfterCMD);
                    return;
                }

                List <int> playerIds = new List <int>();

                using (Database.GAFContext context = new Database.GAFContext())
                    playerIds.AddRange(context.TeamPlayerList.Where(tpl => tpl.TeamId == team.Id).Select(tpl => (int)tpl.PlayerListId));

                List <Player> players = new List <Player>();

                using (Database.GAFContext context = new Database.GAFContext())
                {
                    foreach (int playerId in playerIds)
                    {
                        Player player = context.Player.FirstOrDefault(p => p.Id == (long)playerId);

                        if (player == null)
                        {
                            Logger.Log("Could not find player: " + playerId, LogLevel.WARNING);
                            continue;
                        }

                        players.Add(player);
                    }
                }

                Dictionary <string, string> countryCodes = new Dictionary <string, string>();

                foreach (Player p in players)
                {
                    if (countryCodes.Keys.FirstOrDefault(k => k.Equals(p.Country, StringComparison.CurrentCultureIgnoreCase)) == null)
                    {
                        countryCodes.Add(p.Country, null);
                    }
                }

                using (Database.GAFContext context = new Database.GAFContext())
                {
                    for (int i = 0; i < countryCodes.Keys.Count; i++)
                    {
                        countryCodes[countryCodes.Keys.ElementAt(i)] = context.BotCountryCode
                                                                       .FirstOrDefault(c => c.CountryCode.Equals(countryCodes.Keys.ElementAt(i),
                                                                                                                 StringComparison.CurrentCultureIgnoreCase))
                                                                       .Country;
                    }
                }

                DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                {
                    Title        = "Country info for team: " + e.AfterCMD,
                    ThumbnailUrl = team.Image ?? null,
                    Description  = "Average/Median PP: " + $"{team.AveragePP ?? '?'}/{team.MedianPP ?? '?'}",
                    Timestamp    = DateTime.UtcNow
                };

                for (int i = 0; i < players.Count; i++)
                {
                    builder.AddField("Player", players[i].Nickname, true);
                    builder.AddField("Location", countryCodes[countryCodes.Keys.First(k => k.Equals(players[i].Country, StringComparison.CurrentCultureIgnoreCase))].Trim('"'), true);
                    builder.AddField("PP Raw", $"{players[i].PPRaw ?? '?'}", true);
                }

                DiscordEmbed embed = builder.Build();

                DiscordChannel channel = Coding.Discord.GetChannel(e.ChannelID);
                channel.SendMessageAsync(embed: embed).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Esempio n. 6
0
        public void Activate(CommandEventArg e)
        {
            try
            {
                if (string.IsNullOrEmpty(e.AfterCMD))
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                string[] paramSplit = e.AfterCMD.Split(' ');

                int pageIndex = 1;
                switch (paramSplit.Length)
                {
                default:
                    break;

                case 2:
                    if (!paramSplit[0].Equals("delete", StringComparison.InvariantCultureIgnoreCase))
                    {
                        int.TryParse(paramSplit[1], out pageIndex);
                        goto case 1;
                    }

                    if (!int.TryParse(paramSplit[1], out int timerId))
                    {
                        Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(content: "Could not parse timer id " + paramSplit[1], embed: BuildUsageInfo());
                        return;
                    }

                    lock (_timersLock)
                    {
                        using (BaseDBReader <BotTimer> timerReader = new BaseDBReader <BotTimer>())
                        {
                            timerReader.Remove(bt => bt.Id == timerId);
                            timerReader.Save();
                        }
                    }

                    Coding.Discord.SendMessage(e.ChannelID, "Removed timer with id " + timerId);
                    return;

                case 1:
                    if (paramSplit[0].Equals("list"))
                    {
                        List <BotTimer> timers     = GetTimersByDiscordId(e.DUserID);
                        double          totalPages = 1;

                        if (timers.Count > 10)
                        {
                            totalPages = timers.Count / 10.0;
                        }

                        if ((int)totalPages < totalPages)
                        {
                            totalPages = (int)totalPages + 1;
                        }

                        timers = GetAvailableIndexes((pageIndex - 1) * 10, 10, timers);

                        if (timers.Count == 0)
                        {
                            Coding.Discord.SendMessage(e.ChannelID, "No reminders found");
                            return;
                        }

                        Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildRemindMeList(timers, pageIndex, (int)totalPages, e.DUserID, e.ChannelID, e.GuildID));
                        return;
                    }
                    break;

                case 0:
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                string[] timeSplit = paramSplit[0].Split(':');

                if (timeSplit.Length < 3)
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                if (!int.TryParse(timeSplit[0], out int days) ||
                    !int.TryParse(timeSplit[1], out int hours) ||
                    !int.TryParse(timeSplit[2], out int minutes))
                {
                    Coding.Discord.GetChannel(e.ChannelID).SendMessageAsync(embed: BuildUsageInfo());
                    return;
                }

                days    = Math.Max(0, Math.Min(days, 31));
                hours   = Math.Max(0, Math.Min(hours, 23));
                minutes = Math.Max(0, Math.Min(minutes, 59));

                if (days == 0 &&
                    hours == 0 &&
                    minutes == 0)
                {
                    Coding.Discord.SendMessage(e.ChannelID, "Days, hours and minutes cannot be 0 all at once!");
                    return;
                }

                string message = null;

                if (paramSplit.Length >= 2)
                {
                    message = paramSplit[1];

                    for (int i = 2; i < paramSplit.Length; i++)
                    {
                        message += ' ' + paramSplit[i];
                    }
                }

                BotTimer timer;
                using (BaseDBReader <BotTimer> timerReader = new BaseDBReader <BotTimer>())
                {
                    DateTime startTime = DateTime.UtcNow;
                    DateTime endTime   = startTime.AddHours(hours).AddMinutes(minutes).AddDays(days);

                    timer = timerReader.Add(new BotTimer(true, DateTime.UtcNow, endTime, (long)e.DUserID, false, (long)e.ChannelID, !e.GuildID.HasValue, message));
                    timerReader.Save();
                }

                Coding.Discord.SendMessage(e.ChannelID, $"Created timer {timer.Id}, expires on: {GetExpirationDate(timer)} UTC");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString(), LogLevel.ERROR);
            }
        }