Exemple #1
0
        private static UserRecord GetUserRecord(ModeratorModuleConfig config, ulong id)
        {
            SocketUser user = BotService.Client.GetUser(id);

            if (user == null)
            {
                throw new ArgumentException("User was not found.");
            }
            if (user.IsBot)
            {
                throw new ArgumentException("Cannot get user record for bot.");
            }

            UserRecord matchingRecord = config.UserRecords
                                        .Find(record => record.UserId == id);

            // Ensure the user record exists
            if (matchingRecord == null)
            {
                config.UserRecords.Add(new UserRecord()
                {
                    UserId = id,
                });

                // Could potentially be unsafe?
                matchingRecord = config.UserRecords.Last();
            }

            return(matchingRecord);
        }
Exemple #2
0
        private static async Task WarnOrBanUserAsync(ModeratorModuleConfig config, SocketGuildUser victim, SocketGuildUser requestedBy, string message)
        {
            var userRecord = GetUserRecord(config, victim.Id);

            message = string.IsNullOrWhiteSpace(message) ?
                      "No reason was provided by the moderator." : $"Reason: {message}";

            userRecord.Warnings.Add(new Warning()
            {
                RequestedBy    = requestedBy.Id,
                DateTimeBinary = DateTime.Now.ToBinary(),
            });

            IDMChannel dmChannel = await victim.CreateDMChannelAsync();

            if (userRecord.Warnings.Count >= config.WarningsUntilBan &&
                config.WarningsUntilBan != -1)
            {
                try
                {
                    await victim.BanAsync(1, message);

                    await new WarningMessage(
                        description: $"You have recieved a warning from {requestedBy.Mention} in the server '{requestedBy.Guild.Name}'\nDue to the number of warnings you have recieved from this server, you have been permanently banned.\n```\n{message}\n```",
                        title: "You have been banned!")
                    .SendAsync(dmChannel);

                    return;
                }
                catch (HttpException)
                {
                    // User is most likely admin, so just give another warning.
                }
            }

            await new WarningMessage(
                description: $"You have recieved a warning from {requestedBy.Mention} in the server '{requestedBy.Guild.Name}'\nFurther warnings may result in a ban.\n```\n{message}\n```",
                title: "You have been warned!")
            .SendAsync(dmChannel);
        }