public static async Task TryAuthFromContext(SocketCommandContext context, string code)
        {
            bool authed = await GranularPermissionsStorage.GetAuthStatusFor(context.Guild.Id);

            if (authed)
            {
                await context.Message.AddReactionAsync(new Emoji("❓"));

                await context.Channel.SendMessageAsync("This server is already authorized.");
            }
            else
            {
                bool codeConsumed = await GranularPermissionsStorage.TryConsumeAuthCode(context.Guild.Id, code);

                if (codeConsumed)
                {
                    await context.Message.AddReactionAsync(new Emoji("👌"));
                }
                else
                {
                    await context.Message.AddReactionAsync(new Emoji("👎"));

                    await context.Channel.SendMessageAsync("That code was already used or didn't exist.");
                }
            }
        }
        public async Task UnselfassignMe([Summary("The code")] string code)
        {
            Dictionary <string, ulong> sassables = await GranularPermissionsStorage.GetSelfassignable(this.Context.Guild.Id);

            if (sassables != null && sassables.ContainsKey(code))
            {
                SocketGuildUser sgu = this.Context.User as SocketGuildUser;
                if (sgu != null)
                {
                    IEnumerable <SocketRole> rolesToRemove = sgu.Guild.Roles.Where(x => x.Id == sassables[code]);
                    await sgu.RemoveRolesAsync(rolesToRemove);

                    await this.Context.Message.AddReactionAsync(new Emoji("👌"));

                    return;
                }
                else
                {
                    await this.Context.Message.AddReactionAsync(new Emoji("👎"));
                }
            }
            else
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👎"));
            }
        }
        public async Task Channelmod(
            [Summary("The user to cmod")][Remainder] SocketGuildUser user)
        {
            CollectionReference channelCollection = GranularPermissionsStorage.Db.Document(Convert.ToString(this.Context.Guild.Id)).Collection("channels");
            DocumentReference   channelDoc        = channelCollection.Document(Convert.ToString(this.Context.Channel.Id));

            Dictionary <string, object> update = new Dictionary <string, object>();

            List <ulong> mods = await GranularPermissionsStorage.GetChannelmodsFor(this.Context.Guild.Id, this.Context.Channel);

            if (!mods.Contains(user.Id))
            {
                mods.Add(user.Id);
                update["mods"] = mods;

                await channelDoc.SetAsync(update, SetOptions.MergeAll);

                if (this.Context.Channel is SocketGuildChannel sgc)
                {
                    await sgc.AddPermissionOverwriteAsync(user,
                                                          new OverwritePermissions(readMessages : PermValue.Allow, sendMessages : PermValue.Allow, manageMessages : PermValue.Allow));
                }
                await this.Context.Message.AddReactionAsync(new Emoji("👌"));
            }
            else
            {
                mods.RemoveAll(x => x == user.Id);
                update["mods"] = mods;

                await channelDoc.SetAsync(update, SetOptions.MergeAll);

                await this.Context.Channel.SendMessageAsync("Removed a channelmod. (Their Discord permissions were not changed.)");
            }
        }
        public async Task AddKey([Summary("The authorization code")] string code)
        {
            bool result = await this.Context.IsOwner(); if (!result)
            {
                return;
            }

            await GranularPermissionsStorage.AddAuthCode(code);

            await this.Context.Message.AddReactionAsync(new Emoji("👌"));
        }
        public async Task DefaultRole([Summary("The role to make default")] SocketRole role)
        {
            bool success = await GranularPermissionsStorage.SetDefaultRole(this.Context.Guild.Id, role);

            if (success)
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👌"));
            }
            else
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👎"));
            }
        }
        public async Task MarkSelfassignable([Summary("The code to add the role as")] string code, [Summary("The role itself")] SocketRole role = null)
        {
            bool success = await GranularPermissionsStorage.SetSelfassignable(this.Context.Guild.Id, code, role);

            if (success)
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👌"));
            }
            else
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👎"));
            }
        }
        public async Task RemoveFromChannel(
            [Summary("The user to remove")] SocketGuildUser user,
            [Summary("The channel to add to")] SocketGuildChannel channel)
        {
            List <ulong> mods = await GranularPermissionsStorage.GetChannelmodsFor(this.Context.Guild.Id, channel as ISocketMessageChannel);

            if (!mods.Contains(this.Context.User.Id))
            {
                await this.Context.Channel.SendMessageAsync("You're not a channelmod of this channel: **" + channel.Name + "**");

                return;
            }

            await channel.AddPermissionOverwriteAsync(user,
                                                      new OverwritePermissions(readMessages : PermValue.Deny, sendMessages : PermValue.Deny));

            await this.Context.Message.AddReactionAsync(new Emoji("👌"));
        }
        public async Task CheckAuth()
        {
            bool result = await this.Context.IsOwner(); if (!result)
            {
                return;
            }

            bool authed = await GranularPermissionsStorage.GetAuthStatusFor(this.Context.Guild.Id);

            if (authed)
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👌"));
            }
            else
            {
                await this.Context.Message.AddReactionAsync(new Emoji("👎"));
            }
        }
 public async Task Authorize([Summary("The authorization code")] string code)
 {
     await GranularPermissionsStorage.TryAuthFromContext(this.Context, code);
 }