Esempio n. 1
0
        public async Task SetWarnAction(WarnAction action)
        {
            int count = await QueryFirstOrDefaultAsync <int>($"SELECT count(Id) " +
                                                             $"FROM WarningAction WHERE ServerId = @ServerId;",
                                                             new { ServerId = action.ServerId });

            if (count != 0)
            {
                await ExecuteAsync($"UPDATE WarningAction " +
                                   $"SET ServerId = @ServerId, Action = @Action, ActionThreshold = @ActionThreshold " +
                                   $"WHERE ServerId = @ServerId;",
                                   new { ServerId = action.ServerId, Action = (int)action.Action, ActionThreshold = action.ActionThreshold });
            }
            else
            {
                await ExecuteAsync($"INSERT INTO WarningAction " +
                                   $"(ServerId, Action, ActionThreshold) " +
                                   $"VALUES (@ServerId, @Action, @ActionThreshold);",
                                   new { ServerId = action.ServerId, Action = (int)action.Action, ActionThreshold = action.ActionThreshold });
            }
        }
Esempio n. 2
0
        public async Task WarnAction([Summary("Action: none, kick or ban")] string action = null,
                                     [Summary("The number of warnings before the action is performed")] int maxWarns = -1)
        {
            await Context.Channel.TriggerTypingAsync();

            _logger.LogInformation("{user}#{discriminator} invoked warnaction ({action}, {maxWarns}) messages in {channel} on {server}",
                                   Context.User.Username, Context.User.Discriminator, action, maxWarns, Context.Channel.Name, Context.Guild?.Name ?? "DM");

            var server = await _serverRepository.GetByServerId(Context.Guild.Id);

            if (server == null)
            {
                server = new Server {
                    GuildId = Context.Guild.Id
                };
                await _serverRepository.AddAsync(server);
            }

            if (action == null && maxWarns < 0)
            {
                var wAction = await _warningRepository.GetWarningAction(server);

                if (wAction == null)
                {
                    await ReplyAsync("The warn action has not been set.");

                    return;
                }
                await Context.Channel.SendEmbedAsync("Warn Action", $"The warn action is set to: `{ Enum.GetName(typeof(WarningAction), wAction.Action)}`. The threshold is: `{wAction.ActionThreshold}`",
                                                     ColorHelper.GetColor(server));

                return;
            }

            var        message    = $"Warn action set to `{action.ToLowerInvariant()}`, Max Warnings { maxWarns} by {Context.User.Mention}";
            bool       valid      = false;
            WarnAction warnAction = null;

            if (action.ToLowerInvariant() == "none" && maxWarns > 0)
            {
                valid      = true;
                warnAction = new WarnAction
                {
                    ServerId        = server.Id,
                    Action          = WarningAction.NoAction,
                    ActionThreshold = maxWarns
                };
            }
            else if (action.ToLowerInvariant() == "kick" && maxWarns > 0)
            {
                valid      = true;
                warnAction = new WarnAction
                {
                    ServerId        = server.Id,
                    Action          = WarningAction.Kick,
                    ActionThreshold = maxWarns
                };
            }
            else if (action.ToLowerInvariant() == "ban" && maxWarns > 0)
            {
                valid      = true;
                warnAction = new WarnAction
                {
                    ServerId        = server.Id,
                    Action          = WarningAction.Ban,
                    ActionThreshold = maxWarns
                };
            }

            if (valid)
            {
                await _warningRepository.SetWarnAction(warnAction);

                await _servers.SendLogsAsync(Context.Guild, $"Warn Action Set", message, ImageLookupUtility.GetImageUrl("LOGGING_IMAGES"));

                await Context.Channel.SendEmbedAsync("Warn Action Set", $"Warn action set to: `{action.ToLowerInvariant()}`. Threshold set to: `{maxWarns}`",
                                                     ColorHelper.GetColor(server));
            }
            else
            {
                await ReplyAsync("Please provide a valid option: `none`, `kick`, `ban` and positive maximum warnings.");
            }
        }
Esempio n. 3
0
        public async Task Warn([Summary("The user to warn")] SocketGuildUser user, [Summary("The reason for the warning")][Remainder] string reason = "No Reason Provided")
        {
            await Context.Channel.TriggerTypingAsync();

            _logger.LogInformation("{user}#{discriminator} warned {user} for {reason} in {channel} on {server}",
                                   Context.User.Username, Context.User.Discriminator, user.Username, reason, Context.Channel.Name, Context.Guild?.Name ?? "DM");

            if (user.Id == _client.CurrentUser.Id)
            {
                await ReplyAsync("Nice try, but I am immune from warnings!");

                return;
            }

            if (user.Id == Context.User.Id)
            {
                await ReplyAsync("Lol, you are warning yourself!");
            }

            var server = await ServerHelper.GetOrAddServer(Context.Guild.Id, _serverRepository);

            var userDb = await UserHelper.GetOrAddUser(user, _userRepository);

            var warning = new Warning
            {
                UserId   = userDb.Id,
                ServerId = server.Id,
                Text     = reason
            };
            await _warningRepository.AddAsync(warning);

            var warn = await _warningRepository.GetUsersWarnings(server, userDb);

            var wAction = await _warningRepository.GetWarningAction(server);

            if (wAction == null)
            {
                wAction = new WarnAction
                {
                    Action          = WarningAction.NoAction,
                    ActionThreshold = 1
                };
                await ReplyAsync($"{Context.User.Mention}: NOTE! The warning action has not been set!");
            }

            await Context.Channel.SendEmbedAsync("You have been warned!", $"{user.Mention} you have been warned for: `{reason}`!\n" +
                                                 $"This is warning #`{warn.Count()}` of `{wAction.ActionThreshold}`\n" +
                                                 $"The action is set to: { Enum.GetName(typeof(WarningAction), wAction.Action)}",
                                                 ColorHelper.GetColor(server));

            if (warn.Count() >= wAction.ActionThreshold)
            {
                var message = $"The maximum number of warnings has been reached, because of the warn action ";
                switch (wAction.Action)
                {
                case WarningAction.NoAction:
                    message += "nothing happens.";
                    break;

                case WarningAction.Kick:
                    message += $"{user.Username} has been kicked.";
                    await user.KickAsync("Maximum Warnings Reached!");

                    break;

                case WarningAction.Ban:
                    message += $"{user.Username} has been banned.";
                    await user.BanAsync(0, "Maximum Warnings Reached!");

                    break;

                default:
                    message += "default switch statement :(";
                    break;
                }

                await ReplyAsync(message);
            }
            await _servers.SendLogsAsync(Context.Guild, $"User Warned", $"{Context.User.Mention} warned {user.Username} for: {reason}", ImageLookupUtility.GetImageUrl("LOGGING_IMAGES"));
        }