public static async Task <User> InitUser(this IRedisDatabase redis, ulong userId) { var user = await redis.GetAsync <User>(RedisKeyNaming.User(userId)); if (user != null) { return(user); } user = new User { Id = userId, Description = null, Birthdate = null, SteamId = null, Nicknames = new List <Nickname>(), Infractions = new List <Infraction>(), InfractionId = 0 }; await redis.AddAsync <User>(RedisKeyNaming.User(userId), user); return(user); }
private async Task WhoIsPreset(CommandContext context, ulong userId) { await redis.InitUser(userId); var member = await context.Guild.GetMemberAsync(userId); var nickname = string.IsNullOrWhiteSpace(member.Nickname) ? string.Empty : $"({member.Nickname})"; var owners = context.Client.CurrentApplication.Owners; var author = new EmbedAuthor { Name = $"{member.GetUsertag()} {nickname}", Icon = member.AvatarUrl }; var description = new StringBuilder(); if (member.IsOwner) { description.AppendLine(owners.Any(x => x.Id == member.Id) ? $"{DiscordEmoji.FromGuildEmote(context.Client, EmojiLibrary.Verified)} {Formatter.InlineCode("Guild and Bot Owner")}" : $"{DiscordEmoji.FromGuildEmote(context.Client, EmojiLibrary.Verified)} {Formatter.InlineCode("Guild Owner")}"); } else if (owners.Any(x => x.Id == member.Id)) { description.AppendLine($"{DiscordEmoji.FromGuildEmote(context.Client, EmojiLibrary.Verified)} {Formatter.InlineCode("Bot Owner")}"); } else if (member.IsBot) { description.AppendLine(Formatter.InlineCode("`[BOT]`")); } var userDays = await member.GetDaysExisting(); var userSinceDays = userDays == 1 ? $"yesterday" : userDays == 0 ? "today" : $"{Formatter.Bold($"{userDays}")} days"; var memberDays = await member.GetMemberDays(); var memberSinceDays = memberDays == 1 ? $"yesterday" : memberDays == 0 ? "today" : $"{Formatter.Bold($"{memberDays}")} days ago"; description.AppendLine($"Identity: `{member.Id}`") .AppendLine($"Registered: {await member.CreatedAtLongDateTimeString()} ({userSinceDays})") .AppendLine($"Joined: {await member.JoinedAtLongDateTimeString()} ({memberSinceDays})") .AppendLine($"Join Position: #{await JoinPosition(member, context.Guild)}"); var roles = string.Empty; if (member.Roles.Any()) { var rolesOrdered = member.Roles.ToList().OrderByDescending(x => x.Position); roles = rolesOrdered.Aggregate(roles, (current, role) => current + $"<@&{role.Id}> "); } else { roles = "None"; } var userInfractions = redis.GetAsync <Defcon.Data.Users.User>(RedisKeyNaming.User(userId)).GetAwaiter().GetResult().Infractions; var userGuildInfractions = userInfractions.Where(x => x.GuildId == context.Guild.Id) .ToList(); var infractions = new StringBuilder().AppendLine($"Warnings: {userGuildInfractions.Count(x => x.InfractionType == InfractionType.Warning)}") .AppendLine($"Mutes: {userGuildInfractions.Count(x => x.InfractionType == InfractionType.Mute)}") .AppendLine($"Kicks: {userGuildInfractions.Count(x => x.InfractionType == InfractionType.Kick)}").ToString(); var keys = await UserKeyPermissions(member); var fields = new List <EmbedField> { new EmbedField { Inline = true, Name = "Boosting since", Value = await member.PremiumSinceLongDateTimeString() }, new EmbedField { Inline = true, Name = "Server Infractions", Value = infractions }, new EmbedField { Inline = false, Name = "Roles", Value = roles } }; if (!string.IsNullOrWhiteSpace(keys[0])) { fields.Add(new EmbedField() { Name = "Key Permissions", Value = keys[0], Inline = false }); } if (!string.IsNullOrWhiteSpace(keys[1])) { fields.Add(new EmbedField() { Name = "Acknowledgement", Value = keys[1], Inline = false }); } var embed = new Embed { Description = description.ToString(), Thumbnail = member.AvatarUrl, Author = author, Fields = fields }; await context.SendEmbedMessageAsync(embed); }
public async Task CreateInfraction(DiscordGuild guild, DiscordChannel channel, DiscordClient client, DiscordMember moderator, DiscordMember suspect, string reason, InfractionType infractionType) { var suspectLeft = guild.GetMemberAsync(suspect.Id).Result == null; var embed = new Embed(); var owners = client.CurrentApplication.Owners; var verb = infractionType.ToInfractionString().ToLowerInvariant(); var action = infractionType.ToActionString().ToLowerInvariant(); var isSuspectAdministrator = false; var isModerator = await redis.IsModerator(guild.Id, moderator); var isAdministrator = moderator.Roles.Any(role => role.Permissions.HasPermission(Permissions.Administrator)); if (!isModerator && isAdministrator) { isModerator = true; } if (isModerator) { if (!suspectLeft) { if (suspect.Roles.Any(role => role.Permissions.HasPermission(Permissions.Administrator))) { isSuspectAdministrator = true; } if (moderator == suspect) { await channel.SendMessageAsync($"You can not {verb} yourself!"); } else if (owners.Any(x => x.Id == suspect.Id)) { await channel.SendMessageAsync($"You can not {verb} {suspect.Username}!"); } else if (suspect.IsOwner) { await channel.SendMessageAsync($"You can not {verb} the owner!"); } else if (isSuspectAdministrator) { await channel.SendMessageAsync($"You can not {verb} a administrator!"); } else if (await redis.IsModerator(guild.Id, suspect)) { var guildData = await redis.GetAsync <Guild>(RedisKeyNaming.Guild(guild.Id)); switch (infractionType) { case InfractionType.Ban: case InfractionType.Kick: await channel.SendMessageAsync($"You can not {verb} a moderator!"); break; case InfractionType.Warning: { if (!guildData.AllowWarnModerators) { await channel.SendMessageAsync($"You can not {verb} a moderator! [Disabled]"); } break; } case InfractionType.Mute: { if (!guildData.AllowMuteModerators) { await channel.SendMessageAsync($"You can not {verb} a moderator! [Disabled]"); } break; } } } else { var userData = await redis.InitUser(suspect.Id); userData.Infractions.Add(new Infraction() { Id = ++userData.InfractionId, ModeratorId = moderator.Id, ModeratorUsername = moderator.GetUsertag(), GuildId = guild.Id, InfractionType = infractionType, Reason = reason, Date = DateTimeOffset.UtcNow }); await redis.ReplaceAsync <User>(RedisKeyNaming.User(suspect.Id), userData); var description = new StringBuilder().AppendLine($"Moderator: {moderator.GetUsertag()} {Formatter.InlineCode($"{moderator.Id}")}") .AppendLine($"Reason: {reason}"); description.AppendLine("User left the server already!"); embed = new Embed() { Title = $"{suspect.Username} has been {action}!", Description = description.ToString(), Footer = new EmbedFooter() { Text = $"Infractions: {userData.Infractions.Count}" } }; await channel.SendEmbedMessageAsync(embed); embed.Title = $"You have been {action} on {guild.Name}"; await suspect.SendEmbedMessageAsync(embed); switch (infractionType) { case InfractionType.Kick: await suspect.RemoveAsync(reason); break; case InfractionType.Ban: await guild.BanMemberAsync(suspect.Id, 7, reason); break; } } } else { var suspectedUser = await client.GetUserAsync(suspect.Id); var userData = await redis.InitUser(suspectedUser.Id); userData.Infractions.Add(new Infraction() { Id = ++userData.InfractionId, ModeratorId = moderator.Id, ModeratorUsername = moderator.GetUsertag(), GuildId = guild.Id, InfractionType = infractionType, Reason = reason, Date = DateTimeOffset.UtcNow }); await redis.ReplaceAsync <User>(RedisKeyNaming.User(suspectedUser.Id), userData); var description = new StringBuilder().AppendLine($"Moderator: {moderator.GetUsertag()} {Formatter.InlineCode($"{moderator.Id}")}") .AppendLine($"Reason: {reason}") .AppendLine("User left the server already!"); embed = new Embed() { Title = $"{suspect.Username} has been {action}!", Description = description.ToString(), Footer = new EmbedFooter() { Text = $"Infractions: {userData.Infractions.Count}" } }; await channel.SendEmbedMessageAsync(embed); switch (infractionType) { case InfractionType.Kick: await channel.SendMessageAsync("Suspect already left"); break; case InfractionType.Ban: await guild.BanMemberAsync(suspectedUser.Id); break; } } } else { await channel.SendMessageAsync("You are not a moderator of this server."); } }