public async void ExecuteCommands(string[] commands, Channel channel, ulong user) { string username = channel.GetUser(user).Name; if (!userCommandRate.ContainsKey(user)) { userCommandRate.Add(user, new UserCommandRateInfo(0, DateTime.Now)); } if (channel.Name != "console" && userCommandRate[user].useCount > rateLimit) { double timeoutTime = (userCommandRate[user].timeoutEnd - DateTime.Now).TotalSeconds; if (timeoutTime > 0) { await channel.SendMessage($"@{username}: Please wait {SmartRound(timeoutTime, 100)} seconds before using another command."); return; } else { userCommandRate[user].useCount = 0; } } foreach (string command in commands) { if ((DateTime.Now - userCommandRate[user].lastUsage).TotalSeconds < minTimeRange) { userCommandRate[user].useCount++; } else { userCommandRate[user].useCount = 0; } userCommandRate[user].lastUsage = DateTime.Now; //Limit Command usage if (user != 0 && userCommandRate[user].useCount > rateLimit && channel.Name != "console") { userCommandRate[user].timeoutEnd = DateTime.Now.AddSeconds(timeoutDuration); await channel.SendMessage($"@{username}: Rate Limit reached, aborting... You have been timed out for {timeoutDuration} seconds"); break; } string cName = (from a in command.ToLower().Split(' ') where !string.IsNullOrWhiteSpace(a) select a).First(); Karuta.LOGGER.Log($"Command recieved: \"{cName}\" from \"{(user == 0 ? "EventBot" : $"{ channel.GetUser(user)?.Name}")}\" in channel \"{channel.Name}\" on server \"{channel.Server.Name}\"", nameof(DiscordCommandInterpreter <T>)); if (_commands.ContainsKey(cName)) { try { DiscordCommand cmd = _commands[cName] as DiscordCommand; if (cmd.GetType() != typeof(DiscordImageCommand) && user != 0) { if ((channel.Name == "console" || cmd.GetType() == typeof(DiscordHelpCommand))) { if (cmd.GetType() == typeof(DiscordPurgeCommand)) { if (user == adminUserID && channel.Name == "console") { cmd.Parse(new List <string>(), channel); } else { await channel.SendMessage("You are not authorized to use this command"); Karuta.LOGGER.LogWarning($"Underprivilaged user \"{channel.GetUser(user)?.Name}\" attempted to use command \"{cName}\"", nameof(DiscordCommandInterpreter <T>)); } } else { List <string> args = new List <string>(); args.AddRange(from arg in command.Split(' ') where !string.IsNullOrWhiteSpace(arg) select arg); args.RemoveAt(0); cmd.Parse(args, channel); } } else { await channel.SendMessage("this command can only be used in the console"); } } else { ((DiscordImageCommand)cmd).SendImage(channel); } } catch (Exception ex) { await channel.SendMessage($"An error occured while executing the command: {ex.Message}"); Karuta.LOGGER.LogError($"An error occured while executing the command: {ex.Message}", nameof(DiscordCommandInterpreter <T>)); Karuta.LOGGER.LogError(ex.StackTrace, nameof(DiscordCommandInterpreter <T>)); } } else { await channel.SendMessage("No such command"); } } }
async void Add() { //Validate if (string.IsNullOrWhiteSpace(_name) && string.IsNullOrWhiteSpace(_url)) { await _channel.SendMessage($"An image name and url must be specified, use !help {name} for more info"); return; } if ((_url.Contains('`') || _url.Contains('{'))) { await _channel.SendMessage("Invalid URL"); return; } List <string> imageLinks = new List <string>(); Uri url = new Uri(_url); if (url.Host == "i.imgur.com") { if (_bot.validExtensions.Contains(Path.GetExtension(url.AbsolutePath))) { imageLinks.Add(_url); } } else if (url.Host != "imgur.com") { await _channel.SendMessage("Only imgur images are allowed"); return; } //Resolve Link if (imageLinks.Count == 0) { imageLinks = await _bot.ResolveImgurUrl(url); } //Add Command if (_bot.interpreter.CommandExists(_name)) { DiscordCommand c = _bot.interpreter.GetCommand(_name); if (c?.GetType() == typeof(DiscordImageCommand)) { DiscordImageCommand cmd = (DiscordImageCommand)c; int dupeCount = 0; int addCount = 0; if (!string.IsNullOrWhiteSpace(_help)) { cmd.SetHelpMessage(_help); } foreach (string link in imageLinks) { if (cmd.images.Contains(link)) { dupeCount++; continue; } cmd.AddImage(link); addCount++; } if (dupeCount > 0) { await _channel.SendMessage($"{dupeCount} Image{((dupeCount > 1) ? "s" : "")} already existed and were not added"); } await _channel.SendMessage($"{addCount} Image{((addCount > 1) ? "s" : "")} Added"); //await _channel.SendMessage(cmd.ToString()); } else { await _channel.SendMessage("This name cannot be used"); } } else { //Karuta.Write(count); DiscordImageCommand img = (_help != default(string)) ? new DiscordImageCommand(_name, _help) { images = imageLinks } :new DiscordImageCommand(_name) { images = imageLinks }; _bot.interpreter.RegisterCommand(img); img.init?.Invoke(); _bot.interpreter.GetCommandOfType <DiscordHelpCommand>()?.init(); await _channel.SendMessage($"{imageLinks.Count} Image{((imageLinks.Count > 1) ? "s" : "")} Command Added"); Karuta.Write(img.ToString()); } //foreach (string c in bot.commands.Keys) // _channel.SendMessage(c); _bot.SaveData(); Karuta.InvokeCommand("save", new List <string>()); _url = _name = null; }