Example #1
0
        public static ChannelData GetCachedChannelData(this DiscordChannel channel)
        {
            if (!File.Exists($"./cache/lock/{channel.Id}.ibcd"))
            {
                return(ChannelData.CreateNew());
            }

            StreamReader reader = new($"./cache/lock/{channel.Id}.ibcd");

            return(JsonConvert.DeserializeObject <ChannelData>(reader.ReadToEnd()));
        }
Example #2
0
        public static void SerializeLockingOverwrites(this DiscordChannel channel, ChannelData data)
        {
            if (!Directory.Exists($"./cache/lock"))
            {
                Directory.CreateDirectory($"./cache/lock");
            }

            if (!File.Exists($"./cache/lock/{channel.Id}.ibcd"))
            {
                File.Create($"./cache/lock/{channel.Id}.ibcd").Close();
            }

            StreamWriter writer = new($"./cache/lock/{channel.Id}.ibcd");

            writer.Write(JsonConvert.SerializeObject(data));

            writer.Close();
        }
Example #3
0
        private async Task UnlockCommand(CommandContext ctx, DiscordChannel channel, String reason = "usedefault", Boolean silent = false)
        {
            if (!ctx.Member.HasPermission("insanitybot.moderation.unlock"))
            {
                await ctx.Channel.SendMessageAsync(InsanityBot.LanguageConfig["insanitybot.error.lacking_permission"]);

                return;
            }

            String UnlockReason = reason switch
            {
                "usedefault" => GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.no_reason_given"], ctx),
                _ => GetFormattedString(reason, ctx)
            };

            DiscordEmbedBuilder embedBuilder           = null;
            DiscordEmbedBuilder moderationEmbedBuilder = new()
            {
                Title  = "UNLOCK",
                Color  = DiscordColor.Blue,
                Footer = new DiscordEmbedBuilder.EmbedFooter
                {
                    Text = "InsanityBot 2020-2021"
                }
            };

            moderationEmbedBuilder.AddField("Moderator", ctx.Member.Mention, true)
            .AddField("Channel", channel.Mention, true)
            .AddField("Reason", UnlockReason, true);

            try
            {
                List <DiscordOverwrite> overwrites = channel.GetChannelData();
                ChannelData             cachedData = channel.GetCachedChannelData();

                UInt64 exemptRole;
                if ((exemptRole = Convert.ToUInt64(InsanityBot.Config["insanitybot.identifiers.moderation.lock_exempt_role_id"])) != 0)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(exemptRole), allow : DSharpPlus.Permissions.None, reason :
                                                    "InsanityBot - unlocking channel, removing whitelist");
                }

                foreach (UInt64 v in cachedData.LockedRoles)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(v), deny : DSharpPlus.Permissions.None, reason : "InsanityBot - unlocking channel, removing permission overwrites");
                }

                foreach (UInt64 v in cachedData.LockedRoles)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(v), allow : DSharpPlus.Permissions.None, reason : "InsanityBot - unlocking channel, removing permission overwrites");
                }

                foreach (DiscordOverwrite v in overwrites)
                {
                    await channel.AddOverwriteAsync(await v.GetRoleAsync(), v.Allowed, v.Denied, "InsanityBot - unlocking channel, restoring previous permissions");
                }

                embedBuilder = new DiscordEmbedBuilder
                {
                    Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.unlock.success"], ctx),
                    Color       = DiscordColor.Blue,
                    Footer      = new DiscordEmbedBuilder.EmbedFooter
                    {
                        Text = "InsanityBot 2020-2021"
                    }
                };
            }
            catch (Exception e)
            {
                embedBuilder = new DiscordEmbedBuilder
                {
                    Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.unlock.failure"], ctx),
                    Color       = DiscordColor.Red,
                    Footer      = new DiscordEmbedBuilder.EmbedFooter
                    {
                        Text = "InsanityBot 2020-2021"
                    }
                };
                InsanityBot.Client.Logger.LogError($"{e}: {e.Message}");
            }
            finally
            {
                if (!silent)
                {
                    await ctx.Channel.SendMessageAsync(embed : embedBuilder.Build());
                }
            }
        }
    }
}
            public async Task RemoveBlacklistedRoleCommand(CommandContext ctx, DiscordRole role, DiscordChannel channel)
            {
                if (!ctx.Member.HasPermission("insanitybot.admin.lock_blacklist.remove"))
                {
                    await ctx.Channel.SendMessageAsync(InsanityBot.LanguageConfig["insanitybot.error.lacking_admin_permission"]);

                    return;
                }

                DiscordEmbedBuilder embedBuilder           = null;
                DiscordEmbedBuilder moderationEmbedBuilder = new()
                {
                    Title  = "ADMIN: Lock Blacklist Remove",
                    Color  = new(0xff6347),
                    Footer = new()
                    {
                        Text = "InsanityBot 2020-2021"
                    }
                };

                moderationEmbedBuilder.AddField("Administrator", ctx.Member.Mention, true)
                .AddField("Role", role.Mention, true)
                .AddField("Channel", channel.Mention, true);

                try
                {
                    ChannelData data = channel.GetCachedChannelData();

                    if (data.LockedRoles.Contains(role.Id))
                    {
                        data.LockedRoles.Remove(role.Id);
                    }

                    embedBuilder = new()
                    {
                        Color  = new(0xff6347),
                        Footer = new()
                        {
                            Text = "InsanityBot 2020-2021"
                        },
                        Description = GetFormattedString(
                            InsanityBot.LanguageConfig["insanitybot.commands.lock.blacklist.remove.success"], role, channel)
                    };

                    channel.SerializeLockingOverwrites(data);

                    InsanityBot.Client.Logger.LogInformation(new EventId(2000, "LockAdmin"), $"Removed role {role.Id} from channel blacklist");
                }
                catch (Exception e)
                {
                    embedBuilder = new()
                    {
                        Color  = new(0xff6347),
                        Footer = new()
                        {
                            Text = "InsanityBot 2020-2021"
                        },
                        Description = GetFormattedString(
                            InsanityBot.LanguageConfig["insanitybot.commands.lock.blacklist.remove.failure"], role, channel)
                    };

                    InsanityBot.Client.Logger.LogCritical(new EventId(2000, "LockAdmin"),
                                                          $"Administrative action failed: could not remove {role.Id} from channel blacklist. Please contact the InsanityBot team immediately.\n" +
                                                          $"Please also provide them with the following information:\n\n{e}: {e.Message}\n{e.StackTrace}");
                }
                finally
                {
                    await ctx.Channel.SendMessageAsync(embedBuilder.Build());
                }
            }
        }
Example #5
0
        private async Task LockCommand(CommandContext ctx, DiscordChannel channel, String reason = "usedefault", Boolean silent = false)
        {
            if (!ctx.Member.HasPermission("insanitybot.moderation.lock"))
            {
                await ctx.Channel.SendMessageAsync(InsanityBot.LanguageConfig["insanitybot.error.lacking_permission"]);

                return;
            }

            String LockReason = reason switch
            {
                "usedefault" => GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.no_reason_given"],
                                                   ctx),
                _ => GetFormattedString(reason, ctx)
            };

            DiscordEmbedBuilder embedBuilder           = null;
            DiscordEmbedBuilder moderationEmbedBuilder = new()
            {
                Title  = "LOCK",
                Color  = DiscordColor.Blue,
                Footer = new DiscordEmbedBuilder.EmbedFooter
                {
                    Text = "InsanityBot 2020-2021"
                }
            };

            moderationEmbedBuilder.AddField("Moderator", ctx.Member.Mention, true)
            .AddField("Channel", channel.Mention, true)
            .AddField("Reason", LockReason, true);

            try
            {
                channel.SerializeChannelData();
                ChannelData data = channel.GetCachedChannelData();

                await channel.AddOverwriteAsync(InsanityBot.HomeGuild.EveryoneRole, deny : DSharpPlus.Permissions.SendMessages, reason : "InsanityBot - locking channel");

                foreach (UInt64 v in data.LockedRoles)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(v), deny : DSharpPlus.Permissions.SendMessages, reason : "InsanityBot - locking channel, removing access for listed roles");
                }

                foreach (UInt64 v in data.WhitelistedRoles)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(v), allow : DSharpPlus.Permissions.SendMessages, reason : "InsanityBot - locking channel, re-adding access for whitelisted roles");
                }

                UInt64 exemptRole;
                if ((exemptRole = Convert.ToUInt64(InsanityBot.Config["insanitybot.identifiers.moderation.lock_exempt_role_id"])) != 0)
                {
                    await channel.AddOverwriteAsync(InsanityBot.HomeGuild.GetRole(exemptRole), allow : DSharpPlus.Permissions.SendMessages, reason :
                                                    "InsanityBot - locking channel, granting access to whitelisted users");
                }

                embedBuilder = new DiscordEmbedBuilder
                {
                    Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.lock.success"], ctx),
                    Color       = DiscordColor.Blue,
                    Footer      = new DiscordEmbedBuilder.EmbedFooter
                    {
                        Text = "InsanityBot 2020-2021"
                    }
                };
            }
            catch (Exception e)
            {
                embedBuilder = new DiscordEmbedBuilder
                {
                    Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.lock.failure"], ctx),
                    Color       = DiscordColor.Red,
                    Footer      = new DiscordEmbedBuilder.EmbedFooter
                    {
                        Text = "InsanityBot 2020-2021"
                    }
                };
                InsanityBot.Client.Logger.LogError($"{e}: {e.Message}");
            }
            finally
            {
                if (!silent)
                {
                    await ctx.Channel.SendMessageAsync(embed : embedBuilder.Build());
                }
            }
        }
    }