/// <summary> /// Adds a new verbal log entry to the file /// </summary> /// <param name="verbalEntry">VerbalModlogEntry instance to add to file</param> public static void AddVerbalModlogEntry(this DiscordMember member, VerbalModlogEntry verbalEntry) { UserModlog user = Deserialize(member.Username, member.Id); user.VerbalLog.Add(verbalEntry); user.VerbalLogEntryCount++; Serialize(user, member.Id); }
// extension methods for DiscordMember to allow easier accessibility /// <summary> /// Adds a new modlog entry to the file /// </summary> /// <param name="modlogEntry">The ModlogEntry instance to add to file</param> public static void AddModlogEntry(this DiscordMember member, ModlogEntry modlogEntry) { UserModlog user = Deserialize(member.Username, member.Id); user.Modlog.Add(modlogEntry); user.ModlogEntryCount++; Serialize(user, member.Id); }
/// <summary> /// Serializes a modlog instance to its file /// </summary> /// <param name="user">Modlog instance to serialize</param> /// <param name="UserId">ID of the user whose modlog gets serialized. Used to tell apart different modlog files</param> private static void Serialize(UserModlog user, UInt64 UserId) { FileStream file = new($"./data/{UserId}/modlog.json", FileMode.Truncate); StreamWriter writer = new(file); writer.Write(JsonConvert.SerializeObject(user, Formatting.Indented)); writer.Close(); }
/// <summary> /// Adds a new verbal log entry to the file /// </summary> /// <param name="reason">Reason for the infraction</param> public static void AddVerbalModlogEntry(this DiscordMember member, String reason) { UserModlog user = Deserialize(member.Username, member.Id); user.VerbalLog.Add(new VerbalModlogEntry { Reason = reason, Time = DateTime.UtcNow }); user.VerbalLogEntryCount++; Serialize(user, member.Id); }
public async Task UnwarnCommand(CommandContext ctx, DiscordMember member, Int32 WarningIndex) { if (!ctx.Member.HasPermission("insanitybot.moderation.unwarn")) { await ctx.RespondAsync(InsanityBot.LanguageConfig["insanitybot.error.lacking_permission"]); return; } DiscordEmbedBuilder embedBuilder = null; try { UserModlog modlog = member.GetUserModlog(); modlog.Modlog.RemoveAt(WarningIndex); modlog.ModlogEntryCount--; member.SetUserModlog(modlog); embedBuilder = new DiscordEmbedBuilder { Color = DiscordColor.SpringGreen, Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.unwarn.success"], ctx, member), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = "InsanityBot - ExaInsanity 2020-2021" } }; } catch (Exception e) { embedBuilder = new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.moderation.unwarn.failure"], ctx, member), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = "InsanityBot - ExaInsanity 2020-2021" } }; InsanityBot.Client.Logger.LogError($"{e}: {e.Message}"); } finally { await ctx.RespondAsync(embed : embedBuilder.Build()); } }
/// <summary> /// Attempts to set a specific user's modlog. Returns true if successful. /// </summary> public static Task <Boolean> TrySetModlog(this DiscordUser user, UserModlog modlog) { if (user == null) { throw new ArgumentException("Could not set modlog of nonexistent user", nameof(user)); } try { (user as DiscordMember).SetUserModlog(modlog); return(Task.FromResult(true)); } catch (Exception e) { Console.WriteLine($"{e}: {e.Message}\n{e.StackTrace}"); return(Task.FromResult(false)); } }
public static UserModlog Create(String UserName, UInt64 UserId) { if (!Directory.Exists($"./data/{UserId}")) { Directory.CreateDirectory($"./data/{UserId}"); } if (!File.Exists($"./data/{UserId}/modlog.json")) { StreamWriter writer = new StreamWriter(File.Create($"./data/{UserId}/modlog.json")); UserModlog modlog = new UserModlog(UserName); writer.Write(JsonConvert.SerializeObject(modlog, Formatting.Indented)); writer.Close(); return(modlog); } return(null); }
/// <summary> /// Deserializes a modlog instance from its file /// </summary> /// <param name="UserId">ID of the user whose modlog gets called. Used to get the correct modlog file</param> /// <returns>The modlog instance of the user</returns> private static UserModlog Deserialize(String UserName, UInt64 UserId) { if (!File.Exists($"./data/{UserId}/modlog.json")) { Create(UserName, UserId); } StreamReader reader = new($"./data/{UserId}/modlog.json"); String text = reader.ReadToEnd(); reader.Close(); UserModlog modlog = JsonConvert.DeserializeObject <UserModlog>(text); modlog.Username = UserName; StreamWriter writer = new($"./data/{UserId}/modlog.json"); writer.Write(JsonConvert.SerializeObject(modlog, Formatting.Indented)); writer.Close(); return(modlog); }
/// <summary> /// Adds a new modlog entry to the file /// </summary> /// <param name="type">Modlog Type of the new entry</param> /// <param name="reason">Reason for the infraction</param> public static void AddModlogEntry(this DiscordMember member, ModlogEntryType type, String reason) { try { UserModlog user = Deserialize(member.Username, member.Id); if (user == null) { Console.WriteLine("bad"); } user.Modlog.Add(new ModlogEntry { Type = type, Time = DateTime.UtcNow, Reason = reason }); user.ModlogEntryCount++; Serialize(user, member.Id); } catch (Exception e) { Console.WriteLine($"{e}: {e.Message}\n\n{e.StackTrace}"); } }
/// <summary> /// Sets the UserModlog instance of this member. Only intended for low-level data manipulation and testing, /// not for inclusion in production releases. /// </summary> public static void SetUserModlog(this DiscordMember member, UserModlog user) => Serialize(user, member.Id);
public async Task ModlogCommand(CommandContext ctx, DiscordMember user) { if (!ctx.Member.HasPermission("insanitybot.moderation.modlog")) { await ctx.RespondAsync(InsanityBot.LanguageConfig["insanitybot.error.lacking_permission"]); return; } try { UserModlog modlog = user.GetUserModlog(); DiscordEmbedBuilder modlogEmbed = new DiscordEmbedBuilder { Title = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.commands.modlog.embed_title"], ctx, user), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = "InsanityBot - Exa 2020-2021" } }; if (modlog.Modlog.Count == 0) { modlogEmbed.Color = DiscordColor.SpringGreen; modlogEmbed.Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.commands.modlog.empty_modlog"], ctx, user); } else { modlogEmbed.Color = DiscordColor.Red; for (Byte b = 0; b < ToByte(InsanityBot.Config["insanitybot.commands.modlog.max_modlog_entries_per_embed"]) && b < modlog.ModlogEntryCount; b++) { modlogEmbed.Description += $"{modlog.Modlog[b].Type.ToString().ToUpper()}: {modlog.Modlog[b].Time} - {modlog.Modlog[b].Reason}\n"; } if (modlog.ModlogEntryCount > ToByte(InsanityBot.Config["insanitybot.commands.modlog.max_modlog_entries_per_embed"])) { modlogEmbed.Description += GetFormattedString(InsanityBot.LanguageConfig["insanitybot.commands.modlog.overflow"], ctx, user); } } await ctx.RespondAsync(embed : modlogEmbed.Build()); } catch (Exception e) { InsanityBot.Client.Logger.LogError(new EventId(1170, "Modlog"), $"Could not retrieve modlogs: {e}: {e.Message}"); DiscordEmbedBuilder failedModlog = new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = GetFormattedString(InsanityBot.LanguageConfig["insanitybot.commands.modlog.failed"], ctx, user), Footer = new DiscordEmbedBuilder.EmbedFooter { Text = "InsanityBot - ExaInsanity 2020-2021" } }; await ctx.RespondAsync(embed : failedModlog.Build()); } }