Greatly improved incidents. you can now list unread incidents with .lin, and get .txt of all incidents with .lain command

This commit is contained in:
Master Kwoth 2016-06-28 02:09:59 +02:00
parent 29092206e5
commit a6d35aa6c2
8 changed files with 77 additions and 16 deletions

View File

@ -103,6 +103,14 @@ namespace NadekoBot.Classes
}
}
internal void UpdateAll<T>(IEnumerable<T> objs) where T : IDataModel
{
using (var conn = new SQLiteConnection(FilePath))
{
conn.UpdateAll(objs);
}
}
internal HashSet<T> GetAllRows<T>() where T : IDataModel, new()
{
using (var conn = new SQLiteConnection(FilePath))

View File

@ -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>(incident);
}
}
}

View File

@ -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<CommandService>().CommandExecuted += DeleteCommandMessage;
}

View File

@ -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<Incident>(i => i.ServerId == sid && i.Read == false);
DbHandler.Instance.UpdateAll<Incident>(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<Incident>(i => i.ServerId == sid);
DbHandler.Instance.UpdateAll<Incident>(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);
});
}
}
}

View File

@ -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.")

View File

@ -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.")

View File

@ -136,6 +136,7 @@
<ItemGroup>
<Compile Include="Modules\Administration\Commands\AutoAssignRole.cs" />
<Compile Include="Modules\Administration\Commands\CustomReactionsCommands.cs" />
<Compile Include="Modules\Administration\Commands\SelfAssignedRolesCommand - Copy.cs" />
<Compile Include="Modules\Administration\Commands\SelfCommands.cs" />
<Compile Include="Modules\ClashOfClans\ClashOfClans.cs" />
<Compile Include="Classes\DBHandler.cs" />
@ -192,7 +193,7 @@
<Compile Include="Modules\Administration\Commands\CrossServerTextChannel.cs" />
<Compile Include="Modules\Utility\Commands\InfoCommands.cs" />
<Compile Include="Modules\Utility\Commands\Remind.cs" />
<Compile Include="Modules\Administration\Commands\SelfAssignedRolesCommand.cs" />
<Compile Include="Modules\Administration\Commands\IncidentsCommands.cs" />
<Compile Include="Modules\ClashOfClans\ClashOfClansModule.cs" />
<Compile Include="Modules\Permissions\Commands\FilterWordsCommand.cs" />
<Compile Include="Modules\Permissions\Commands\FilterInvitesCommand.cs" />

View File

@ -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;
}
}