示例#1
0
        /// <summary>
        /// Add an auto role for the given server
        /// </summary>
        /// <param name="serverId">The id of the sercer</param>
        /// <param name="roleId">The id of the role to add</param>
        /// <returns></returns>
        public async Task AddAutoRole(ulong serverId, ulong roleId)
        {
            var server = await _serverRepository.GetByServerId(serverId);

            if (server == null)
            {
                await _serverRepository.AddAsync(new Server { GuildId = serverId, Prefix = _settings.DefaultPrefix });
            }

            await _autoRoleRepository.AddAsync(new AutoRole { RoleId = roleId, ServerId = server.Id });
        }
        private async Task <Server> GetServerOrThrow(ulong serverId)
        {
            var server = await _serverRepository.GetByServerId(serverId);

            if (server == null)
            {
                _logger.LogWarning("Attempted to access a server ({server}) that does not exist.", serverId);
                throw new ArgumentException("Server does not exist!", nameof(serverId));
            }

            return(server);
        }
示例#3
0
        public async static Task <Server> GetOrAddServer(ulong serverId, IServerRepository serverRepository)
        {
            var server = await serverRepository.GetByServerId(serverId);

            if (server == null)
            {
                server = new Server {
                    GuildId = serverId
                };
                await serverRepository.AddAsync(server);
            }

            return(server);
        }
        public async Task AllowProfanity(ulong serverId, string profanity)
        {
            var profanityDB = await GetProfanity(profanity);

            if (profanityDB == null)
            {
                profanityDB = new Profanity {
                    Word = profanity
                };
                await AddAsync(profanityDB);
            }

            var server = await _serverRepository.GetByServerId(serverId);

            if (server == null)
            {
                _logger.LogWarning("Attempted to access a server ({server}) that does not exist.", serverId);
                throw new ArgumentException("Server does not exist!", nameof(serverId));
            }

            int count = await QueryFirstOrDefaultAsync <int>($"SELECT count(Id) " +
                                                             $"FROM ProfanityServer WHERE ServerId = @ServerId " +
                                                             $"AND ProfanityId = @ProfanityId;",
                                                             new { ServerId = server.Id, ProfanityId = profanityDB.Id });

            if (count != 0)
            {
                await ExecuteAsync($"UPDATE ProfanityServer " +
                                   $"SET ServerId = @ServerId, ProfanityId = @ProfanityId, ProfanityMode = @ProfanityMode " +
                                   $"WHERE ServerId = @ServerId AND ProfanityId = @ProfanityId;",
                                   new { ServerId = server.Id, ProfanityId = profanityDB.Id, ProfanityMode = (int)ProfanityMode.Allow });
            }
            else
            {
                await ExecuteAsync($"INSERT INTO ProfanityServer " +
                                   $"(ServerId, ProfanityId, ProfanityMode) " +
                                   $"VALUES (@ServerId, @ProfanityId, @ProfanityMode);",
                                   new { ServerId = server.Id, ProfanityId = profanityDB.Id, ProfanityMode = (int)ProfanityMode.Allow });
            }
        }
示例#5
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.");
            }
        }