Example #1
0
        /// <summary>
        ///     Voice mutes specified user.
        ///     <para>Prevents the user from talking on voice channels.</para>
        /// </summary>
        public async Task <Embed> VoiceMuteAsync(SocketCommandContext context, IGuildUser user, string reason)
        {
            //If user is already muted, tell the command issuer that the specified user is already muted
            if (user.IsMuted)
            {
                return(CustomFormats.CreateErrorEmbed($"{user} is already voice muted."));
            }

            //If user isn't already muted, then mute him
            await user.ModifyAsync(x => x.Mute = true, new RequestOptions { AuditLogReason = reason });

            var caseId = await GenerateModCaseId(context.Guild.Id);

            //Create modCase
            var modCase = new ModCase(context, user, caseId, PunishmentType.VMute, reason);
            await _botContext.ModCases.AddAsync(modCase);

            await _botContext.SaveChangesAsync();

            await SendModLog(context.Guild, modCase);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} voice muted",
                                                           $"{user} has been voice muted for: {reason ?? "_No reason_"}.",
                                                           Color.DarkGrey));
        }
Example #2
0
        /// <summary>
        ///     Mutes specified user.
        ///     <para>Prevents the user from sending chat messages.</para>
        /// </summary>
        public async Task <Embed> MuteAsync(SocketCommandContext context, IGuildUser user, string reason)
        {
            //Get Muted role if it exists or create it if it doesn't exist
            var muteRole = Helper.DoesRoleExist(context.Guild, "Muted") ??
                           await context.Guild.CreateRoleAsync("Muted", GuildPermissions.None, Color.DarkGrey, false,
                                                               null);

            if (user.RoleIds.Any(role => role == muteRole.Id))
            {
                return(CustomFormats.CreateErrorEmbed($"{user} is already muted!"));
            }

            //Add muted role to the user
            await user.AddRoleAsync(muteRole);

            //Loop through every channel in the guild and deny the user from sending messages
            foreach (var channel in context.Guild.TextChannels)
            {
                await channel.AddPermissionOverwriteAsync(user, new OverwritePermissions(sendMessages : PermValue.Deny));
            }

            var caseId = await GenerateModCaseId(context.Guild.Id);

            //Create modCase
            var modCase = new ModCase(context, user, caseId, PunishmentType.Mute, reason);
            await _botContext.ModCases.AddAsync(modCase);

            await _botContext.SaveChangesAsync();

            await SendModLog(context.Guild, modCase);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} muted",
                                                           $"{user} has been muted for: {reason ?? "_No reason_"}.", Color.DarkGrey));
        }
Example #3
0
        /// <summary>
        ///     Unmutes specified user.
        /// </summary>
        public static async Task <Embed> UnmuteAsync(SocketCommandContext context, IGuildUser user)
        {
            //Get Muted role if it exists
            var muteRole = Helper.DoesRoleExist(user.Guild, "Muted");

            if (muteRole == null)
            {
                return(CustomFormats.CreateErrorEmbed("Your server doesn't have a **'Muted'** role!"));
            }

            if (user.RoleIds.All(role => role != muteRole.Id))
            {
                return(CustomFormats.CreateErrorEmbed($"{user} is not muted!"));
            }

            //Loop through every channel in the guild and remove the permission overwrite
            foreach (var channel in context.Guild.TextChannels)
            {
                await channel.RemovePermissionOverwriteAsync(user);
            }

            //Remove muted role from user
            await user.RemoveRoleAsync(muteRole);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} unmuted", $"{user} has been unmuted.",
                                                           0x268618));
        }
Example #4
0
        /// <summary>
        ///     Fired whenever a user is unbanned from the server.
        /// </summary>
        private async Task UserUnbanned(SocketUser unbannedUser, SocketGuild guild)
        {
            //Retrieve guild settings
            var guildSettings = _botContext.Guilds.AsNoTracking().FirstOrDefault(x => x.GuildId == guild.Id);

            if (guildSettings is null)
            {
                return;
            }

            //Check if guild has moderation functionality enabled
            if (guildSettings.ModerationChannel == 0)
            {
                return;
            }

            IEnumerable <RestAuditLogEntry> logs = null;

            try
            {
                logs = await guild.GetAuditLogsAsync(5).FlattenAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            var entry             = logs?.FirstOrDefault(x => (x.Data as UnbanAuditLogData)?.Target.Id == unbannedUser.Id);
            var moderationChannel = guild.GetTextChannel(guildSettings.ModerationChannel);
            await moderationChannel.SendMessageAsync(embed : ModerationFormats.UnbanEmbed(unbannedUser, entry?.User));
        }
Example #5
0
        /// <summary>Voice unmutes specified user.
        /// </summary>
        public static async Task <Embed> UnmuteVoiceAsync(IGuildUser user)
        {
            //If user isn't muted, tell the command issuer that the specified user isn't muted
            if (!user.IsMuted)
            {
                return(CustomFormats.CreateErrorEmbed($"{user} isn't voice muted."));
            }

            //If user is muted, then unmute him
            await user.ModifyAsync(x => x.Mute = false);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} voice unmuted", $"{user} has been voice unmuted.",
                                                           Color.DarkGrey));
        }
Example #6
0
        public async Task SendModLog(IGuild guild, ModCase modCase)
        {
            //Retrieve guild settings
            var guildSettings = _botContext.Guilds.AsNoTracking().FirstOrDefault(x => x.GuildId == guild.Id);

            if (guildSettings == null)
            {
                return;
            }

            if (guildSettings.ModerationChannel == 0)
            {
                return;
            }

            var moderationChannel = await guild.GetTextChannelAsync(guildSettings.ModerationChannel);

            await moderationChannel.SendMessageAsync(embed : ModerationFormats.ModLogEmbed(modCase));
        }
Example #7
0
        /// <summary>Kick specified user from the server with reason.
        /// </summary>
        public async Task <Embed> KickAsync(IUser user, string reason, SocketCommandContext context)
        {
            await SendPunishmentDm(user, ModerationFormats.DmPunishmentEmbed("You have been kicked!",
                                                                             $"You have been kicked from {context.Guild.Name}", context.Guild));

            await((IGuildUser)user).KickAsync(reason);

            var caseId = await GenerateModCaseId(context.Guild.Id);

            //Create mod case
            var modCase = new ModCase(context, user, caseId, PunishmentType.Kick, reason);
            await _botContext.ModCases.AddAsync(modCase);

            await _botContext.SaveChangesAsync();

            await SendModLog(context.Guild, modCase);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} kicked", $"{user} was kicked from the server for: {reason ?? "_No reason_"}.", Color.DarkGrey));
        }
Example #8
0
        /// <summary> Searches mod cases for matching case id and returns it. </summary>
        public async Task LookupCaseAsync(SocketCommandContext context, ulong caseId)
        {
            //Try to get the mod case specified
            var modCase = await _botContext.ModCases.AsNoTracking().FirstOrDefaultAsync(x => x.ModCaseId == caseId && x.GuildId == context.Guild.Id);

            //If there isn't any mod case for specified id, then return
            if (modCase == null)
            {
                await context.Channel.SendMessageAsync(
                    embed : CustomFormats.CreateErrorEmbed("Specified case not found!"));

                return;
            }

            //Try to get the latest user username as the one in mod case can be outdated.
            //If we can't get the username for some reason, use the one in the mod case
            string username    = context.Client.GetUser(modCase.UserId)?.ToString() ?? modCase.UserName;
            string modUsername = context.Client.GetUser(modCase.ModId)?.ToString() ?? modCase.ModName;

            //Send the mod case
            await context.Channel.SendMessageAsync(embed : ModerationFormats.LookupEmbed(modCase, username, modUsername));
        }
Example #9
0
        /// <summary>
        ///     Ban specified user from the server with reason.
        /// </summary>
        public async Task <Embed> BanAsync(IUser user, int pruneDays, string reason, SocketCommandContext context)
        {
            await context.Message.DeleteAsync();

            if (pruneDays < 0 || pruneDays > 7)
            {
                return(CustomFormats.CreateErrorEmbed("Prune days must be between 0 and 7"));
            }

            //Check if user is already banned
            var isBanned = await context.Guild.GetBanAsync(user);

            if (isBanned != null)
            {
                return(CustomFormats.CreateErrorEmbed($"{user.Username} is already banned!"));
            }

            await SendPunishmentDm(user, ModerationFormats.DmPunishmentEmbed("You have been banned!",
                                                                             $"You have been permanently banned from {context.Guild.Name}", context.Guild));

            _banCache.Set(user.Id, new CacheModel(context.Guild.Id), TimeSpan.FromSeconds(5));

            //Ban user
            await context.Guild.AddBanAsync(user, pruneDays, reason);

            var caseId = await GenerateModCaseId(context.Guild.Id);

            //Create modCase
            var modCase = new ModCase(context.User, context.Guild.Id, user, caseId, PunishmentType.Ban, reason);
            await _botContext.ModCases.AddAsync(modCase);

            await _botContext.SaveChangesAsync();

            await SendModLog(context.Guild, modCase);

            return(ModerationFormats.CreateModerationEmbed(user, $"{user} banned",
                                                           $"{user} was banned from the server for: {reason ?? "_No reason_"}.", Color.DarkGrey));
        }
Example #10
0
 public static async Task SendModLog(SocketTextChannel moderationChannel, ModCase modCase)
 {
     await moderationChannel.SendMessageAsync(embed : ModerationFormats.ModLogEmbed(modCase));
 }