public async Task CreateRoleAsync(CachedRole role)
        {
            await using (var db = new DataContext())
            {
                AssignableRole roleMatch = _cache.Retrieve <AssignableRole>(x =>
                                                                            x.GuildId == Context.Guild.Id.RawValue && x.RoleId == role.Id.RawValue)
                                           ?? db.Roles.FirstOrDefault(x => x.GuildId == Context.Guild.Id.RawValue && x.RoleId == role.Id.RawValue);
                if (roleMatch == null)
                {
                    await ReplyAsync("I cannot give you this role, mere mortal.");

                    return;
                }
                else
                {
                    _cache.Put(roleMatch, TimeSpan.FromHours(1));
                }
            }

            if (!Context.Guild.CurrentMember.Permissions.ManageRoles)
            {
                await ReplyAsync("I can't give roles in this server. PLS GIV ADMIN");

                return;
            }

            await Context.Guild.GrantRoleAsync(Context.User.Id, role.Id);

            await ReplyAsync("You hath been granted this role, continue forth youngling.");
        }
示例#2
0
        private async Task AddRoles(ulong guildId, IEnumerable <IRole> roles, bool checkPermissions = true)
        {
            if (checkPermissions)
            {
                var bot = await roles.First().Guild.GetCurrentUserAsync();

                var invalid = roles.Where(x => !x.CanUserAssign(bot)).ToList();
                if (invalid.Any())
                {
                    throw new CommandException($"The bot doesn't have permission to assign role(s) {invalid.WordJoinQuoted()} to users. Please make sure the role is below the bot's highest role in the server's role list.");
                }
            }

            await _settings.Modify(guildId, (RolesSettings s) =>
            {
                foreach (var role in roles)
                {
                    var newRole    = new AssignableRole();
                    newRole.RoleId = role.Id;

                    if (s.AssignableRoles.Any(x => x.RoleId == role.Id))
                    {
                        throw new CommandException($"Role `{role.Name}` is already self-assignable.");
                    }

                    if (s.AssignableRoles.Any(x => x.SecondaryId == role.Id))
                    {
                        throw new CommandException($"Role `{role.Name}` is already set as a secondary to another role.");
                    }

                    s.AssignableRoles.Add(newRole);
                }
            });
        }
        public async Task LeaveRoleAsync(CachedRole role)
        {
            await using (var db = new DataContext())
            {
                AssignableRole roleMatch = _cache.Retrieve <AssignableRole>(x =>
                                                                            x.GuildId == Context.Guild.Id.RawValue && x.RoleId == role.Id.RawValue)
                                           ?? db.Roles.FirstOrDefault(x => x.GuildId == Context.Guild.Id.RawValue && x.RoleId == role.Id.RawValue);

                if (roleMatch == null)
                {
                    await ReplyAsync("I cannot remove this role, as it does not exist <a:slinky:743336270972846091>");

                    return;
                }
                else
                {
                    _cache.Put(roleMatch, TimeSpan.FromHours(1));
                }
            }

            if (!Context.Guild.CurrentMember.Permissions.ManageRoles)
            {
                await ReplyAsync("I can't manage roles in this server. PLS GIV ADMIN");

                return;
            }

            await Context.Guild.RevokeRoleAsync(Context.User.Id, role.Id);

            await ReplyAsync("You hath been abolished from this role, be free my child.");
        }
示例#4
0
        public async Task Add([Remainder] string role)
        {
            var roleResult = SearchRole(role);

            if (roleResult == null)
            {
                await ReplyAsync("Role does not exist!").ConfigureAwait(false);
            }
            else
            {
                if (RoleRequestService.GetRole(roleResult.Id) == null)
                {
                    if (Context.Guild.OwnerId != Context.User.Id)
                    {
                        // find highest role of the user
                        var user = await Context.Guild.GetUserAsync(Context.User.Id).ConfigureAwait(false);

                        var highestRolePosition = 0;
                        user.RoleIds
                        .Select(roleId => Context.Guild.Roles.First(r => r.Id == roleId))
                        .ToList()
                        .ForEach(role => highestRolePosition = role.Position > highestRolePosition ? role.Position : highestRolePosition);
                        if (highestRolePosition <= roleResult.Position)
                        {
                            await ReplyAsync("You cannot add a role that is a level greater than or equal to your highest role!").ConfigureAwait(false);

                            return;
                        }
                    }
                    var warnings = "";
                    warnings += roleResult.Permissions.ManageRoles ? "\n**Warning:** This role has permissions to assign roles to other users!" : "";
                    warnings += roleResult.Permissions.Administrator ? "\n**Warning:** This role has administrator permissions!" : "";
                    var newRole = new AssignableRole()
                    {
                        RoleId   = roleResult.Id,
                        ServerId = Context.Guild.Id,
                        RoleName = roleResult.Name
                    };
                    RoleRequestService.AddRole(newRole);
                    await ReplyAsync($"{roleResult} is now self-assignable! {warnings}").ConfigureAwait(false);
                }
                else
                {
                    await ReplyAsync("Role is already assignable!").ConfigureAwait(false);
                }
            }
        }
示例#5
0
        public async Task AddCommand(CommandContext ctx, DiscordRole role, [RemainingText] string name)
        {
            using (var db = new RolesContext())
            {
                var dbRole = new AssignableRole
                {
                    Name     = name,
                    RoleId   = role.Id.ToString(),
                    ServerId = ctx.Guild.Id.ToString()
                };

                db.AssignableRoles.Add(dbRole);
                await db.SaveChangesAsync();

                await ctx.RespondAsync("Role added successfully.");
            }
        }
示例#6
0
        public async Task RegisterRole(IRole role, IRole category, bool requireAdult = false)
        {
            var arole = new AssignableRole()
            {
                GuildId       = Context.Guild.Id,
                Position      = role.Position,
                Require18Plus = requireAdult,
                RoleCategory  = category.Id,
                RoleId        = role.Id
            };

            await _dbContext.AssignableRoles.AddAsync(arole);

            await _dbContext.SaveChangesAsync();

            await Context.AddConfirmation();
        }
        public async Task AddRoleAsync(IRole role, IEmoji emojiDefinition)
        {
            if (role.Position > Context.Guild.CurrentMember.Hierarchy || role.Position > ((CachedMember)Context.User).Hierarchy)
            {
                await ReplyAsync("This role is TOO POWERFUL you fool.");

                return;
            }

            if (!Context.Guild.CurrentMember.Permissions.ManageRoles)
            {
                await ReplyAsync("I can't give roles in this server. PLS GIV ME ADMIN");

                return;
            }

            AssignableRole newRole;

            if (emojiDefinition is LocalCustomEmoji lc)
            {
                newRole = new AssignableRole
                {
                    GuildId   = Context.Guild.Id,
                    RoleId    = role.Id,
                    EmojiName = emojiDefinition.Name,
                    Animated  = emojiDefinition.MessageFormat.StartsWith("<a"),
                    EmojiId   = ulong.Parse(emojiDefinition.ReactionFormat.Split(":")[1])
                };
            }
            else if (emojiDefinition is LocalEmoji le)
            {
                newRole = new AssignableRole
                {
                    GuildId   = Context.Guild.Id,
                    RoleId    = role.Id,
                    EmojiName = emojiDefinition.Name,
                    Animated  = false,
                    EmojiId   = null
                };
            }
            else
            {
                return;
            }

            await using (var db = new DataContext())
            {
                var roleMatch = db.Roles.FirstOrDefault(x => x.GuildId == newRole.GuildId && x.RoleId == newRole.RoleId);
                if (roleMatch != null)
                {
                    await ReplyAsync("That role can already be given to users you buffoon.");

                    return;
                }
                var emojiRoleMatch = db.Roles.FirstOrDefault(x => x.GuildId == newRole.GuildId && x.EmojiName == newRole.EmojiName);
                if (emojiRoleMatch != null)
                {
                    await ReplyAsync("That emoji is already associated with a role, imbecile.");

                    return;
                }

                await db.Roles.AddAsync(newRole);

                _cache.Put(newRole, TimeSpan.FromHours(1));

                if (newRole.IsCustomEmoji())
                {
                    _cache.RemoveIgnore <AssignableRole>(newRole.GuildId, newRole.EmojiId);
                }
                else
                {
                    _cache.RemoveIgnore <AssignableRole>(newRole.GuildId, newRole.EmojiName);
                }

                await db.SaveChangesAsync();
                await ReplyAsync(
                    $"Your citizens may now be granted this role, use `{Context.Prefix}getRole {role.Name}` to be granted it.");
            }
        }
示例#8
0
 public void RemoveRole(AssignableRole entity)
 {
     DbContext.Remove(entity);
 }
示例#9
0
 public AssignableRole AddRole(AssignableRole entity)
 {
     entity = DbContext.AssignableRoles.Add(entity).Entity;
     DbContext.SaveChanges();
     return(entity);
 }