public ChatFilter(IServiceProvider service) { _client = service.GetRequiredService <DiscordShardedClient>(); modCommands = new ModCommands(service); // New instance of the ModCommands class. _client.MessageReceived += CheckForCensoredWords; _client.MessageReceived += CheckForPingSpam; _client.MessageReceived += CheckForLinks; }
public static Command CreateInstance(ModCommands root, JProperty def) { string label = def.Name; if (string.IsNullOrWhiteSpace(label)) { throw new RuleImportException("Label cannot be blank."); } var definition = (JObject)def.Value; string cmdinvoke = definition["command"]?.Value <string>(); if (string.IsNullOrWhiteSpace(cmdinvoke)) { throw new RuleImportException($"{label}: 'command' value was not specified."); } if (cmdinvoke.Contains(" ")) { throw new RuleImportException($"{label}: 'command' must not contain spaces."); } string ctypestr = definition["type"]?.Value <string>(); if (string.IsNullOrWhiteSpace(ctypestr)) { throw new RuleImportException($"Value 'type' must be specified in definition for '{label}'."); } if (_commands.TryGetValue(ctypestr, out Type ctype)) { try { return((Command)Activator.CreateInstance(ctype, root, label, definition)); } catch (TargetInvocationException ex) { if (ex.InnerException is RuleImportException) { throw new RuleImportException($"Error in configuration for command '{label}': {ex.InnerException.Message}"); } else { throw; } } } else { throw new RuleImportException($"The given 'type' value is invalid in definition for '{label}'."); } }
// Configuration: // "role" - string; The given role that applies to this command. // "successmsg" - string; Messages to display on command success. Overrides default. protected RoleManipulation(ModCommands l, string label, JObject conf, CommandMode mode) : base(l, label, conf) { _mode = mode; var rolestr = conf["role"]?.Value <string>(); if (string.IsNullOrWhiteSpace(rolestr)) { throw new RuleImportException("Role must be provided."); } _role = new EntityName(rolestr, EntityType.Role); _successMsg = conf["successmsg"]?.Value <string>(); DefaultUsageMsg = $"{this.Trigger} [user or user ID]\n" + (_mode == CommandMode.Add ? "Adds" : "Removes") + " the specified role " + (_mode == CommandMode.Add ? "to" : "from") + " the given user."; }
// Configuration: // "forcereason" - boolean; Force a reason to be given. Defaults to false. // "purgedays" - integer; Number of days of target's post history to delete, if banning. // Must be between 0-7 inclusive. Defaults to 0. // "successmsg" - Message to display on command success. Overrides default. // "notifymsg" - Message to send to the target user being acted upon. Default message is used // if the value is not specified. If a blank value is given, the feature is disabled. // Takes the special values $s for server name and $r for reason text. protected BanKick(ModCommands l, string label, JObject conf, CommandMode mode) : base(l, label, conf) { _mode = mode; _forceReason = conf["forcereason"]?.Value <bool>() ?? false; _purgeDays = conf["purgedays"]?.Value <int>() ?? 0; if (_mode == CommandMode.Ban && (_purgeDays > 7 || _purgeDays < 0)) { throw new RuleImportException("The value of 'purgedays' must be between 0 and 7."); } _successMsg = conf["successmsg"]?.Value <string>(); if (conf["notifymsg"] == null) { // Message not specified - use default _notifyMsg = string.Format(NotifyDefault, mode == CommandMode.Ban ? "banned" : "kicked"); } else { string val = conf["notifymsg"].Value <string>(); if (string.IsNullOrWhiteSpace(val)) { _notifyMsg = null; // empty value - disable message } else { _notifyMsg = val; } } // Building usage message here DefaultUsageMsg = $"{this.Trigger} [user or user ID] " + (_forceReason ? "[reason]" : "*[reason]*") + "\n" + "Removes the given user from this server" + (_mode == CommandMode.Ban ? " and prevents the user from rejoining" : "") + ". " + (_forceReason ? "L" : "Optionally l") + "ogs the reason for the " + (_mode == CommandMode.Ban ? "ban" : "kick") + " to the Audit Log."; if (_purgeDays > 0) { DefaultUsageMsg += $"\nAdditionally removes the user's post history for the last {_purgeDays} day(s)."; } }
public void ParseMod(ModCommands x) { var userParameter = x.UserParameter; var dbModContext = new db(new SqlCeConnection("Data Source=|DataDirectory|IRCbotDB.sdf;Max Database Size=4091")); if (x.Action == "message") SendMessage(x.Result); else if (x.CommandParameter == null) return; else if (userParameter.Length == 0) SendMessage("A word or name must be provided."); else { switch (x.Action) { case "set": var modVariable = dbModContext.ModVariables.First(y => y.Variable == x.Result); modVariable.Value = x.ResultParameter; MyGlobals.ModVariables[x.Result] = x.ResultParameter; dbModContext.SubmitChanges(); break; case "stalk": var stalk = dbModContext.Stalk.FirstOrDefault(y => y.User == userParameter); // checks if user can be found in table if (stalk != null) { var deltatime = DeltaTimeFormat(DateTime.Now - (DateTime)stalk.Time); SendMessage(stalk.User + " seen " + deltatime + " ago saying " + stalk.Message); } else SendMessage("No records of " + userParameter); break; case "database": // initial setup, and checks to see if userParameter is already in the table var tableName = x.Result; var tableType = Assembly.GetExecutingAssembly().GetType("IRCbot." + tableName); var itable = dbModContext.GetTable(tableType); object found = false; foreach (var y in itable) // My answer! http://stackoverflow.com/questions/1820102/how-can-i-create-a-linq-to-sql-statement-when-i-have-table-name-as-string/20307529#20307529 { var value = (string)y.GetType().GetProperty("Word").GetValue(y, null); Console.Write(value + ","); if (value == userParameter) found = y; } if (x.ActionParameter == "add") { if (found.Equals(false)) { Console.WriteLine(userParameter + " added"); dynamic tableClass = Activator.CreateInstance(tableType); tableClass.Word = userParameter; itable.InsertOnSubmit(tableClass); dbModContext.SubmitChanges(); MyGlobals.BanWords[tableName].Add(userParameter); SendMessage(userParameter + " added to the " + tableName); } else SendMessage(userParameter + " already in the " + tableName); } else { Debug.Assert(x.ActionParameter == "remove"); if (!found.Equals(false)) { Console.WriteLine(userParameter + " removed"); itable.DeleteOnSubmit(found); dbModContext.SubmitChanges(); MyGlobals.BanWords[tableName].Remove(userParameter); SendMessage(userParameter + " removed from the " + tableName); } else SendMessage(userParameter + " not found in the " + tableName); } break; } } }
public Command(ModCommands l, string label, JObject conf) { _mod = l; _label = label; _command = conf["command"].Value <string>(); }
// No configuration. public ConfReload(ModCommands l, string label, JObject conf) : base(l, label, conf) { }
public Kick(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Kick) { }
public Ban(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Ban) { }
public RoleDel(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Del) { }
public RoleAdd(ModCommands l, string label, JObject conf) : base(l, label, conf, CommandMode.Add) { }
// No configuration at the moment. // TODO: Whitelist/blacklist - to limit which channels it can "say" into public Say(ModCommands l, string label, JObject conf) : base(l, label, conf) { DefaultUsageMsg = $"{this.Trigger} [channel] [message]\n" + "Displays the given message exactly as specified to the given channel."; }
// No configuration. // TODO bring in some options from BanKick. Particularly custom success msg. // TODO when ModLogs fully implemented, add a reason? public Unban(ModCommands l, string label, JObject conf) : base(l, label, conf) { DefaultUsageMsg = $"{this.Trigger} [user or user ID]\n" + "Unbans the given user, allowing them to rejoin the server."; }