Esempio n. 1
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            bool globalBan = false;

            //discordId, osuId
            (ulong, ulong)ids = ParseIds(args, ref globalBan);

            if (ids.Item1 == 0 && ids.Item2 == 0)
            {
                if (args.Parameters[0].Equals("list", StringComparison.CurrentCultureIgnoreCase) && TryListBans(args))
                {
                    return;
                }

                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse parameters");
                return;
            }
            else if (globalBan && args.AccessLevel < AccessLevel.Dev)
            {
                HelpCommand.ShowHelp(args.Channel, this, "You do not have enough permissions to use this command");
                return;
            }

            string reason = globalBan ? args.ParameterString.Remove(0, args.Parameters[0].Length + args.Parameters[1].Length + args.Parameters[2].Length + 2).TrimStart(' ') :
                            args.ParameterString.Remove(0, args.Parameters[0].Length + args.Parameters[1].Length + 1).TrimStart(' ');

            if (string.IsNullOrEmpty(reason))
            {
                reason = null;
            }

            BanManager.BanUser((long)ids.Item1, (long)ids.Item2, globalBan ? 0 : (long)args.Guild.Id, reason);
        }
Esempio n. 2
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            string param = args.Parameters[0].ToLower(CultureInfo.CurrentCulture);

            args.Parameters.RemoveAt(0);

            switch (param)
            {
            default:
                HelpCommand.ShowHelp(args.Channel, this);
                return;

            case "player":
            case "p":
                OnPlayerCommand(client, args);
                return;

            case "team":
            case "t":
                OnTeamCommand(client, args);
                return;

            case "match":
            case "m":
                OnMatchCommand(client, args);
                return;
            }
        }
Esempio n. 3
0
        private void OnPlayerCommand(DiscordHandler client, CommandEventArg args)
        {
            string param = args.Parameters[0].ToLower(CultureInfo.CurrentCulture);

            args.Parameters.RemoveAt(0);

            switch (param)
            {
            case "profile":
            case "p":
                OnPlayerProfile(client, args);
                return;

            case "top":
            case "t":
                OnPlayerTopList(client, args);
                return;

            case "last":
            case "l":
                OnPlayerTopList(client, args, true);
                return;

            default:
                HelpCommand.ShowHelp(args.Channel, this);
                return;
            }
        }
Esempio n. 4
0
        private void OnMatchCommand(DiscordHandler client, CommandEventArg args)
        {
            using DBContext c = new DBContext();
            long matchId = -1;

            //team a vs team b
            if (args.Parameters.Count > 1)
            {
                StringBuilder vsSb = new StringBuilder();

                for (int i = 0; i < args.Parameters.Count; i++)
                {
                    vsSb.Append(" " + args.Parameters[i]);
                }

                vsSb.Remove(0, 1);

                string matchName = vsSb.ToString();

                matchId = c.SeasonResult.FirstOrDefault(sr => sr.MatchName.Equals(matchName, StringComparison.CurrentCultureIgnoreCase) &&
                                                        sr.DiscordGuildId == (long)args.Guild.Id)?.Id ?? -1;
            }
            else //matchid
            {
                if (!int.TryParse(args.Parameters[0], out int mid))
                {
                    HelpCommand.ShowHelp(args.Channel, this);
                    return;
                }

                matchId = mid;

                if (!c.SeasonResult.Any(sr => sr.MatchId == matchId &&
                                        sr.DiscordGuildId == (long)args.Guild.Id))
                {
                    client.SendSimpleEmbed(args.Channel, "Could not find match " + matchId).ConfigureAwait(false);
                    return;
                }
            }

            DiscordEmbed embed = GetMatchEmbedFromDB((int)matchId);

            if (embed == null)
            {
                DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
                {
                    Title       = "Could not find match ",
                    Description = Resources.InvisibleCharacter
                };

                embed = builder.Build();
            }

            args.Channel.SendMessageAsync(embed: embed);
        }
Esempio n. 5
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (!bool.TryParse(args.Parameters[0], out bool status))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse status");
                return;
            }

            string message = args.ParameterString.Remove(0, args.Parameters[0].Length + 1);

            Program.MaintenanceScanner.SetMaintenanceStatus(status, message);
            args.Channel.SendMessageAsync($"Set status to: {status} and message to: {message}");
        }
Esempio n. 6
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (!args.Parameters[0].Equals("reset", StringComparison.CurrentCultureIgnoreCase))
            {
                AccessLevel newAccess;
                if (args.Parameters[1].TryParseEnum(out AccessLevel acc))
                {
                    newAccess = acc;
                }
                else
                {
                    HelpCommand.ShowHelp(args.Channel, this, string.Format(CultureInfo.CurrentCulture, ResourceExceptions.FailedParseException, "Access Level"));
                    return;
                }

                if (newAccess == AccessLevel.Dev)
                {
                    HelpCommand.ShowHelp(args.Channel, this, "Dev can only be set via DB");
                    return;
                }

                if (!handler.SetCommandAccessLevel(args.Parameters[0], args.Guild.Id, newAccess))
                {
                    client.SendSimpleEmbed(args.Channel, "Failed to change accesslevel", "Command not found or command not overwrittable").ConfigureAwait(false);
                }
                else
                {
                    client.SendSimpleEmbed(args.Channel, "Accesslevel changed", "Changed commands accesslevel").ConfigureAwait(false);
                }
            }
            else
            {
                using DBContext c = new DBContext();
                CommandAccess ca = c.CommandAccess.FirstOrDefault(ca => ca.DiscordGuildId == (long)args.Guild.Id &&
                                                                  ca.TypeName.StartsWith(args.Parameters[1], StringComparison.CurrentCultureIgnoreCase));

                if (ca != null)
                {
                    c.CommandAccess.Remove(ca);
                    c.SaveChanges();

                    client.SendSimpleEmbed(args.Channel, "Resetted", "Resetted command " + args.Parameters[1]).ConfigureAwait(false);
                }
                else
                {
                    client.SendSimpleEmbed(args.Channel, "Not Found/Resetted", "Could not find command or command already resetted").ConfigureAwait(false);
                }
            }
        }
Esempio n. 7
0
        private void GetUser(CommandEventArg args)
        {
            if (args.Parameters.Count == 0)
            {
                HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.NotEnoughParameters);
                return;
            }
            if (!long.TryParse(args.Parameters[0], out long osuId))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Could not parse osu id");
                return;
            }

            args.Channel.SendMessageAsync(embed: GSStatisticHandler.BuildPlayerProfile(osuId)).ConfigureAwait(false);
        }
Esempio n. 8
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            switch (args.Parameters[0].ToLower(System.Globalization.CultureInfo.CurrentCulture))
            {
            case "list":
                args.Parameters.RemoveAt(0);
                ShowList(args);
                return;

            case "remove":
                args.Parameters.RemoveAt(0);
                Remove(args);
                return;

            default:
                if (args.Parameters.Count < 2)
                {
                    HelpCommand.ShowHelp(args.Channel, this);
                    return;
                }
                break;
            }

            DateTime?date = ParseDate(args.Parameters, 0, 62);

            if (!date.HasValue)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Could not parse date");
                return;
            }

            args.Parameters.RemoveAt(0);

            StringBuilder msg = new StringBuilder(args.Parameters[0]);

            for (int i = 1; i < args.Parameters.Count; i++)
            {
                msg.Append(' ' + args.Parameters[i]);
            }

            using DBContext c = new DBContext();
            var reminder = c.Reminder.Add(new SkyBot.Database.Models.Reminder((long)args.User.Id, (long)args.Channel.Id, msg.ToString(), date.Value)).Entity;

            c.SaveChanges();

            args.Channel.SendMessageAsync("Created your reminder with id " + reminder.Id +
                                          " for date: " + reminder.EndDate);
        }
Esempio n. 9
0
        private void OnAdd(CommandEventArg e)
        {
            if (e.Parameters.Count == 0)
            {
                HelpCommand.ShowHelp(e.Channel, this);
                return;
            }

            StringBuilder msgBuilder = new StringBuilder(e.Parameters[0]);

            for (int i = 1; i < e.Parameters.Count; i++)
            {
                msgBuilder.Append(" " + e.Parameters[i]);
            }

            Ticket ticket = new Ticket((long)e.User.Id, (long)e.Guild.Id, 0, 0, 1, DateTime.Now, msgBuilder.ToString());

            using (DBContext c = new DBContext())
            {
                ticket = c.Ticket.Add(ticket).Entity;
                c.SaveChanges();
            }

            var duser = e.User;

            DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
            {
                Title       = $"New ticket by {duser.Username}#{duser.Discriminator} ({duser.Mention} (copy&paste for mention))",
                Description = $"ID: {ticket.Id}"
            };

            builder.AddField("Message", ticket.Message);

            DiscordChannel ticketChannel = e.Guild.GetChannel(GetTicketRoomId(e.Guild));

            ticketChannel.SendMessageAsync(embed: builder.Build());

            e.Message.DeleteAsync("Ticket submitted").Wait();

            System.Threading.Tasks.Task.Run(() =>
            {
                e.Member.CreateDmChannelAsync().ConfigureAwait(false).GetAwaiter().GetResult()
                .SendMessageAsync($"Submitted your ticket");
            });
        }
Esempio n. 10
0
        private bool TryListBans(CommandEventArg args)
        {
            List <BannedUser> bans = BanManager.GetBans(guildId: args.Parameters.Count < 2 ? 0 :
                                                        args.Parameters[1].Equals("global", StringComparison.CurrentCultureIgnoreCase) ? 0 :
                                                        args.Parameters[1].Equals("local", StringComparison.CurrentCultureIgnoreCase) ? (long)args.Guild.Id : 0);

            if (bans.Count == 0)
            {
                HelpCommand.ShowHelp(args.Channel, this, "No bans found");
                return(true);
            }

            int page = 1;

            if (args.Parameters.Count >= 3 && int.TryParse(args.Parameters[2], out int page_))
            {
                page = page_;
            }

            DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
            {
                Title       = "Ban list",
                Description = Resources.InvisibleCharacter
            };
            EmbedPageBuilder epb = new EmbedPageBuilder(3);

            epb.AddColumn("Discord ID");
            epb.AddColumn("Osu ID");
            epb.AddColumn("Reason");

            for (int i = 0; i < bans.Count; i++)
            {
                epb.Add("Discord ID", bans[i].DiscordUserId.ToString(CultureInfo.CurrentCulture));
                epb.Add("Osu ID", bans[i].OsuUserId.ToString(CultureInfo.CurrentCulture));
                epb.Add("Reason", string.IsNullOrEmpty(bans[i].Reason) ? "none" : bans[i].Reason);
            }

            DiscordEmbed embed = epb.BuildPage(builder, page);

            args.Channel.SendMessageAsync(embed: embed).ConfigureAwait(false);

            return(true);
        }
Esempio n. 11
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            switch (args.Parameters[0].ToLower(CultureInfo.CurrentCulture))
            {
            case "exampleprofile":
            case "ep":
                args.Channel.SendMessageAsync(embed: GSStatisticHandler.GetTestProfile()).ConfigureAwait(false);
                break;

            case "get":
                args.Parameters.RemoveAt(0);
                GetUser(args);
                break;

            default:
                HelpCommand.ShowHelp(args.Channel, this);
                return;
            }
        }
Esempio n. 12
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (!int.TryParse(args.Parameters[0], out int rank))
            {
                HelpCommand.ShowHelp(args.Channel, this, $"Could not parse the rank {args.Parameters[0]}");
                return;
            }

            if (!int.TryParse(args.Parameters[1], out int badgeCount))
            {
                HelpCommand.ShowHelp(args.Channel, this, $"Could not parse the badge count {args.Parameters[1]}");
                return;
            }

            if (args.Parameters.Count >= 3 && args.Parameters[2].Equals("test", StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }

            client.SendSimpleEmbed(args.Channel, "BWS Rank", GSStatisticHandler.CalculateBWS(rank, badgeCount).ToString()).ConfigureAwait(false);
        }
Esempio n. 13
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (args.Parameters.Count > 1)
            {
                switch (args.Parameters[1].ToLower(System.Globalization.CultureInfo.CurrentCulture))
                {
                case "guildid":
                case "id":
                    args.Channel.SendMessageAsync("This is a reserved key, you cannot change this");
                    return;
                }
            }

            using DBContext c = new DBContext();
            switch (args.Parameters[0].ToLower(System.Globalization.CultureInfo.CurrentCulture))
            {
            case "set":
                args.ParameterString = args.ParameterString.Remove(0, args.Parameters[0].Length).TrimStart(' ');
                args.Parameters.RemoveAt(0);
                Set(args, c);
                break;

            case "get":
                args.ParameterString = args.ParameterString.Remove(0, args.Parameters[0].Length).TrimStart(' ');
                args.Parameters.RemoveAt(0);
                Get(args);
                break;

            case "list":
                args.ParameterString = args.ParameterString.Remove(0, args.Parameters[0].Length).TrimStart(' ');
                args.Parameters.RemoveAt(0);
                List(args);
                break;

            default:
                HelpCommand.ShowHelp(args.Channel, this);
                break;
            }
        }
Esempio n. 14
0
        private void Remove(CommandEventArg args)
        {
            if (!long.TryParse(args.Parameters[0], out long timerId))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse Reminder id");
                return;
            }

            using DBContext c = new DBContext();
            var reminder = c.Reminder.FirstOrDefault(r => r.DiscordUserId == (long)args.User.Id &&
                                                     r.Id == timerId);

            if (reminder == null)
            {
                args.Channel.SendMessageAsync($"Reminder with id {timerId} not found");
                return;
            }

            c.Reminder.Remove(reminder);
            c.SaveChanges();

            args.Channel.SendMessageAsync("Removed reminder with id " + timerId);
        }
Esempio n. 15
0
        /// <summary>
        /// Sends an embed to a specific channel
        /// </summary>
        /// <param name="content">message content</param>
        /// <param name="webhook">Use webhook or message</param>
        /// <param name="webhookUser">Webhook username</param>
        private static void SendEmbed(DiscordHandler client, ICommand cmd, CommandEventArg args, DiscordEmbed embed, string content, ulong channelId, DiscordGuild guild, bool webhook, string webhookUser, string webhookAvatar = null)
        {
            DiscordChannel channel;

            try
            {
                channel = guild.GetChannel(channelId);
            }
            catch (DSharpPlus.Exceptions.NotFoundException)
            {
                throw new ReadableCmdException("Could not find the discord channel");
            }

            //TODO: make it so webhook and default can both send up to 5 embeds max at once

            if (webhook)
            {
                try
                {
                    using (WebHookHandler whh = new WebHookHandler(channel, webhookUser, webhookAvatar))
                    {
                        whh.SendEmbed(content, new DiscordEmbed[] { embed }).ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                }
                catch (UriFormatException)
                {
                    HelpCommand.ShowHelp(args.Channel, cmd, "Invalid link");
                    return;
                }
            }
            else
            {
                channel.SendMessageAsync(content: content, embed: embed).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            client.SendSimpleEmbed(args.Channel, "Sent Embed").ConfigureAwait(false);
        }
Esempio n. 16
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (args.Parameters[0].Equals("list", StringComparison.CurrentCultureIgnoreCase))
            {
                int page = 1;

                if (args.Parameters.Count > 1 && int.TryParse(args.Parameters[1], out int newPage))
                {
                    page = newPage;
                }

                using DBContext c = new DBContext();

                List <DiscordRoleBind> drbs = c.DiscordRoleBind.Where(drb => drb.GuildId == (long)args.Guild.Id).ToList();

                if (drbs.Count == 0)
                {
                    client.SendSimpleEmbed(args.Channel, "Could not find any role binds").ConfigureAwait(false);
                    return;
                }

                StringBuilder roleIdBuilder = new StringBuilder();
                StringBuilder accessBuilder = new StringBuilder();

                for (int i = 0; i < drbs.Count; i++)
                {
                    roleIdBuilder.AppendLine(((ulong)drbs[i].RoleId).ToString(CultureInfo.CurrentCulture));
                    accessBuilder.AppendLine(drbs[i].AccessLevel.ToString(CultureInfo.CurrentCulture));
                }

                EmbedPageBuilder epb = new EmbedPageBuilder(2);
                epb.AddColumn("RoleId", roleIdBuilder.ToString());
                epb.AddColumn("Access Level", accessBuilder.ToString());

                args.Channel.SendMessageAsync(embed: epb.BuildPage(page));
                return;
            }
            else if (args.Parameters.Count < 2)
            {
                HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.NotEnoughParameters);
                return;
            }

            if (!ulong.TryParse(args.Parameters[1], out ulong roleId))
            {
                HelpCommand.ShowHelp(args.Channel, this, string.Format(CultureInfo.CurrentCulture, ResourceExceptions.FailedParseException, args.Parameters[1]));
                return;
            }
            else if (args.Guild.GetRole(roleId) == null)
            {
                HelpCommand.ShowHelp(args.Channel, this, $"Discord Role {args.Parameters[1]} not found!");
                return;
            }

            AccessLevel?access = null;

            if (args.Parameters.Count > 2)
            {
                if (args.Parameters[2].TryParseEnum(out AccessLevel acc))
                {
                    access = acc;
                }
                else
                {
                    HelpCommand.ShowHelp(args.Channel, this, $"Access Level {args.Parameters[2]} not found!");
                    return;
                }

                if (access.HasValue)
                {
                    switch (access.Value)
                    {
                    case AccessLevel.Dev:
                        args.Channel.SendMessageAsync("You do not have enough permissions");
                        return;

                    case AccessLevel.Admin:
                    case AccessLevel.Host:
                        if (args.AccessLevel < AccessLevel.Host)
                        {
                            args.Channel.SendMessageAsync("You do not have enough permissions");
                            return;
                        }
                        break;
                    }
                }
            }


            switch (args.Parameters[0].ToLower(CultureInfo.CurrentCulture))
            {
            case "bind":
                if (!access.HasValue)
                {
                    HelpCommand.ShowHelp(args.Channel, this, $"Access Level {args.Parameters[2]} not found!");
                    return;
                }

                if (CommandHandler.BindPermssion(args.Guild, roleId, access.Value))
                {
                    args.Channel.SendMessageAsync("Binded permission");
                }
                else
                {
                    args.Channel.SendMessageAsync("Already binded or failed to bind");
                }

                break;

            case "unbind":
                if (CommandHandler.UnbindPermission(args.Guild, roleId, access))
                {
                    args.Channel.SendMessageAsync("Unbinded permission");
                }
                else
                {
                    args.Channel.SendMessageAsync("Already unbinded or failed to unbind");
                }
                break;
            }
        }
Esempio n. 17
0
        private void OnSet(List <string> split, CommandEventArg e)
        {
            AccessLevel access = e.AccessLevel;

            if (access < AccessLevel.Moderator ||
                !long.TryParse(split[0], out long ticketId))
            {
                HelpCommand.ShowHelp(e.Channel, this);
                return;
            }

            split.RemoveAt(0);
            Ticket ticket;

            using (DBContext c = new DBContext())
                ticket = c.Ticket.FirstOrDefault(b => b.Id == ticketId);

            if (ticket == null)
            {
                e.Channel.SendMessageAsync($"Could not find ticket {ticketId}");
                return;
            }

            bool changed = false;

            for (int i = 0; i < split.Count; i++)
            {
                switch (split[0].ToLower(CultureInfo.CurrentCulture))
                {
                case "-status":
                case "-ticketstatus":
                    if (Enum.TryParse(split[i + 1], out TicketStatus status_))
                    {
                        ticket.Status = (short)status_;
                        i++;
                        changed = true;
                        break;
                    }
                    else if (short.TryParse(split[i + 1], out short status_s))
                    {
                        ticket.Status = status_s;
                        i++;
                        changed = true;
                        break;
                    }
                    break;

                case "-priority":
                case "-ticketpriority":
                    if (Enum.TryParse(split[i + 1], out TicketPriority prio_))
                    {
                        ticket.Priority = (short)prio_;
                        i++;
                        changed = true;
                        break;
                    }
                    else if (short.TryParse(split[i + 1], out short prio_s))
                    {
                        ticket.Priority = prio_s;
                        i++;
                        changed = true;
                        break;
                    }
                    break;

                case "-tag":
                case "-tickettag":
                    if (Enum.TryParse(split[i + 1], out TicketTag tag_))
                    {
                        ticket.Tag = (short)tag_;
                        i++;
                        changed = true;
                        break;
                    }
                    else if (short.TryParse(split[i + 1], out short tag_s))
                    {
                        ticket.Tag = tag_s;
                        i++;
                        changed = true;
                        break;
                    }
                    break;
                }
            }

            if (!changed)
            {
                e.Channel.SendMessageAsync("You did not change anything");
                return;
            }

            using (DBContext c = new DBContext())
            {
                c.Ticket.Update(ticket);
                c.SaveChanges();
            }

            e.Channel.SendMessageAsync("Updated ticket");
        }
Esempio n. 18
0
        private void OnGet(List <string> split, CommandEventArg e)
        {
            AccessLevel access = e.AccessLevel;

            List <Ticket> tickets = new List <Ticket>();
            StringBuilder searchOptionsbuilder = null;

            //User search
            if (access < AccessLevel.Moderator)
            {
                using (DBContext c = new DBContext())
                    tickets.AddRange(c.Ticket.Where(b => b.DiscordId == (long)e.User.Id &&
                                                    b.DiscordGuildId == (long)e.Guild.Id));
            }
            else //Staff search
            {
                long?          discordId    = null;
                long?          id           = null;
                TicketTag?     tag          = null;
                TicketStatus?  status       = null;
                TicketPriority?priority     = null;
                bool           sortByNewest = false;
                bool           sortByOldest = false;
                searchOptionsbuilder = new StringBuilder();


                for (int i = 2; i < split.Count; i++)
                {
                    if (!split[i].StartsWith('-'))
                    {
                        break;
                    }

                    switch (split[i].ToLower(CultureInfo.CurrentCulture))
                    {
                    case "-ticketid":
                    case "-id":
                        if (!long.TryParse(split[i + 1], out long id_))
                        {
                            HelpCommand.ShowHelp(e.Channel, this);
                            return;
                        }

                        id = id_;
                        searchOptionsbuilder.Append("id ");
                        searchOptionsbuilder.Append(id);
                        searchOptionsbuilder.Append(" | ");
                        i++;
                        break;

                    case "-did":
                    case "-uid":
                    case "-userid":
                    case "-discordid":
                        if (!long.TryParse(split[i + 1], out long dId_))
                        {
                            HelpCommand.ShowHelp(e.Channel, this);
                            return;
                        }

                        discordId = dId_;
                        searchOptionsbuilder.Append("dId ");
                        searchOptionsbuilder.Append(dId_);
                        searchOptionsbuilder.Append(" | ");
                        i++;
                        break;

                    case "-tag":
                    case "-tickettag":
                        if (Enum.TryParse(split[i + 1], out TicketTag tag_))
                        {
                            tag = tag_;
                            searchOptionsbuilder.Append("tag ");
                            searchOptionsbuilder.Append(tag.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }
                        else if (short.TryParse(split[i + 1], out short tag_s))
                        {
                            tag = (TicketTag)tag_s;
                            searchOptionsbuilder.Append("tag ");
                            searchOptionsbuilder.Append(tag.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }

                        HelpCommand.ShowHelp(e.Channel, this);
                        return;

                    case "-status":
                    case "-ticketstatus":
                        if (Enum.TryParse(split[i + 1], out TicketStatus status_))
                        {
                            status = status_;
                            searchOptionsbuilder.Append("status ");
                            searchOptionsbuilder.Append(status.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }
                        else if (short.TryParse(split[i + 1], out short status_s))
                        {
                            status = (TicketStatus)status_s;
                            searchOptionsbuilder.Append("status ");
                            searchOptionsbuilder.Append(status.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }

                        HelpCommand.ShowHelp(e.Channel, this);
                        return;

                    case "-priority":
                    case "-ticketpriority":
                        if (Enum.TryParse(split[i + 1], out TicketPriority prio_))
                        {
                            priority = prio_;
                            searchOptionsbuilder.Append("priority ");
                            searchOptionsbuilder.Append(priority.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }
                        else if (short.TryParse(split[i + 1], out short prio_s))
                        {
                            priority = (TicketPriority)prio_s;
                            searchOptionsbuilder.Append("priority ");
                            searchOptionsbuilder.Append(priority.Value.ToString());
                            searchOptionsbuilder.Append(" | ");
                            i++;
                            break;
                        }

                        HelpCommand.ShowHelp(e.Channel, this);
                        return;

                    case "-sortByNewest":
                    case "-sbn":
                        sortByNewest = true;
                        searchOptionsbuilder.Append("sortByNewest |");
                        break;

                    case "-sortByOldest":
                    case "-sbo":
                        sortByOldest = true;
                        searchOptionsbuilder.Append("sortByOldest |");
                        break;
                    }
                }

                if (split[0].ToLower(CultureInfo.CurrentCulture) == "s")
                {
                    if (!discordId.HasValue && !id.HasValue)
                    {
                        e.Channel.SendMessageAsync("!ticket s page -id(+value, Ticket Id) -uid(+value, Discord User Id)");
                        return;
                    }

                    tickets.Add(TicketHandler.GetTicket(id, discordId));
                }
                else if (split[0].ToLower(CultureInfo.CurrentCulture).Equals("m", StringComparison.CurrentCultureIgnoreCase))
                {
                    tickets.AddRange(TicketHandler.GetTickets(discordId, (long?)e.Guild.Id, id, status,
                                                              priority, tag, sortByNewest,
                                                              sortByOldest));
                }
            }

            if (tickets.Count == 0)
            {
                e.Channel.SendMessageAsync("Could not find any tickets");
                return;
            }

            int page = 0;

            if (int.TryParse(split[1], out int page_))
            {
                page = page_;
            }

            int pageStart = GetPageIndexStart(page);

            DiscordEmbedBuilder builder = new DiscordEmbedBuilder()
            {
                Title = "Ticketlist",
            };

            if (searchOptionsbuilder != null)
            {
                builder.Description = "Search options: " + searchOptionsbuilder.ToString();
            }

            if (tickets.Count > 1)
            {
                StringBuilder idBuilder       = new StringBuilder();
                StringBuilder statusBuilder   = new StringBuilder();
                StringBuilder priorityBuilder = new StringBuilder();

                for (int i = pageStart; i < pageStart + 10; i++)
                {
                    if (i >= tickets.Count)
                    {
                        break;
                    }

                    idBuilder.AppendLine(tickets[i].Id.ToString(CultureInfo.CurrentCulture));
                    statusBuilder.AppendLine(((TicketStatus)tickets[i].Status).ToString());
                    priorityBuilder.AppendLine(((TicketPriority)tickets[i].Priority).ToString());
                }

                builder.AddField("ID", idBuilder.ToString(), true);
                builder.AddField("Status", statusBuilder.ToString(), true);
                builder.AddField("Priority", priorityBuilder.ToString(), true);
            }
            else
            {
                var duser = e.User;

                builder.Timestamp = tickets[0].Timestamp;

                builder.AddField("Ticket Id", tickets[0].Id.ToString(CultureInfo.CurrentCulture), true);
                builder.AddField("Discord User", $"{duser.Username}#{duser.Discriminator} ({duser.Mention})", true);
                builder.AddField("Message", tickets[0].Message);

                builder.AddField("Settings", $"Priority: {(TicketPriority)tickets[0].Priority}\nTag: {(TicketTag)tickets[0].Tag}\nStatus: {(TicketStatus)tickets[0].Status}");
            }

            var embed = builder.Build();

            e.Channel.SendMessageAsync(embed: embed);
        }
Esempio n. 19
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            using WebClient wc = new WebClient();

            Uri downloadUri;

            try
            {
                downloadUri = new Uri(args.Parameters[2].TrimStart('<').TrimEnd('>'));
            }
            catch (UriFormatException)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Invalid link");
                return;
            }

            string json = wc.DownloadString(downloadUri);

            if (string.IsNullOrEmpty(json))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Embed json not found");
                return;
            }

            EmbedJson ej = Newtonsoft.Json.JsonConvert.DeserializeObject <EmbedJson>(json);

            if (ej == null ||
                (string.IsNullOrEmpty(ej.Content) && ej.Embed == null))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse embed json");
                return;
            }

            DiscordEmbed embed = ej.BuildEmbed();

            if (embed == null)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse embed json");
                return;
            }

            try
            {
                switch (args.Parameters[0].ToLower(CultureInfo.CurrentCulture))
                {
                case "webhook":
                {
                    if (args.Parameters.Count < 4)
                    {
                        HelpCommand.ShowHelp(args.Channel, this);
                        return;
                    }

                    ulong chId = DiscordHandler.ExtractMentionId(args.Parameters[1], true);

                    if (chId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "channel id");
                        return;
                    }

                    string avatarUri = null;

                    if (args.Parameters.Count > 4)
                    {
                        avatarUri = args.Parameters[4].TrimStart('<').TrimEnd('>');
                    }

                    SendEmbed(client, this, args, embed, ej.Content, chId, args.Guild, true, args.Parameters[3], avatarUri);
                }
                break;

                default:
                case "create":
                {
                    ulong chId = DiscordHandler.ExtractMentionId(args.Parameters[1], true);

                    if (chId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "channel id");
                        return;
                    }

                    SendEmbed(client, this, args, embed, ej.Content, chId, args.Guild, false, null, null);
                }
                break;

                case "edit":
                    DiscordMessageLink msgLink = DiscordHandler.ExtractMessageLink(args.Parameters[1]);

                    if (msgLink.DiscordGuildId == 0 ||
                        msgLink.DiscordChannelId == 0 ||
                        msgLink.DiscordMessageId == 0)
                    {
                        HelpCommand.ShowHelp(args.Channel, this, ResourceExceptions.CannotParseDiscordId + "message uri");
                        return;
                    }

                    EditEmbed(client, args, embed, ej.Content, msgLink.DiscordChannelId, msgLink.DiscordMessageId, args.Guild);
                    break;
                }
            }
            catch (ReadableCmdException rce)
            {
                HelpCommand.ShowHelp(args.Channel, this, rce.Message);
            }
        }
Esempio n. 20
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            if (args.Parameters.Count == 0)
            {
                args.Channel.SendMessageAsync($"{args.User.Mention} your access level is {Program.DiscordHandler.CommandHandler.GetAccessLevel(args.User, args.Guild)}");
                return;
            }

            if (!ulong.TryParse(args.Parameters[0], out ulong uid))
            {
                HelpCommand.ShowHelp(args.Channel, this, string.Format(ResourceExceptions.FailedParseException, args.Parameters[0]));
                return;
            }

            var         duser  = client.GetUserAsync(uid).Result;
            AccessLevel access = Program.DiscordHandler.CommandHandler.GetAccessLevel(duser, args.Guild);

            if (args.Parameters.Count == 1)
            {
                args.Channel.SendMessageAsync($"Access level of user {duser.Username} is {access}");
                return;
            }
            else if (args.AccessLevel < AccessLevel.Admin)
            {
                HelpCommand.ShowHelp(args.Channel, this);
                return;
            }


            AccessLevel newAccess;

            if (args.Parameters[1].TryParseEnum(out AccessLevel acc))
            {
                newAccess = acc;
            }
            else
            {
                HelpCommand.ShowHelp(args.Channel, this, string.Format(ResourceExceptions.FailedParseException, "Access Level"));
                return;
            }

            switch (newAccess)
            {
            case AccessLevel.Dev:
                args.Channel.SendMessageAsync("Dev permission can only be set via db");
                return;

            case AccessLevel.Host:
                if (args.AccessLevel < AccessLevel.Host)
                {
                    args.Channel.SendMessageAsync("You need to be atleast Host to set someone to host!");
                    return;
                }
                break;

            case AccessLevel.Admin:
                if (args.AccessLevel < AccessLevel.Host)
                {
                    args.Channel.SendMessageAsync("Only the host can add more admins!");
                    return;
                }
                break;

            default:
                break;
            }

            CommandHandler.SetAccessLevel(uid, args.Guild.Id, newAccess);

            args.Channel.SendMessageAsync($"Set {uid} from {access} to {newAccess}");
        }
Esempio n. 21
0
        public void Invoke(DiscordHandler client, CommandHandler handler, CommandEventArg args)
        {
            using DBContext c = new DBContext();
            DiscordGuildConfig dgc = args.Config;

            if (dgc == null || dgc.MutedRoleId == 0)
            {
                args.Channel.SendMessageAsync("You need to setup your muted role first in the config");
                return;
            }

            args.Parameters[0] = args.Parameters[0].Trim('<', '@', '!', '>');

            DiscordMember member = null;

            if (ulong.TryParse(args.Parameters[0], out ulong uid))
            {
                try
                {
                    member = args.Guild.GetMemberAsync(uid).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (DSharpPlus.Exceptions.NotFoundException)
                {
                }
            }

            if (member == null)
            {
                HelpCommand.ShowHelp(args.Channel, this, "Could not find member or parse id: " + args.Parameters[0]);
                return;
            }

            if (!long.TryParse(args.Parameters[1], out long durationM))
            {
                HelpCommand.ShowHelp(args.Channel, this, "Failed to parse duration: " + args.Parameters[1]);
                return;
            }

            var  drole = args.Guild.GetRole((ulong)dgc.MutedRoleId);
            Mute m;

            if (member.Roles.Contains(drole))
            {
                //Update duration if diffrent
                m = c.Mute.FirstOrDefault(mu => mu.DiscordGuildId == (long)args.Guild.Id && mu.DiscordUserId == (long)uid && !mu.Unmuted);

                if (m != null)
                {
                    if (m.DurationM == durationM)
                    {
                        args.Channel.SendMessageAsync("Not updated, time is the same");
                        return;
                    }

                    m.DurationM = durationM;
                    c.Mute.Update(m);
                    c.SaveChanges();

                    args.Channel.SendMessageAsync("Updated duration");

                    return;
                }
                else
                {
                    member.RevokeRoleAsync(drole, "invalid mute, revoking").ConfigureAwait(false).GetAwaiter().GetResult();
                }
            }

            if (args.Parameters.Count < 3)
            {
                HelpCommand.ShowHelp(args.Channel, this);
                return;
            }

            StringBuilder reasonBuilder = new StringBuilder(args.Parameters[2]);

            for (int i = 3; i < args.Parameters.Count; i++)
            {
                reasonBuilder.Append(" " + args.Parameters[i]);
            }

            m = new Mute((long)uid, (long)args.Guild.Id, DateTime.UtcNow, durationM, args.ParameterString.Remove(0, args.Parameters[0].Length + args.Parameters[1].Length + 2));

            member.GrantRoleAsync(drole, $"muted by {args.User.Username} for {durationM} minutes, reason: {m.Reason}");

            c.Mute.Add(m);
            c.SaveChanges();

            args.Channel.SendMessageAsync($"Muted {member.Username} for {durationM} minutes, reason: {m.Reason}");
        }