diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 2e4c129c..afbfb765 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -103,6 +103,14 @@ namespace NadekoBot.Classes } } + internal void UpdateAll(IEnumerable objs) where T : IDataModel + { + using (var conn = new SQLiteConnection(FilePath)) + { + conn.UpdateAll(objs); + } + } + internal HashSet GetAllRows() where T : IDataModel, new() { using (var conn = new SQLiteConnection(FilePath)) diff --git a/NadekoBot/Classes/IncidentsHandler.cs b/NadekoBot/Classes/IncidentsHandler.cs index 8bbff5d7..5d8190fe 100644 --- a/NadekoBot/Classes/IncidentsHandler.cs +++ b/NadekoBot/Classes/IncidentsHandler.cs @@ -1,18 +1,25 @@ -using System; -using System.IO; +using NadekoBot.DataModels; +using System; namespace NadekoBot.Classes { internal static class IncidentsHandler { - public static void Add(ulong serverId, string text) + public static void Add(ulong serverId, ulong channelId, string text) { - Directory.CreateDirectory("data/incidents"); - File.AppendAllText($"data/incidents/{serverId}.txt", text + "\n--------------------------\n"); var def = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"INCIDENT: {text}"); Console.ForegroundColor = def; + var incident = new Incident + { + ChannelId = (long)channelId, + ServerId = (long)serverId, + Text = text, + Read = false + }; + + DbHandler.Instance.InsertData(incident); } } } diff --git a/NadekoBot/Modules/Administration/AdministrationModule.cs b/NadekoBot/Modules/Administration/AdministrationModule.cs index f3ae5cbd..8689ea4a 100644 --- a/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/NadekoBot/Modules/Administration/AdministrationModule.cs @@ -27,6 +27,7 @@ namespace NadekoBot.Modules.Administration commands.Add(new CustomReactionsCommands(this)); commands.Add(new AutoAssignRole(this)); commands.Add(new SelfCommands(this)); + commands.Add(new IncidentsCommands(this)); NadekoBot.Client.GetService().CommandExecuted += DeleteCommandMessage; } diff --git a/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs b/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs new file mode 100644 index 00000000..a5854708 --- /dev/null +++ b/NadekoBot/Modules/Administration/Commands/IncidentsCommands.cs @@ -0,0 +1,47 @@ +using Discord.Commands; +using NadekoBot.Classes; +using NadekoBot.DataModels; +using NadekoBot.Modules.Permissions.Classes; +using System.IO; +using System.Linq; + +namespace NadekoBot.Modules.Administration.Commands +{ + internal class IncidentsCommands : DiscordCommand + { + public IncidentsCommands(DiscordModule module) : base(module) { } + internal override void Init(CommandGroupBuilder cgb) + { + cgb.CreateCommand(Module.Prefix + "listincidents") + .Alias(Prefix + "lin") + .Description("List all UNREAD incidents and flags them as read.") + .AddCheck(SimpleCheckers.ManageServer()) + .Do(async e => + { + var sid = (long)e.Server.Id; + var incs = DbHandler.Instance.FindAll(i => i.ServerId == sid && i.Read == false); + DbHandler.Instance.UpdateAll(incs.Select(i => { i.Read = true; return i; })); + + await e.User.SendMessage(string.Join("\n----------------------", incs.Select(i => i.Text))); + }); + + cgb.CreateCommand(Module.Prefix + "listallincidents") + .Alias(Prefix + "lain") + .Description("Sends you a file containing all incidents and flags them as read.") + .AddCheck(SimpleCheckers.ManageServer()) + .Do(async e => + { + var sid = (long)e.Server.Id; + var incs = DbHandler.Instance.FindAll(i => i.ServerId == sid); + DbHandler.Instance.UpdateAll(incs.Select(i => { i.Read = true; return i; })); + var data = string.Join("\n----------------------\n", incs.Select(i => i.Text)); + MemoryStream ms = new MemoryStream(); + var sw = new StreamWriter(ms); + sw.WriteLine(data); + sw.Flush(); + sw.BaseStream.Position = 0; + await e.User.SendFile("incidents.txt", sw.BaseStream); + }); + } + } +} diff --git a/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs index c9c9a945..ebf8534a 100644 --- a/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs @@ -25,9 +25,9 @@ namespace NadekoBot.Modules.Permissions.Commands if (filterRegex.IsMatch(args.Message.RawText)) { await args.Message.Delete().ConfigureAwait(false); - IncidentsHandler.Add(args.Server.Id, $"User [{args.User.Name}/{args.User.Id}] posted " + - $"INVITE LINK in [{args.Channel.Name}/{args.Channel.Id}] channel. " + - $"Full message: [[{args.Message.Text}]]"); + IncidentsHandler.Add(args.Server.Id, args.Channel.Id, $"User [{args.User.Name}/{args.User.Id}] posted " + + $"INVITE LINK in [{args.Channel.Name}/{args.Channel.Id}] channel.\n" + + $"`Full message:` {args.Message.Text}"); if (serverPerms.Verbose) await args.Channel.SendMessage($"{args.User.Mention} Invite links are not " + $"allowed on this channel.") diff --git a/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs index 3d21c1a0..5607592c 100644 --- a/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs @@ -23,9 +23,9 @@ namespace NadekoBot.Modules.Permissions.Commands if (serverPerms.Words.Any(w => wordsInMessage.Contains(w))) { await args.Message.Delete().ConfigureAwait(false); - IncidentsHandler.Add(args.Server.Id, $"User [{args.User.Name}/{args.User.Id}] posted " + - $"BANNED WORD in [{args.Channel.Name}/{args.Channel.Id}] channel. " + - $"Full message: [[{args.Message.Text}]]"); + IncidentsHandler.Add(args.Server.Id, args.Channel.Id, $"User [{args.User.Name}/{args.User.Id}] posted " + + $"BANNED WORD in [{args.Channel.Name}/{args.Channel.Id}] channel.\n" + + $"`Full message:` {args.Message.Text}"); if (serverPerms.Verbose) await args.Channel.SendMessage($"{args.User.Mention} One or more of the words you used " + $"in that sentence are not allowed here.") diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 2f831d00..069d0758 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -136,6 +136,7 @@ + @@ -192,7 +193,7 @@ - + diff --git a/NadekoBot/_Models/DataModels/Incident.cs b/NadekoBot/_Models/DataModels/Incident.cs index b1254f72..2ce2ddd6 100644 --- a/NadekoBot/_Models/DataModels/Incident.cs +++ b/NadekoBot/_Models/DataModels/Incident.cs @@ -1,13 +1,10 @@ -using System; - -namespace NadekoBot.DataModels +namespace NadekoBot.DataModels { class Incident : IDataModel { public long ServerId { get; set; } public long ChannelId { get; set; } public string Text { get; set; } - public DateTime When { get; set; } public bool Read { get; set; } = false; } }