From a39a42ef404c41f2c7f9f7c6711b2941adfc3c47 Mon Sep 17 00:00:00 2001 From: Marrub Date: Fri, 28 Oct 2016 02:48:58 -0400 Subject: [PATCH] Refactoring. Bot: Split into multiple files. Bot: Add roles system to replace BotCommandFlags. BotClient: Split into separate directory. --- Source/{Bot.cs => Bot/Data.cs} | 138 +++++------------- Source/{BotEvents.cs => Bot/Events.cs} | 22 +-- Source/{BotInfo.cs => Bot/Info.cs} | 32 ++-- Source/{BotModule.cs => Bot/Module.cs} | 44 +++--- Source/Bot/PrivateFuncs.cs | 79 ++++++++++ Source/{BotClient.cs => Client/Client.cs} | 35 ++++- .../Discord.cs} | 88 +++++++---- Source/{BotClientIRC.cs => Client/IRC.cs} | 11 +- Source/Exceptions.cs | 13 ++ Source/Modules/Mod_Admin.cs | 6 +- Source/Modules/Mod_Audio.cs | 37 ++--- Source/Modules/Mod_Fun.cs | 51 +++++-- Source/Modules/Mod_Quote.cs | 8 +- Source/Modules/Mod_Utils.cs | 5 +- Source/Program.cs | 14 +- vrobot3.csproj | 23 +-- 16 files changed, 340 insertions(+), 266 deletions(-) rename Source/{Bot.cs => Bot/Data.cs} (61%) rename Source/{BotEvents.cs => Bot/Events.cs} (83%) rename Source/{BotInfo.cs => Bot/Info.cs} (66%) rename Source/{BotModule.cs => Bot/Module.cs} (75%) create mode 100644 Source/Bot/PrivateFuncs.cs rename Source/{BotClient.cs => Client/Client.cs} (63%) rename Source/{BotClientDiscord.cs => Client/Discord.cs} (75%) rename Source/{BotClientIRC.cs => Client/IRC.cs} (77%) diff --git a/Source/Bot.cs b/Source/Bot/Data.cs similarity index 61% rename from Source/Bot.cs rename to Source/Bot/Data.cs index db92b81..636b2cd 100644 --- a/Source/Bot.cs +++ b/Source/Bot/Data.cs @@ -6,7 +6,7 @@ // //----------------------------------------------------------------------------- // -// Main bot class. +// Data for the Bot class. // //----------------------------------------------------------------------------- @@ -14,7 +14,6 @@ using System; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; -using System.IO; using CommandFuncDict = System.Collections.Generic.Dictionary< @@ -44,27 +43,31 @@ namespace ProjectGolan.Vrobot3 // public partial class Bot { - public List modules = new List(); - public CommandFuncDict cmdfuncs = new CommandFuncDict(); + private readonly Dictionary lastLine; + private readonly IBotClient client; + + public List modules { get; private set; } + public CommandFuncDict cmdfuncs { get; private set; } public readonly BotInfo info; - private Dictionary lastLine = new Dictionary(); - private IBotClient client; - - public bool isInAudioChannel => false; - public ServerInfo serverInfo => client.info; + public bool isInAudioChannel => client.isInAudioChannel(); + public BotClientInfo clientInfo => client.info; // // Bot constructor // public Bot(BotInfo info) { - this.info = info; + this.info = info; + this.lastLine = new Dictionary(); + this.modules = new List(); + this.cmdfuncs = new CommandFuncDict(); switch(info.serverType) { - case ServerType.IRC: client = new BotClientIRC(this); break; - case ServerType.Discord: client = new BotClientDiscord(this); break; + case "IRC": this.client = new BotClientIRC(this); break; + case "Discord": this.client = new BotClientDiscord(this); break; + default: throw new BotConfigurationException("Invalid server type."); } var modClasses = @@ -82,10 +85,33 @@ namespace ProjectGolan.Vrobot3 } // - // connect + // client // public void connect() => client.connect(); + public void action(Channel channel, String msg) => + client.sendAction(channel, msg); + public void action(ulong id, String msg) => + client.sendAction(client.getChannel(id), msg); + + public void message(Channel channel, String msg) => + client.sendMessage(channel, msg); + public void message(ulong id, String msg) => + client.sendMessage(client.getChannel(id), msg); + + public void messageRaw(Channel channel, String msg) => + client.sendMessageRaw(channel, msg); + public void messageRaw(ulong id, String msg) => + client.sendMessageRaw(client.getChannel(id), msg); + + public void reply(User usr, Channel channel, String msg) => + message(channel, usr.name + ": " + msg); + public void reply(User usr, ulong id, String msg) => + message(id, usr.name + ": " + msg); + + public void partAudioChannel() => client.partAudioChannel(); + public Task playAudioFile(String file) => client.playAudioFile(file); + // // disconnect // @@ -96,30 +122,6 @@ namespace ProjectGolan.Vrobot3 client.disconnect(); } - // - // reply - // - public void reply(User usr, Channel channel, String msg) => - message(channel, usr.name + ": " + msg); - public void reply(User usr, ulong id, String msg) => - message(id, usr.name + ": " + msg); - - // - // message - // - public void message(Channel channel, String msg) => - client.sendMessage(channel, msg); - public void message(ulong id, String msg) => - client.sendMessage(client.getChannel(id), msg); - - // - // action - // - public void action(Channel channel, String msg) => - client.sendAction(channel, msg); - public void action(ulong id, String msg) => - client.sendAction(client.getChannel(id), msg); - // // joinAudioChannel // @@ -135,16 +137,6 @@ namespace ProjectGolan.Vrobot3 return false; } - // - // partAudioChannel - // - public void partAudioChannel() => client.partAudioChannel(); - - // - // playAudioFile - // - public Task playAudioFile(String file) => client.playAudioFile(file); - // // checkModPermissions // @@ -176,58 +168,6 @@ namespace ProjectGolan.Vrobot3 return false; } - - // - // runCommand - // - private void runCommand(User usr, Channel channel, BotCommand cmd, - String rest) - { - try - { - cmd(usr, channel, rest ?? String.Empty); - } - catch(CommandArgumentException exc) - { - if(exc.Message != null) - reply(usr, channel, exc.Message); - else - Console.WriteLine("{0}: Unknown CommandArgumentException", - info.serverName); - } - catch(Exception exc) - { - reply(usr, channel, "fug it borked"); - Console.WriteLine("{0}: Unhandled exception in command: {1}", - info.serverName, exc?.Message ?? "unknown error"); - File.WriteAllText(Program.Instance.dataDir + "/cmdexcdump.txt", - exc.ToString()); - } - } - - // - // moduleIsValid - // - private bool moduleIsValid(Type type) - { - if(!typeof(IBotModule).IsAssignableFrom(type) || - !type.IsClass || type.IsAbstract) - return false; - - foreach(var attribute in type.GetCustomAttributes(false)) - { - if((attribute is BotModuleIRCAttribute && - info.serverType != ServerType.IRC) || - (attribute is BotModuleDiscordAttribute && - info.serverType != ServerType.Discord) || - (attribute is BotModuleRequiresAudioAttribute && - !serverInfo.hasAudio) || - attribute is BotModuleDisabledAttribute) - return false; - } - - return true; - } } } diff --git a/Source/BotEvents.cs b/Source/Bot/Events.cs similarity index 83% rename from Source/BotEvents.cs rename to Source/Bot/Events.cs index e8699d1..2900531 100644 --- a/Source/BotEvents.cs +++ b/Source/Bot/Events.cs @@ -11,7 +11,6 @@ //----------------------------------------------------------------------------- using System; -using System.Collections.Generic; using System.Linq; namespace ProjectGolan.Vrobot3 @@ -38,23 +37,25 @@ namespace ProjectGolan.Vrobot3 // public void onMessage(User usr, Channel channel, String msg) { - var validCmdPreceders = ".%".ToCharArray(); + var validCmdPreceders = ".%$".ToCharArray(); String rest = null; if(msg.Length >= 1 && validCmdPreceders.Contains(msg[0])) { Predicate pred; - if(msg[0] == '%') - pred = fn => fn.flags.HasFlag(BotCommandFlags.AdminOnly); - else - pred = fn => !fn.flags.HasFlag(BotCommandFlags.AdminOnly); + switch(msg[0]) + { + case '%': pred = fn => fn.role == BotRole.Admin; break; + case '$': pred = fn => fn.role == BotRole.HalfAdmin; break; + default: pred = fn => fn.role == BotRole.User; break; + } var dict = from fn in cmdfuncs where pred(fn.Value.Item2) select fn; // Get the command name. - String[] splt = msg.Substring(1).Split(" ".ToCharArray(), 2); - String cmdname = splt[0].ToLower(); + var splt = msg.Substring(1).Split(" ".ToCharArray(), 2); + var cmdname = splt[0].ToLower(); // Handle commands ending with "^". // These take the last message as input. @@ -71,9 +72,8 @@ namespace ProjectGolan.Vrobot3 var tcmdr = tcmd.First(); // Check permissions. - if(usr.hostname != info.adminId && - (tcmdr.Item2.flags.HasFlag(BotCommandFlags.AdminOnly) || - !checkModPermissions(channel, tcmdr.Item1.GetType()))) + if(!client.userPermitted(usr, tcmdr.Item2.role) || + !checkModPermissions(channel, tcmdr.Item1.GetType())) goto RaiseMessage; // If we have input, grab that too. diff --git a/Source/BotInfo.cs b/Source/Bot/Info.cs similarity index 66% rename from Source/BotInfo.cs rename to Source/Bot/Info.cs index e72dd5f..f8be0f1 100644 --- a/Source/BotInfo.cs +++ b/Source/Bot/Info.cs @@ -16,24 +16,12 @@ using System.Collections.Generic; namespace ProjectGolan.Vrobot3 { // - // ServerType + // RoleInfo // - public enum ServerType + public struct RoleInfo { - IRC, - Discord - } - - // - // ServerInfo - // - public struct ServerInfo - { - public bool hasAudio; - public bool hasColors; - public bool hasNewlines; - public int messageSafeMaxLen; - public bool shortMessages; + public String[] admin; + public String[] halfadmin; } // @@ -42,12 +30,12 @@ namespace ProjectGolan.Vrobot3 public struct BotInfo { public Dictionary enables; - public ServerType serverType; - public String serverName; - public String serverPass; - public String serverAddr; - public String adminId; - public String[] channels; + public String serverType; + public String serverName; + public String serverPass; + public String serverAddr; + public String[] channels; + public RoleInfo roles; } // diff --git a/Source/BotModule.cs b/Source/Bot/Module.cs similarity index 75% rename from Source/BotModule.cs rename to Source/Bot/Module.cs index ce2f1c1..e71c6c3 100644 --- a/Source/BotModule.cs +++ b/Source/Bot/Module.cs @@ -54,15 +54,15 @@ namespace ProjectGolan.Vrobot3 } // - // BotCommandFlags + // BotRole // - // Flags for command registration. + // Used for command role-checking. // - [Flags] - public enum BotCommandFlags + public enum BotRole { - AdminOnly = 1 << 0, - Hidden = 1 << 1 + User, + HalfAdmin, + Admin } // @@ -73,8 +73,9 @@ namespace ProjectGolan.Vrobot3 public struct BotCommandStructure { public BotCommand cmd; - public BotCommandFlags flags; - public String help; + public String help; + public bool hidden; + public BotRole role; } // @@ -93,30 +94,21 @@ namespace ProjectGolan.Vrobot3 public event Modules.EventType.OnMessage onMessage; public event Modules.EventType.OnSeen onSeen; - public void raiseOnCmdMessage(User usr, Channel channel, String msg) - { - if(onCmdMessage != null) - onCmdMessage(usr, channel, msg); - } + public void raiseOnCmdMessage(User usr, Channel channel, String msg) => + onCmdMessage?.Invoke(usr, channel, msg); - public void raiseOnMessage(User usr, Channel channel, String msg) - { - if(onMessage != null) - onMessage(usr, channel, msg); - } + public void raiseOnMessage(User usr, Channel channel, String msg) => + onMessage?.Invoke(usr, channel, msg); - public void raiseOnSeen(User usr, Channel channel) - { - if(onSeen != null) - onSeen(usr, channel); - } + public void raiseOnSeen(User usr, Channel channel) => + onSeen?.Invoke(usr, channel); } - public IBotModule(Bot bot) { this.bot = bot; } + protected IBotModule(Bot bot) { this.bot = bot; } public CommandDict commands = new CommandDict(); - public Events events; - protected Bot bot; + public Events events; + protected Bot bot; } } diff --git a/Source/Bot/PrivateFuncs.cs b/Source/Bot/PrivateFuncs.cs new file mode 100644 index 0000000..87a845f --- /dev/null +++ b/Source/Bot/PrivateFuncs.cs @@ -0,0 +1,79 @@ +//----------------------------------------------------------------------------- +// +// Copyright © 2016 Project Golan +// +// See "LICENSE" for more information. +// +//----------------------------------------------------------------------------- +// +// Private functions for Bot. +// +//----------------------------------------------------------------------------- + +using System; +using System.IO; + +namespace ProjectGolan.Vrobot3 +{ + // + // Bot + // + public partial class Bot + { + // + // runCommand + // + private void runCommand(User usr, Channel channel, BotCommand cmd, + String rest) + { + try + { + cmd(usr, channel, rest ?? String.Empty); + } + catch(CommandArgumentException exc) + { + reply(usr, channel, exc.Message); + } + catch(Exception exc) + { + reply(usr, channel, "fug it borked"); + Console.WriteLine("{0}: Unhandled exception in command: {1}", + info.serverName, exc.Message); + File.WriteAllText(Program.Instance.dataDir + "/cmdexcdump.txt", + exc.ToString()); + } + } + + // + // moduleIsValid + // + private bool moduleIsValid(Type type) + { + if(!typeof(IBotModule).IsAssignableFrom(type) || + !type.IsClass || type.IsAbstract) + return false; + + foreach(var attribute in type.GetCustomAttributes(false)) + { + if(attribute is BotModuleDisabledAttribute) + return false; + + if(attribute is BotModuleIRCAttribute && + !(client is BotClientIRC)) + return false; + + if(attribute is BotModuleDiscordAttribute && + !(client is BotClientDiscord)) + return false; + + if(attribute is BotModuleRequiresAudioAttribute && + !clientInfo.hasAudio) + return false; + } + + return true; + } + } +} + +// EOF diff --git a/Source/BotClient.cs b/Source/Client/Client.cs similarity index 63% rename from Source/BotClient.cs rename to Source/Client/Client.cs index 21c7dc6..ee9bf52 100644 --- a/Source/BotClient.cs +++ b/Source/Client/Client.cs @@ -15,31 +15,52 @@ using System.Threading.Tasks; namespace ProjectGolan.Vrobot3 { + // + // BotClientInfo + // + public class BotClientInfo + { + public bool hasAudio; + public bool hasColors; + public bool hasNewlines; + public int messageSafeMaxLen; + public bool shortMessages; + } + // // IBotClient // public abstract class IBotClient { protected Bot bot; - public ServerInfo info; + public BotClientInfo info { get; protected set; } - public IBotClient(Bot bot) { this.bot = bot; } + protected IBotClient(Bot bot) + { + this.info = new BotClientInfo(); + this.bot = bot; + } - // Connect + // connect public abstract void connect(); public abstract void disconnect(); - // Send + // send public abstract void sendAction(Channel channel, String msg); public abstract void sendMessage(Channel channel, String msg); + public virtual void sendMessageRaw(Channel channel, String msg) => + sendMessage(channel, msg); - // Channel + // channel public abstract Channel getChannel(ulong id); public virtual void joinChannel(Channel channel) {} public virtual void partChannel(Channel channel) {} - // Audio - public virtual ChannelAudio getAudioChannel(User user) => + // user + public abstract bool userPermitted(User usr, BotRole role); + + // audio + public virtual ChannelAudio getAudioChannel(User usr) => new ChannelAudio(); public virtual async Task joinAudioChannel(ChannelAudio channel) => await Task.FromResult(0); diff --git a/Source/BotClientDiscord.cs b/Source/Client/Discord.cs similarity index 75% rename from Source/BotClientDiscord.cs rename to Source/Client/Discord.cs index 8b9c483..31a2add 100644 --- a/Source/BotClientDiscord.cs +++ b/Source/Client/Discord.cs @@ -24,27 +24,9 @@ namespace ProjectGolan.Vrobot3 // public class BotClientDiscord : IBotClient { - // - // AudioBuffer - // - class AudioBuffer - { -// private static const int MaxSize = 20 * 1024 * 1024; - private readonly String name; - private ulong num = 0; - private ulong next = 1; - private ulong bufSize = 0; - public bool completed { get; private set; } = false; - - public AudioBuffer() - { - name = System.IO.Path.GetRandomFileName() + "."; - } - } - - private Discord.DiscordClient client = new Discord.DiscordClient(); + private Discord.DiscordClient client; private Discord.Audio.IAudioClient audioClient; - private Discord.Server server; + private Discord.Server server; // // BotClientDiscord constructor @@ -52,13 +34,15 @@ namespace ProjectGolan.Vrobot3 public BotClientDiscord(Bot bot) : base(bot) { - info.hasAudio = true; - info.hasColors = false; - info.hasNewlines = true; - info.messageSafeMaxLen = 1777; - info.shortMessages = false; + this.client = new Discord.DiscordClient(); - client.MessageReceived += (sender, evt) => + this.info.hasAudio = true; + this.info.hasColors = false; + this.info.hasNewlines = true; + this.info.messageSafeMaxLen = 1777; + this.info.shortMessages = false; + + this.client.MessageReceived += (sender, evt) => { if(!evt.Message.IsAuthor && !evt.User.IsBot && (bot.info.channels == null || @@ -78,7 +62,7 @@ namespace ProjectGolan.Vrobot3 } }; - client.UsingAudio(x => x.Mode = AudioMode.Outgoing); + this.client.UsingAudio(x => x.Mode = AudioMode.Outgoing); } // @@ -91,6 +75,33 @@ namespace ProjectGolan.Vrobot3 return server.GetUser(ulong.Parse(usr.hostname)); } + // + // checkRole + // + private bool checkRole(User usr, String[] strings) + { + var duser = getUser(usr); + + foreach(var str in strings) + { + if(str[0] == '#') + { + var sel = + from role in duser.Roles + let strn = str.Substring(1) + where role.Name == strn + select role; + + if(sel.Any()) + return true; + } + else if(usr.hostname == str) + return true; + } + + return false; + } + // // connect // @@ -130,6 +141,13 @@ namespace ProjectGolan.Vrobot3 public override void sendMessage(Channel channel, String msg) => client.GetChannel(channel.id)?.SendMessage(Discord.Format.Escape(msg)); + // + // sendMessageRaw + // + public override void sendMessageRaw(Channel channel, String msg) => + sendMessage(channel, + "```" + Discord.Format.Escape(msg ?? String.Empty) + "```"); + // // getChannel // @@ -142,6 +160,14 @@ namespace ProjectGolan.Vrobot3 return channel; } + // + // userPermitted + // + public override bool userPermitted(User usr, BotRole role) => + role == BotRole.User || + (role == BotRole.HalfAdmin && checkRole(usr, bot.info.roles.halfadmin)) || + (role == BotRole.Admin && checkRole(usr, bot.info.roles.admin)); + // // getAudioChannel // @@ -209,12 +235,12 @@ namespace ProjectGolan.Vrobot3 var buf = new byte[3840 * 32]; var ostream = audioClient.OutputStream; - var istream = proc.StandardOutput.BaseStream; + var istream = proc?.StandardOutput.BaseStream; - int count; try { - while(!proc.HasExited && + int count; + while(proc?.HasExited == false && (count = await istream.ReadAsync(buf, 0, buf.Length)) != 0) { Thread.Sleep(8); @@ -228,7 +254,7 @@ namespace ProjectGolan.Vrobot3 } finally { - istream.Dispose(); + istream?.Dispose(); ostream.Dispose(); } } diff --git a/Source/BotClientIRC.cs b/Source/Client/IRC.cs similarity index 77% rename from Source/BotClientIRC.cs rename to Source/Client/IRC.cs index 1cb74a0..270e260 100644 --- a/Source/BotClientIRC.cs +++ b/Source/Client/IRC.cs @@ -25,11 +25,11 @@ namespace ProjectGolan.Vrobot3 public BotClientIRC(Bot bot) : base(bot) { - info.hasAudio = false; - info.hasColors = true; - info.hasNewlines = false; - info.messageSafeMaxLen = 601; - info.shortMessages = true; + this.info.hasAudio = false; + this.info.hasColors = true; + this.info.hasNewlines = false; + this.info.messageSafeMaxLen = 601; + this.info.shortMessages = true; } public override void connect() {} @@ -39,6 +39,7 @@ namespace ProjectGolan.Vrobot3 public override void partChannel(Channel channel) {} public override void sendAction(Channel channel, String msg) {} public override void sendMessage(Channel channel, String msg) {} + public override bool userPermitted(User usr, BotRole role) => true; } } diff --git a/Source/Exceptions.cs b/Source/Exceptions.cs index ee0dee8..418ad4a 100644 --- a/Source/Exceptions.cs +++ b/Source/Exceptions.cs @@ -26,6 +26,19 @@ namespace ProjectGolan.Vrobot3 { } } + + // + // BotConfigurationException + // + public class BotConfigurationException : Exception + { + public BotConfigurationException() {} + public BotConfigurationException(String message) : base(message) {} + public BotConfigurationException(String message, Exception inner) : + base(message, inner) + { + } + } } // EOF diff --git a/Source/Modules/Mod_Admin.cs b/Source/Modules/Mod_Admin.cs index 3e7fbf5..ea078b3 100644 --- a/Source/Modules/Mod_Admin.cs +++ b/Source/Modules/Mod_Admin.cs @@ -29,14 +29,14 @@ namespace ProjectGolan.Vrobot3.Modules { commands["kill"] = new BotCommandStructure{ cmd = cmdKill, - flags = BotCommandFlags.AdminOnly, + role = BotRole.Admin, help = "Kills all bot instances.\n" + "Syntax: %kill" }; commands["msg"] = new BotCommandStructure{ cmd = cmdMsg, - flags = BotCommandFlags.AdminOnly, + role = BotRole.Admin, help = "Sends a message.\n" + "Syntax: %msg channel, msg\n" + "Example: %msg #general, ur all dumb" @@ -44,7 +44,7 @@ namespace ProjectGolan.Vrobot3.Modules commands["action"] = new BotCommandStructure{ cmd = cmdAction, - flags = BotCommandFlags.AdminOnly, + role = BotRole.Admin, help = "Sends an action.\n" + "Syntax: %action channel, msg\n" + "Example: %action #general, explodes violently" diff --git a/Source/Modules/Mod_Audio.cs b/Source/Modules/Mod_Audio.cs index af0c681..80e1561 100644 --- a/Source/Modules/Mod_Audio.cs +++ b/Source/Modules/Mod_Audio.cs @@ -29,7 +29,7 @@ namespace ProjectGolan.Vrobot3.Modules // class QueueItem { - Utils.URI uri; + private Utils.URI uri; public QueueItem(Utils.URI uri) { @@ -44,9 +44,9 @@ namespace ProjectGolan.Vrobot3.Modules // class Queue { - TimeSpan curTime; - List items; - int pos; + private TimeSpan curTime; + private List items; + private int pos; public Queue() { @@ -63,9 +63,9 @@ namespace ProjectGolan.Vrobot3.Modules } } - String[] validMethods = { "http", "https", "ftp", "ftps" }; - Random rnd = Utils.GetRND(); - Queue queue = new Queue(); + private readonly String[] validMethods = + { "http", "https", "ftp", "ftps" }; + private Queue queue = new Queue(); // // Mod_Audio constructor @@ -95,12 +95,6 @@ namespace ProjectGolan.Vrobot3.Modules "Syntax: .lsqueue" }; - commands["fugoff"] = new BotCommandStructure{ - cmd = cmdFugOff, - help = "GET ME COGS OR FUG OFF", - flags = BotCommandFlags.AdminOnly - }; - commands["summon"] = new BotCommandStructure{ cmd = cmdSummon, help = "Makes the bot join your audio channel.\n" + @@ -111,14 +105,14 @@ namespace ProjectGolan.Vrobot3.Modules cmd = cmdVanquish, help = "Makes the bot leave their audio channel.\n" + "Syntax: %vanquish", - flags = BotCommandFlags.AdminOnly + role = BotRole.HalfAdmin }; } // // summon // - async Task summon(User usr, Channel channel) + private async Task summon(User usr, Channel channel) { if(bot.isInAudioChannel) return true; @@ -158,7 +152,7 @@ namespace ProjectGolan.Vrobot3.Modules } bot.reply(usr, channel, - $"Added {loadPass} item{loadPass == 1 ? "" : "s"} to the queue."); + $"Added {loadPass} item{loadPass != 1 ? "s" : ""} to the queue."); } // @@ -175,17 +169,6 @@ namespace ProjectGolan.Vrobot3.Modules { } - // - // cmdFugOff - // - public async void cmdFugOff(User usr, Channel channel, String msg) - { - if(!await summon(usr, channel)) - return; - - await bot.playAudioFile("\"/home/marrub/musix/MusixDL/Shadowfax - Shadowdance/01 New Electric India.mp3\"").ConfigureAwait(false); - } - // // cmdSummon // diff --git a/Source/Modules/Mod_Fun.cs b/Source/Modules/Mod_Fun.cs index 494b4d4..e484607 100644 --- a/Source/Modules/Mod_Fun.cs +++ b/Source/Modules/Mod_Fun.cs @@ -12,6 +12,7 @@ //----------------------------------------------------------------------------- using System; +using System.Linq; namespace ProjectGolan.Vrobot3.Modules { @@ -74,30 +75,60 @@ namespace ProjectGolan.Vrobot3.Modules public Mod_Fun(Bot bot) : base(bot) { - commands["carmack"] = new BotCommandStructure{ + commands["carmack"] = new BotCommandStructure { cmd = new ShitpostingDevice("MM", "", 3, 20, bot).run, - flags = BotCommandFlags.Hidden + hidden = true }; - commands["revenant"] = new BotCommandStructure{ + + commands["revenant"] = new BotCommandStructure { cmd = new ShitpostingDevice("AA", "", 3, 20, bot).run, - flags = BotCommandFlags.Hidden + hidden = true }; - commands["wan"] = new BotCommandStructure{ + + commands["wan"] = new BotCommandStructure { cmd = new ShitpostingDevice("wan ", "- !", 2, 12, bot).run, - flags = BotCommandFlags.Hidden + hidden = true }; - commands["nyan"] = new BotCommandStructure{ + + commands["nyan"] = new BotCommandStructure { cmd = new ShitpostingDevice("nyan ", "!~", 2, 12, bot).run, - flags = BotCommandFlags.Hidden + hidden = true }; - commands[":^)"] = new BotCommandStructure{ + + commands[":^)"] = new BotCommandStructure { cmd = (usr, channel, msg) => bot.message(channel, ":^)"), - flags = BotCommandFlags.Hidden + hidden = true }; +// commands["box"] = new BotCommandStructure { +// cmd = cmdBox, +// hidden = true, +// role = BotRole.HalfAdmin +// }; + events.onMessage += onMessage; } + // + // cmdBox + // +// public void cmdBox(User usr, Channel channel, String msg) +// { +// var outp = msg + '\n'; +// +// for(var i = 1; i < msg.Length; i++) +// { +// var ln = msg[i].ToString(); +// ln = ln.PadRight(msg.Length); +// ln += msg[msg.Length - i]; +// outp += ln + '\n'; +// } +// +// outp += msg.Reverse(); +// +// bot.messageRaw(channel, outp); +// } + // // onMessage // diff --git a/Source/Modules/Mod_Quote.cs b/Source/Modules/Mod_Quote.cs index ad89aea..bed493d 100644 --- a/Source/Modules/Mod_Quote.cs +++ b/Source/Modules/Mod_Quote.cs @@ -63,24 +63,24 @@ namespace ProjectGolan.Vrobot3.Modules throw new CommandArgumentException("invalid quote ID"); var quote = Utils.GetResponseString(APIURI + id.ToString(), - bot.serverInfo.messageSafeMaxLen); + bot.clientInfo.messageSafeMaxLen); if(String.IsNullOrEmpty(quote)) throw new CommandArgumentException("QDB exploded try again later"); - if(bot.serverInfo.shortMessages) + if(bot.clientInfo.shortMessages) quote = Regex.Replace(quote, "\n+", "\n").Trim(); var lines = quote.Split('\n'); - if(bot.serverInfo.shortMessages && + if(bot.clientInfo.shortMessages && (lines.Length > 5 || quote.Length > 600)) { bot.reply(usr, channel, "Quote is too long."); return; } - if(bot.serverInfo.hasNewlines) + if(bot.clientInfo.hasNewlines) bot.message(channel, quote); else foreach(var ln_ in lines) diff --git a/Source/Modules/Mod_Utils.cs b/Source/Modules/Mod_Utils.cs index 4d17984..f5d8f4f 100644 --- a/Source/Modules/Mod_Utils.cs +++ b/Source/Modules/Mod_Utils.cs @@ -114,9 +114,8 @@ namespace ProjectGolan.Vrobot3.Modules var outp = String.Empty; var en = from kvp in bot.cmdfuncs - let f = kvp.Value.Item2.flags - let fhidden = f.HasFlag(BotCommandFlags.Hidden) - let fadmin = f.HasFlag(BotCommandFlags.AdminOnly) + let fhidden = kvp.Value.Item2.hidden + let fadmin = kvp.Value.Item2.role != BotRole.User where bot.checkModPermissions(channel, this.GetType()) && (admin || !fadmin) && !fhidden diff --git a/Source/Program.cs b/Source/Program.cs index 148ecea..dba2de9 100644 --- a/Source/Program.cs +++ b/Source/Program.cs @@ -40,13 +40,6 @@ namespace ProjectGolan.Vrobot3 public BotInfo[] servers; } - private readonly List bots = new List(); - private readonly List threads = new List(); - public String dataDir = "../data"; - public ProgramInfo info; - - public static Program Instance; - // // Main // @@ -95,6 +88,13 @@ namespace ProjectGolan.Vrobot3 bots.Clear(); threads.Clear(); } + + private readonly List bots = new List(); + private readonly List threads = new List(); + public String dataDir = "../data"; + public ProgramInfo info; + + public static Program Instance; } } diff --git a/vrobot3.csproj b/vrobot3.csproj index fd4ddc8..1e3f7ca 100644 --- a/vrobot3.csproj +++ b/vrobot3.csproj @@ -70,17 +70,14 @@ - - - - - - - - - - - + + + + + + + + @@ -88,6 +85,10 @@ + + + +