Beispiel #1
0
        public async Task ConfigRoleQuickAsync(EventContext e)
        {
            using (var context = new MikiContext())
            {
                string roleName = e.Arguments.ToString().Split('"')[1];

                IDiscordRole role = null;
                if (ulong.TryParse(roleName, out ulong s))
                {
                    role = await e.Guild.GetRoleAsync(s);
                }
                else
                {
                    role = (await GetRolesByName(e.Guild, roleName)).FirstOrDefault();
                }

                LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong());

                MSLResponse arguments = new MMLParser(e.Arguments.ToString().Substring(roleName.Length + 3))
                                        .Parse();

                if (role.Name.Length > 20)
                {
                    await e.ErrorEmbed("Please keep role names below 20 letters.")
                    .ToEmbed().SendToChannel(e.Channel);

                    return;
                }

                if (newRole == null)
                {
                    newRole = context.LevelRoles.Add(new LevelRole()
                    {
                        GuildId = (e.Guild.Id.ToDbLong()),
                        RoleId  = (role.Id.ToDbLong()),
                    }).Entity;
                }

                if (arguments.HasKey("automatic"))
                {
                    newRole.Automatic = arguments.GetBool("automatic");
                }

                if (arguments.HasKey("optable"))
                {
                    newRole.Optable = arguments.GetBool("optable");
                }

                if (arguments.HasKey("level-required"))
                {
                    newRole.RequiredLevel = arguments.GetInt("level-required");
                }

                if (arguments.HasKey("role-required"))
                {
                    long id = 0;
                    if (arguments.TryGet("role-required", out long l))
                    {
                        id = l;
                    }
                    else
                    {
                        var r = (await e.Guild.GetRolesAsync())
                                .Where(x => x.Name.ToLower() == arguments.GetString("role-required").ToLower())
                                .FirstOrDefault();

                        if (r != null)
                        {
                            id = r.Id.ToDbLong();
                        }
                    }

                    if (id != 0)
                    {
                        newRole.RequiredRole = id;
                    }
                }

                await context.SaveChangesAsync();

                Utils.Embed.SetTitle("⚙ Role Config")
                .SetColor(102, 117, 127)
                .SetDescription($"Updated {role.Name}!")
                .ToEmbed().QueueToChannel(e.Channel);
            }
        }
Beispiel #2
0
        /*public async Task ConfigRoleInteractiveAsync(EventContext e)
         *      {
         *              using (var context = new MikiContext())
         *              {
         *                      EmbedBuilder sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                              .SetDescription("Type out the role name you want to config")
         *                              .SetColor(138, 182, 239);
         *                      IDiscordMessage sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *                      IDiscordMessage msg = null;
         *
         *                      while (true)
         *                      {
         *                              msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *                              if (msg.Content.Length < 20)
         *                              {
         *                                      break;
         *                              }
         *                              else
         *                              {
         *                                      await sourceMessage.EditAsync(new EditMessageArgs
         *                                      {
         *                                              embed = e.ErrorEmbed("That role name is way too long! Try again.")
         *                                                      .ToEmbed()
         *                                      });
         *                              }
         *                      }
         *
         *                      string roleName = msg.Content;
         *
         *                      List<IDiscordRole> rolesFound = await GetRolesByName(e.GetGuild(), roleName.ToLower());
         *                      IDiscordRole role = null;
         *
         *                      if(rolesFound.Count == 0)
         *                      {
         *                              // Hey, I couldn't find this role, Can I make a new one?
         *                              await sourceMessage.EditAsync(new EditMessageArgs
         *                              {
         *                                      embed = e.ErrorEmbed($"There's no role that is named `{roleName}`, Shall I create it? Y/N").ToEmbed()
         *                              });
         *
         *                              msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *                              if (msg.Content.ToLower()[0] != 'y')
         *                              {
         *                                      throw new RoleNullException();
         *                              }
         *
         *                              role = await e.GetGuild().CreateRoleAsync(new CreateRoleArgs
         *                              {
         *                                      Name = roleName,
         *                              });
         *                      }
         *                      else if (rolesFound.Count > 1)
         *                      {
         *                              string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}"));
         *
         *                              if (roleIds.Length > 1024)
         *                              {
         *                                      roleIds = roleIds.Substring(0, 1024);
         *                              }
         *
         *                              sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                                              .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID")
         *                                              .AddInlineField("Roles - Ids", roleIds)
         *                                              .SetColor(138, 182, 239);
         *
         *                              sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *                              while(true)
         *                              {
         *                                      msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *                                      if (ulong.TryParse(msg.Content, out ulong id))
         *                                      {
         *                                              role = rolesFound.Where(x => x.Id == id)
         *                                                      .FirstOrDefault();
         *
         *                                              if (role != null)
         *                                              {
         *                                                      break;
         *                                              }
         *                                              else
         *                                              {
         *                                                      await sourceMessage.EditAsync(new EditMessageArgs {
         *                                                              embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!")
         *                                                              .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed()
         *                                                      });
         *                                              }
         *                                      }
         *                                      else
         *                                      {
         *                                              await sourceMessage.EditAsync(new EditMessageArgs
         *                                              {
         *                                                      embed = e.ErrorEmbed("I couldn't find that role. Try again!")
         *                                                      .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed()
         *                                              });
         *                                      }
         *                              }
         *                      }
         *                      else
         *                      {
         *                              role = rolesFound.FirstOrDefault();
         *                      }
         *
         *                      LevelRole newRole = await context.LevelRoles.FindAsync(e.GetGuild().Id.ToDbLong(), role.Id.ToDbLong());
         *                      if(newRole == null)
         *                      {
         *                              newRole = (await context.LevelRoles.AddAsync(new LevelRole()
         *                              {
         *                                      RoleId = role.Id.ToDbLong(),
         *                                      GuildId = e.GetGuild().Id.ToDbLong()
         *                              })).Entity;
         *                      }
         *
         *                      sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                              .SetDescription("Is there a role that is needed to get this role? Type out the role name, or `-` to skip")
         *                              .SetColor(138, 182, 239);
         *
         *                      sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *
         *                      while (true)
         *                      {
         *                              msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *                              rolesFound = (await GetRolesByName(e.GetGuild(), msg.Content.ToLower()));
         *                              IDiscordRole parentRole = null;
         *
         *                              if (rolesFound.Count > 1)
         *                              {
         *                                      string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}"));
         *
         *                                      if (roleIds.Length > 1024)
         *                                      {
         *                                              roleIds = roleIds.Substring(0, 1024);
         *                                      }
         *
         *                                      sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                                                      .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID")
         *                                                      .AddInlineField("Roles - Ids", roleIds)
         *                                                      .SetColor(138, 182, 239);
         *
         *                                      sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *                                      while (true)
         *                                      {
         *                                              msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *                                              if (ulong.TryParse(msg.Content, out ulong id))
         *                                              {
         *                                                      parentRole = rolesFound.Where(x => x.Id == id)
         *                                                              .FirstOrDefault();
         *
         *                                                      if (parentRole != null)
         *                                                      {
         *                                                              break;
         *                                                      }
         *                                                      else
         *                                                      {
         *                                                              await sourceMessage.EditAsync(new EditMessageArgs {
         *                                                                      embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!")
         *                                                                      .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed()
         *                                                              }) ;
         *                                                      }
         *                                              }
         *                                              else
         *                                              {
         *                                                      await sourceMessage.EditAsync(new EditMessageArgs
         *                                                      {
         *                                                              embed = e.ErrorEmbed("I couldn't find that role. Try again!")
         *                                                              .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed()
         *                                                      });
         *                                              }
         *                                      }
         *                              }
         *                              else
         *                              {
         *                                      parentRole = rolesFound.FirstOrDefault();
         *                              }
         *
         *                              if (parentRole != null || msg.Content == "-")
         *                              {
         *                                      newRole.RequiredRole = (long?)parentRole?.Id ?? 0;
         *                                      break;
         *                              }
         *
         *                              await sourceMessage.EditAsync(new EditMessageArgs
         *                              {
         *                                      embed = e.ErrorEmbed("I couldn't find that role. Try again!").ToEmbed()
         *                              });
         *                      }
         *
         *                      sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                              .SetDescription($"Is there a level requirement? type a number, if there is no requirement type 0")
         *                              .SetColor(138, 182, 239);
         *
         *                      sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *
         *                      while (true)
         *                      {
         *                              msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *                              if (int.TryParse(msg.Content, out int r))
         *                              {
         *                                      if (r >= 0)
         *                                      {
         *                                              newRole.RequiredLevel = r;
         *                                              break;
         *                                      }
         *                                      else
         *                                      {
         *                                              await sourceMessage.EditAsync(new EditMessageArgs
         *                                              {
         *                                                      embed = sourceEmbed.SetDescription($"Please pick a number above 0. Try again")
         *                                                              .ToEmbed()
         *                                              });
         *                                      }
         *                              }
         *                              else
         *                              {
         *                                      await sourceMessage.EditAsync(new EditMessageArgs
         *                                      {
         *                                              embed = sourceEmbed.SetDescription($"Are you sure `{msg.Content}` is a number? Try again").ToEmbed()
         *                                      });
         *                              }
         *                      }
         *
         *                      sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                              .SetDescription($"Should I give them when the user level ups? type `yes`, otherwise it will be considered as no")
         *                              .SetColor(138, 182, 239);
         *
         *                      sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *
         *                      msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *                      if (msg == null)
         *                      {
         *                              return;
         *                      }
         *
         *                      newRole.Automatic = msg.Content.ToLower()[0] == 'y';
         *
         *                      sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                              .SetDescription($"Should users be able to opt in? type `yes`, otherwise it will be considered as no")
         *                              .SetColor(138, 182, 239);
         *
         *                      sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *
         *                      msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *                      newRole.Optable = msg.Content.ToLower()[0] == 'y';
         *
         *                      if (newRole.Optable)
         *                      {
         *                              sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode")
         *                                      .SetDescription($"Do you want the user to pay mekos for the role? Enter any amount. Enter 0 to keep the role free.")
         *                                      .SetColor(138, 182, 239);
         *
         *                              sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.GetChannel());
         *
         *                              while (true)
         *                              {
         *                                      msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession());
         *
         *              if (msg == null)
         *                                      {
         *                                              return;
         *                                      }
         *
         *                                      if (int.TryParse(msg.Content, out int r))
         *                                      {
         *                                              if (r >= 0)
         *                                              {
         *                                                      newRole.Price = r;
         *                                                      break;
         *                                              }
         *                                              else
         *                                              {
         *                                                      await sourceMessage.EditAsync(new EditMessageArgs
         *                                                      {
         *                                                              embed = e.ErrorEmbed($"Please pick a number above 0. Try again").ToEmbed()
         *                                                      });
         *                                              }
         *                                      }
         *                                      else
         *                                      {
         *                                              await sourceMessage.EditAsync(new EditMessageArgs
         *                                              {
         *                                                      embed = e.ErrorEmbed($"Not quite sure if this is a number 🤔").ToEmbed()
         *                                              });
         *                                      }
         *                              }
         *                      }
         *
         *                      await context.SaveChangesAsync();
         *                      Utils.Embed.SetTitle("⚙ Role Config")
         *                              .SetColor(102, 117, 127)
         *                              .SetDescription($"Updated {role.Name}!")
         *                              .ToEmbed().QueueToChannelAsync(e.GetChannel());
         *              }
         *      }*/
        public async Task ConfigRoleQuickAsync(IContext e)
        {
            var context = e.GetService <MikiDbContext>();

            if (!e.GetArgumentPack().Take(out string roleName))
            {
                await e.ErrorEmbed("Expected a role name")
                .ToEmbed().QueueAsync(e, e.GetChannel());

                return;
            }

            IDiscordRole role = null;

            if (ulong.TryParse(roleName, out ulong s))
            {
                role = await e.GetGuild().GetRoleAsync(s);
            }
            else
            {
                role = (await GetRolesByNameAsync(e.GetGuild(), roleName)).FirstOrDefault();
            }

            var newRole = await context.LevelRoles.FindAsync((long)e.GetGuild().Id, (long)role.Id);

            var arguments = new MMLParser(e.GetArgumentPack().Pack.TakeAll()).Parse();

            if (role.Name.Length > 20)
            {
                await e.ErrorEmbed("Please keep role names below 20 letters.")
                .ToEmbed()
                .QueueAsync(e, e.GetChannel());

                return;
            }

            if (newRole == null)
            {
                newRole = context.LevelRoles.Add(new LevelRole()
                {
                    GuildId = (e.GetGuild().Id.ToDbLong()),
                    RoleId  = (role.Id.ToDbLong()),
                }).Entity;
            }

            if (arguments.HasKey("automatic"))
            {
                newRole.Automatic = arguments.GetBool("automatic");
            }

            if (arguments.HasKey("optable"))
            {
                newRole.Optable = arguments.GetBool("optable");
            }

            if (arguments.HasKey("level-required"))
            {
                newRole.RequiredLevel = arguments.GetInt("level-required");
            }

            if (arguments.HasKey("price"))
            {
                newRole.Price = arguments.GetInt("price");
                if (newRole.Price < 0)
                {
                    throw new ArgumentLessThanZeroException();
                }
            }

            if (arguments.HasKey("role-required"))
            {
                long id = 0;
                if (arguments.TryGet("role-required", out long l))
                {
                    id = l;
                }
                else
                {
                    var r = (await e.GetGuild().GetRolesAsync())
                            .Where(x => x.Name.ToLower() == arguments.GetString("role-required").ToLower())
                            .FirstOrDefault();

                    if (r != null)
                    {
                        id = r.Id.ToDbLong();
                    }
                }

                if (id != 0)
                {
                    newRole.RequiredRole = id;
                }
            }

            await context.SaveChangesAsync();

            await new EmbedBuilder()
            .SetTitle("⚙ Role Config")
            .SetColor(102, 117, 127)
            .SetDescription($"Updated {role.Name}!")
            .ToEmbed().QueueAsync(e, e.GetChannel());
        }