From 29092206e5a8fb1adbff4cf79f3554360129da44 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 27 Jun 2016 22:33:03 +0200 Subject: [PATCH] Split administration into administration and utility. Added incidents table (WIP) --- NadekoBot/Classes/DBHandler.cs | 7 +- NadekoBot/Classes/IncidentsHandler.cs | 9 +- .../Administration/AdministrationModule.cs | 117 -------------- .../Commands/InfoCommands.cs | 2 +- .../Commands/Remind.cs | 2 +- NadekoBot/Modules/Utility/UtilityModule.cs | 151 ++++++++++++++++++ NadekoBot/NadekoBot.cs | 2 + NadekoBot/NadekoBot.csproj | 6 +- NadekoBot/_Models/DataModels/Incident.cs | 13 ++ NadekoBot/_Models/JSONModels/Configuration.cs | 1 + NadekoBot/bin/Debug/data/config_example.json | 3 +- 11 files changed, 185 insertions(+), 128 deletions(-) rename NadekoBot/Modules/{Administration => Utility}/Commands/InfoCommands.cs (99%) rename NadekoBot/Modules/{Administration => Utility}/Commands/Remind.cs (99%) create mode 100644 NadekoBot/Modules/Utility/UtilityModule.cs create mode 100644 NadekoBot/_Models/DataModels/Incident.cs diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index 96f7d8dd..2e4c129c 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -32,6 +32,7 @@ namespace NadekoBot.Classes conn.CreateTable(); conn.CreateTable(); conn.CreateTable(); + conn.CreateTable(); conn.Execute(Queries.TransactionTriggerQuery); try { @@ -205,7 +206,7 @@ public class PlaylistData public static class Queries { - public static string TransactionTriggerQuery = @" + public const string TransactionTriggerQuery = @" CREATE TRIGGER IF NOT EXISTS OnTransactionAdded AFTER INSERT ON CurrencyTransaction BEGIN @@ -216,8 +217,8 @@ INSERT OR REPLACE INTO CurrencyState (Id, UserId, Value, DateAdded) NEW.DateAdded); END "; - public static string DeletePlaylistTriggerQuery = @" -CREATE TRIGGER music_playlist + public const string DeletePlaylistTriggerQuery = @" +CREATE TRIGGER IF NOT EXISTS music_playlist AFTER DELETE ON MusicPlaylist FOR EACH ROW BEGIN diff --git a/NadekoBot/Classes/IncidentsHandler.cs b/NadekoBot/Classes/IncidentsHandler.cs index 0791fa6c..8bbff5d7 100644 --- a/NadekoBot/Classes/IncidentsHandler.cs +++ b/NadekoBot/Classes/IncidentsHandler.cs @@ -1,9 +1,12 @@ using System; using System.IO; -namespace NadekoBot.Classes { - internal static class IncidentsHandler { - public static void Add(ulong serverId, string text) { +namespace NadekoBot.Classes +{ + internal static class IncidentsHandler + { + public static void Add(ulong serverId, string text) + { Directory.CreateDirectory("data/incidents"); File.AppendAllText($"data/incidents/{serverId}.txt", text + "\n--------------------------\n"); var def = Console.ForegroundColor; diff --git a/NadekoBot/Modules/Administration/AdministrationModule.cs b/NadekoBot/Modules/Administration/AdministrationModule.cs index c731dd69..f3ae5cbd 100644 --- a/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/NadekoBot/Modules/Administration/AdministrationModule.cs @@ -24,8 +24,6 @@ namespace NadekoBot.Modules.Administration commands.Add(new VoicePlusTextCommand(this)); commands.Add(new CrossServerTextChannel(this)); commands.Add(new SelfAssignedRolesCommand(this)); - commands.Add(new Remind(this)); - commands.Add(new InfoCommands(this)); commands.Add(new CustomReactionsCommands(this)); commands.Add(new AutoAssignRole(this)); commands.Add(new SelfCommands(this)); @@ -293,22 +291,6 @@ namespace NadekoBot.Modules.Administration } }); - cgb.CreateCommand(Prefix + "roles") - .Description("List all roles on this server or a single user if specified.") - .Parameter("user", ParameterType.Unparsed) - .Do(async e => - { - if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) - { - var usr = e.Server.FindUsers(e.GetArg("user")).FirstOrDefault(); - if (usr == null) return; - - await e.Channel.SendMessage($"`List of roles for **{usr.Name}**:` \n• " + string.Join("\n• ", usr.Roles)).ConfigureAwait(false); - return; - } - await e.Channel.SendMessage("`List of roles:` \n• " + string.Join("\n• ", e.Server.Roles)).ConfigureAwait(false); - }); - cgb.CreateCommand(Prefix + "ban").Alias(Prefix + "b") .Parameter("user", ParameterType.Required) .Parameter("msg", ParameterType.Unparsed) @@ -630,40 +612,6 @@ namespace NadekoBot.Modules.Administration await e.Channel.SendMessage(":ok: **New channel name set.**").ConfigureAwait(false); }); - cgb.CreateCommand(Prefix + "userid").Alias(Prefix + "uid") - .Description("Shows user ID.") - .Parameter("user", ParameterType.Unparsed) - .Do(async e => - { - var usr = e.User; - if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); - if (usr == null) - return; - await e.Channel.SendMessage($"Id of the user { usr.Name } is { usr.Id }").ConfigureAwait(false); - }); - - cgb.CreateCommand(Prefix + "channelid").Alias(Prefix + "cid") - .Description("Shows current channel ID.") - .Do(async e => await e.Channel.SendMessage("This channel's ID is " + e.Channel.Id).ConfigureAwait(false)); - - cgb.CreateCommand(Prefix + "serverid").Alias(Prefix + "sid") - .Description("Shows current server ID.") - .Do(async e => await e.Channel.SendMessage("This server's ID is " + e.Server.Id).ConfigureAwait(false)); - - cgb.CreateCommand(Prefix + "stats") - .Description("Shows some basic stats for Nadeko.") - .Do(async e => - { - await e.Channel.SendMessage(await NadekoStats.Instance.GetStats()); - }); - - cgb.CreateCommand(Prefix + "dysyd") - .Description("Shows some basic stats for Nadeko.") - .Do(async e => - { - await e.Channel.SendMessage((await NadekoStats.Instance.GetStats()).Matrix().TrimTo(1990)).ConfigureAwait(false); - }); - cgb.CreateCommand(Prefix + "heap") .Description("Shows allocated memory - **Bot Owner Only!**") .AddCheck(SimpleCheckers.OwnerOnly()) @@ -797,19 +745,6 @@ namespace NadekoBot.Modules.Administration client.SetGame(e.GetArg("set_game")); }); - cgb.CreateCommand(Prefix + "checkmyperms") - .Description("Checks your userspecific permissions on this channel.") - .Do(async e => - { - var output = "```\n"; - foreach (var p in e.User.ServerPermissions.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any())) - { - output += p.Name + ": " + p.GetValue(e.User.ServerPermissions, null).ToString() + "\n"; - } - output += "```"; - await e.User.SendMessage(output).ConfigureAwait(false); - }); - cgb.CreateCommand(Prefix + "send") .Description("Send a message to someone on a different server through the bot. **Bot Owner Only!**\n**Usage**: `.send serverid|u:user_id Send this to a user!` or `.send serverid|c:channel_id Send this to a channel!`") .Parameter("ids", ParameterType.Required) @@ -889,37 +824,6 @@ namespace NadekoBot.Modules.Administration }).ConfigureAwait(false); }); - cgb.CreateCommand(Prefix + "inrole") - .Description("Lists every person from the provided role or roles (separated by a ',') on this server.") - .Parameter("roles", ParameterType.Unparsed) - .Do(async e => - { - await Task.Run(async () => - { - if (!e.User.ServerPermissions.MentionEveryone) return; - var arg = e.GetArg("roles").Split(',').Select(r => r.Trim()); - string send = $"`Here is a list of users in a specfic role:`"; - foreach (var roleStr in arg.Where(str => !string.IsNullOrWhiteSpace(str))) - { - var role = e.Server.FindRoles(roleStr).FirstOrDefault(); - if (role == null) continue; - send += $"\n`{role.Name}`\n"; - send += string.Join(", ", role.Members.Select(r => "**" + r.Name + "**#" + r.Discriminator)); - } - - while (send.Length > 2000) - { - var curstr = send.Substring(0, 2000); - await - e.Channel.Send(curstr.Substring(0, - curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1)).ConfigureAwait(false); - send = curstr.Substring(curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1) + - send.Substring(2000); - } - await e.Channel.Send(send).ConfigureAwait(false); - }).ConfigureAwait(false); - }); - cgb.CreateCommand(Prefix + "unstuck") .Description("Clears the message queue. **Bot Owner Only!**") .AddCheck(SimpleCheckers.OwnerOnly()) @@ -982,27 +886,6 @@ namespace NadekoBot.Modules.Administration await e.Channel.SendMessage(":ok:").ConfigureAwait(false); }); - cgb.CreateCommand(Prefix + "whoplays") - .Description("Shows a list of users who are playing the specified game.") - .Parameter("game", ParameterType.Unparsed) - .Do(async e => - { - var game = e.GetArg("game")?.Trim().ToUpperInvariant(); - if (string.IsNullOrWhiteSpace(game)) - return; - var en = e.Server.Users - .Where(u => u.CurrentGame?.Name?.ToUpperInvariant() == game) - .Select(u => u.Name); - - var arr = en as string[] ?? en.ToArray(); - - int i = 0; - if (arr.Length == 0) - await e.Channel.SendMessage("Nobody. (not 100% sure)").ConfigureAwait(false); - else - await e.Channel.SendMessage("```xl\n" + string.Join("\n", arr.GroupBy(item => (i++) / 3).Select(ig => string.Join("", ig.Select(el => $"• {el,-35}")))) + "\n```").ConfigureAwait(false); - }); - cgb.CreateCommand(Prefix + "leave") .Description("Leaves a server with a supplied ID.\n**Usage**: `.leave 493243292839`") .Parameter("num", ParameterType.Required) diff --git a/NadekoBot/Modules/Administration/Commands/InfoCommands.cs b/NadekoBot/Modules/Utility/Commands/InfoCommands.cs similarity index 99% rename from NadekoBot/Modules/Administration/Commands/InfoCommands.cs rename to NadekoBot/Modules/Utility/Commands/InfoCommands.cs index 6d661285..4447537a 100644 --- a/NadekoBot/Modules/Administration/Commands/InfoCommands.cs +++ b/NadekoBot/Modules/Utility/Commands/InfoCommands.cs @@ -6,7 +6,7 @@ using System; using System.Linq; using System.Text; -namespace NadekoBot.Modules.Administration.Commands +namespace NadekoBot.Modules.Utility.Commands { class InfoCommands : DiscordCommand { diff --git a/NadekoBot/Modules/Administration/Commands/Remind.cs b/NadekoBot/Modules/Utility/Commands/Remind.cs similarity index 99% rename from NadekoBot/Modules/Administration/Commands/Remind.cs rename to NadekoBot/Modules/Utility/Commands/Remind.cs index 8e171af5..0ce58d6f 100644 --- a/NadekoBot/Modules/Administration/Commands/Remind.cs +++ b/NadekoBot/Modules/Utility/Commands/Remind.cs @@ -9,7 +9,7 @@ using System.Linq; using System.Text.RegularExpressions; using System.Timers; -namespace NadekoBot.Modules.Administration.Commands +namespace NadekoBot.Modules.Utility.Commands { class Remind : DiscordCommand { diff --git a/NadekoBot/Modules/Utility/UtilityModule.cs b/NadekoBot/Modules/Utility/UtilityModule.cs new file mode 100644 index 00000000..6d510a25 --- /dev/null +++ b/NadekoBot/Modules/Utility/UtilityModule.cs @@ -0,0 +1,151 @@ +using Discord.Commands; +using Discord.Modules; +using NadekoBot.Extensions; +using NadekoBot.Modules.Permissions.Classes; +using NadekoBot.Modules.Utility.Commands; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace NadekoBot.Modules.Utility +{ + internal class UtilityModule : DiscordModule + { + public UtilityModule() + { + commands.Add(new Remind(this)); + commands.Add(new InfoCommands(this)); + } + + public override string Prefix => NadekoBot.Config.CommandPrefixes.Utility; + + public override void Install(ModuleManager manager) + { + + manager.CreateCommands("", cgb => + { + cgb.AddCheck(PermissionChecker.Instance); + + var client = manager.Client; + + commands.ForEach(cmd => cmd.Init(cgb)); + + cgb.CreateCommand(Prefix + "whoplays") + .Description("Shows a list of users who are playing the specified game.") + .Parameter("game", ParameterType.Unparsed) + .Do(async e => + { + var game = e.GetArg("game")?.Trim().ToUpperInvariant(); + if (string.IsNullOrWhiteSpace(game)) + return; + var en = e.Server.Users + .Where(u => u.CurrentGame?.Name?.ToUpperInvariant() == game) + .Select(u => u.Name); + + var arr = en as string[] ?? en.ToArray(); + + int i = 0; + if (arr.Length == 0) + await e.Channel.SendMessage("Nobody. (not 100% sure)").ConfigureAwait(false); + else + await e.Channel.SendMessage("```xl\n" + string.Join("\n", arr.GroupBy(item => (i++) / 3).Select(ig => string.Join("", ig.Select(el => $"• {el,-35}")))) + "\n```").ConfigureAwait(false); + }); + + cgb.CreateCommand(Prefix + "inrole") + .Description("Lists every person from the provided role or roles (separated by a ',') on this server.") + .Parameter("roles", ParameterType.Unparsed) + .Do(async e => + { + await Task.Run(async () => + { + if (!e.User.ServerPermissions.MentionEveryone) return; + var arg = e.GetArg("roles").Split(',').Select(r => r.Trim()); + string send = $"`Here is a list of users in a specfic role:`"; + foreach (var roleStr in arg.Where(str => !string.IsNullOrWhiteSpace(str))) + { + var role = e.Server.FindRoles(roleStr).FirstOrDefault(); + if (role == null) continue; + send += $"\n`{role.Name}`\n"; + send += string.Join(", ", role.Members.Select(r => "**" + r.Name + "**#" + r.Discriminator)); + } + + while (send.Length > 2000) + { + var curstr = send.Substring(0, 2000); + await + e.Channel.Send(curstr.Substring(0, + curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1)).ConfigureAwait(false); + send = curstr.Substring(curstr.LastIndexOf(", ", StringComparison.Ordinal) + 1) + + send.Substring(2000); + } + await e.Channel.Send(send).ConfigureAwait(false); + }).ConfigureAwait(false); + }); + + cgb.CreateCommand(Prefix + "checkmyperms") + .Description("Checks your userspecific permissions on this channel.") + .Do(async e => + { + var output = "```\n"; + foreach (var p in e.User.ServerPermissions.GetType().GetProperties().Where(p => !p.GetGetMethod().GetParameters().Any())) + { + output += p.Name + ": " + p.GetValue(e.User.ServerPermissions, null).ToString() + "\n"; + } + output += "```"; + await e.User.SendMessage(output).ConfigureAwait(false); + }); + + cgb.CreateCommand(Prefix + "stats") + .Description("Shows some basic stats for Nadeko.") + .Do(async e => + { + await e.Channel.SendMessage(await NadekoStats.Instance.GetStats()); + }); + + cgb.CreateCommand(Prefix + "dysyd") + .Description("Shows some basic stats for Nadeko.") + .Do(async e => + { + await e.Channel.SendMessage((await NadekoStats.Instance.GetStats()).Matrix().TrimTo(1990)).ConfigureAwait(false); + }); + + cgb.CreateCommand(Prefix + "userid").Alias(Prefix + "uid") + .Description("Shows user ID.") + .Parameter("user", ParameterType.Unparsed) + .Do(async e => + { + var usr = e.User; + if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); + if (usr == null) + return; + await e.Channel.SendMessage($"Id of the user { usr.Name } is { usr.Id }").ConfigureAwait(false); + }); + + cgb.CreateCommand(Prefix + "channelid").Alias(Prefix + "cid") + .Description("Shows current channel ID.") + .Do(async e => await e.Channel.SendMessage("This channel's ID is " + e.Channel.Id).ConfigureAwait(false)); + + cgb.CreateCommand(Prefix + "serverid").Alias(Prefix + "sid") + .Description("Shows current server ID.") + .Do(async e => await e.Channel.SendMessage("This server's ID is " + e.Server.Id).ConfigureAwait(false)); + + cgb.CreateCommand(Prefix + "roles") + .Description("List all roles on this server or a single user if specified.") + .Parameter("user", ParameterType.Unparsed) + .Do(async e => + { + if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) + { + var usr = e.Server.FindUsers(e.GetArg("user")).FirstOrDefault(); + if (usr == null) return; + + await e.Channel.SendMessage($"`List of roles for **{usr.Name}**:` \n• " + string.Join("\n• ", usr.Roles)).ConfigureAwait(false); + return; + } + await e.Channel.SendMessage("`List of roles:` \n• " + string.Join("\n• ", e.Server.Roles)).ConfigureAwait(false); + }); + }); + } + } +} + diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index d203f30d..df3c8f7d 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -22,6 +22,7 @@ using NadekoBot.Modules.Pokemon; using NadekoBot.Modules.Searches; using NadekoBot.Modules.Translator; using NadekoBot.Modules.Trello; +using NadekoBot.Modules.Utility; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -163,6 +164,7 @@ namespace NadekoBot //install modules modules.Add(new HelpModule(), "Help", ModuleFilter.None); modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None); + modules.Add(new UtilityModule(), "Utility", ModuleFilter.None); modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None); modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None); diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index 23ca1c7c..2f831d00 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -150,6 +150,8 @@ + + @@ -188,8 +190,8 @@ - - + + diff --git a/NadekoBot/_Models/DataModels/Incident.cs b/NadekoBot/_Models/DataModels/Incident.cs new file mode 100644 index 00000000..b1254f72 --- /dev/null +++ b/NadekoBot/_Models/DataModels/Incident.cs @@ -0,0 +1,13 @@ +using System; + +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; + } +} diff --git a/NadekoBot/_Models/JSONModels/Configuration.cs b/NadekoBot/_Models/JSONModels/Configuration.cs index 1b58871a..ad75f35d 100644 --- a/NadekoBot/_Models/JSONModels/Configuration.cs +++ b/NadekoBot/_Models/JSONModels/Configuration.cs @@ -161,6 +161,7 @@ Nadeko Support Server: "; public string Permissions { get; set; } = ";"; public string Programming { get; set; } = "%"; public string Pokemon { get; set; } = ">"; + public string Utility { get; set; } = "."; } public static class ConfigHandler diff --git a/NadekoBot/bin/Debug/data/config_example.json b/NadekoBot/bin/Debug/data/config_example.json index 10f0b6c5..bd094371 100644 --- a/NadekoBot/bin/Debug/data/config_example.json +++ b/NadekoBot/bin/Debug/data/config_example.json @@ -87,7 +87,8 @@ "Gambling": "$", "Permissions": ";", "Programming": "%", - "Pokemon": ">" + "Pokemon": ">", + "Utility": "." }, "ServerBlacklist": [], "ChannelBlacklist": [],