private IEnumerable<ResponseMessage> PinHandler(IncomingMessage message, string matchedHandle) { if (!_adminPlugin.AdminModeEnabled()) { yield return message.ReplyToChannel("Admin mode isn't enabled."); yield break; } string pinString = message.TargetedText.Substring(matchedHandle.Length).Trim(); int pin; if (int.TryParse(pinString, out pin)) { if (_adminPlugin.AuthoriseUser(message.UserId, pin)) { yield return message.ReplyToChannel($"{message.Username} - you now have admin rights."); _log.Info($"{message.Username} now has admin rights."); } else { yield return message.ReplyToChannel("Incorrect admin pin entered."); } } else { yield return message.ReplyToChannel($"Unable to parse pin '{pinString}'"); } }
private IEnumerable<ResponseMessage> StopPingingHandler(IncomingMessage message, string matchedHandle) { if (_pingPlugin.StopPingingUser(message.UserId)) { yield return message.ReplyToChannel($"Ok, I will stop pinging @{message.Username}"); } else { yield return message.ReplyToChannel($"BUT I AM NOT PINGING @{message.Username}"); } }
private IEnumerable<ResponseMessage> StatsHandler(IncomingMessage message, string matchedHandle) { string textMessage = string.Join(Environment.NewLine, _statsPlugin.GetStats().OrderBy(x => x)); if (!string.IsNullOrEmpty(textMessage)) { yield return message.ReplyToChannel(">>>" + textMessage); } else { yield return message.ReplyToChannel("No stats have been recorded yet."); } }
private IEnumerable<ResponseMessage> FlickrHandler(IncomingMessage message, string matchedHandle) { string searchTerm = message.TargetedText.Substring(matchedHandle.Length).Trim(); if (string.IsNullOrEmpty(searchTerm)) { yield return message.ReplyToChannel($"Please give me something to search, e.g. {matchedHandle} trains"); } else { yield return message.IndicateTypingOnChannel(); string apiKey = _configReader.GetConfigEntry<string>("flickr:apiKey"); if (string.IsNullOrEmpty(apiKey)) { _statsPlugin.IncrementState("Flickr:Failed"); yield return message.ReplyToChannel("Woops, looks like a Flickr API Key has not been entered. Please ask the admin to fix this"); } else { var flickr = new Flickr(apiKey); var options = new PhotoSearchOptions { Tags = searchTerm, PerPage = 50, Page = 1}; PhotoCollection photos = flickr.PhotosSearch(options); if (photos.Any()) { _statsPlugin.IncrementState("Flickr:Sent"); int i = new Random().Next(0, photos.Count); Photo photo = photos[i]; var attachment = new Attachment { AuthorName = photo.OwnerName, Fallback = photo.Description, ImageUrl = photo.LargeUrl, ThumbUrl = photo.ThumbnailUrl }; yield return message.ReplyToChannel($"Here is your picture about '{searchTerm}'", attachment); } else { _statsPlugin.IncrementState("Flickr:Failed"); yield return message.ReplyToChannel($"Sorry @{message.Username}, I couldn't find anything about {searchTerm}"); } } } }
private IEnumerable<ResponseMessage> CalculateHandler(IncomingMessage message, string matchedHandle) { string response = string.Empty; if (matchedHandle != null) { string expression = message.FullText.Substring(matchedHandle.Length).Trim(); Parser parser = new Parser(); try { IExpression parsedExpression = parser.Parse(expression); object result = parsedExpression.Calculate(); response = $"{parsedExpression} = {result}"; } catch (Exception e) { bool showErrors = !string.IsNullOrEmpty(matchedHandle); if (showErrors) { _statsPlugin.IncrementState("Calc:Failed"); response = $"Who taught you maths? {e.Message}"; } } } if (!string.IsNullOrEmpty(response)) { _statsPlugin.IncrementState("Calc:Calced"); yield return message.ReplyToChannel($"@{message.Username}: {response}"); } }
private IEnumerable<ResponseMessage> ListHandlerForChannel(IncomingMessage message, string matchedHandle) { SchedulePlugin.ScheduleEntry[] schedules = _schedulePlugin.ListSchedulesForChannel(message.Channel); if (schedules.Any()) { yield return message.ReplyToChannel("Schedules for channel:"); string[] scheduleStrings = schedules.Select((x, i) => x.ToString(i)).ToArray(); yield return message.ReplyToChannel(">>>" + string.Join("\n", scheduleStrings)); } else { yield return message.ReplyToChannel("No schedules set for this channel."); } }
private IEnumerable<ResponseMessage> WelcomeHandler(IncomingMessage message, string matchedHandle) { _statsPlugin.IncrementState("Hello"); yield return message.ReplyToChannel($"Hey @{message.Username}, how you doing?"); Thread.Sleep(TimeSpan.FromSeconds(5)); yield return message.ReplyDirectlyToUser("I know where you live..."); }
private IEnumerable<ResponseMessage> JokeHandler(IncomingMessage message, string matchedHandle) { yield return message.IndicateTypingOnChannel(); IRestResponse jokeResponse = new Random().Next(0, 100) < 80 ? GetTambalJoke() : GetMommaJoke(); if (jokeResponse.StatusCode == HttpStatusCode.OK) { _statsPlugin.IncrementState("Jokes:Told"); var joke = JsonConvert.DeserializeObject<JokeContainer>(jokeResponse.Content); yield return message.ReplyToChannel(joke.Joke); } else { _statsPlugin.IncrementState("Jokes:Failed"); yield return message.ReplyToChannel($"Dam, I can't think of one. [{jokeResponse.StatusCode}]"); } }
private IEnumerable<ResponseMessage> AdminHelpHandler(IncomingMessage message, string matchedHandle) { if (!_adminPlugin.AuthenticateUser(message.UserId)) { yield return message.ReplyToChannel($"Sorry {message.Username}, only admins can use this function."); yield break; } foreach (var handlerMapping in HandlerMappings) { string mappings = string.Join(" | ", handlerMapping.ValidHandles.Select(x => $"{x}")); yield return message.ReplyDirectlyToUser($"`{mappings}` - {handlerMapping.Description}"); } }
private IEnumerable<ResponseMessage> ChannelsHandler(IncomingMessage message, string matchedHandle) { if (!_adminPlugin.AuthenticateUser(message.UserId)) { yield return message.ReplyToChannel($"Sorry {message.Username}, only admins can use this function."); yield break; } Dictionary<string, string> channels = _noobotCore.ListChannels(); yield return message.ReplyToChannel("All Connected Channels:"); yield return message.ReplyToChannel(">>>" + string.Join("\n", channels.Select(x => $"{x.Key}: {x.Value}"))); }
private IEnumerable<ResponseMessage> DeleteHandlerForChannel(IncomingMessage message, string matchedHandle) { string idString = message.TargetedText.Substring(matchedHandle.Length).Trim(); Guid guid; if (Guid.TryParse(idString, out guid)) { ScheduleEntry[] schedules = _schedulePlugin.ListSchedulesForChannel(message.Channel); ScheduleEntry scheduleToDelete = schedules.FirstOrDefault(x => x.Guid == guid); if (scheduleToDelete == null) { yield return message.ReplyToChannel($"Unable to find schedule with GUID: `'{guid}'`"); } else { _schedulePlugin.DeleteSchedule(guid); yield return message.ReplyToChannel($"Removed schedule: `{scheduleToDelete}`"); } } else { yield return message.ReplyToChannel($"Invalid id entered. Try using `schedule list`. (`{idString}`)"); } }
private IEnumerable<ResponseMessage> SchedulesListHandler(IncomingMessage message, string matchedHandle) { if (!_adminPlugin.AuthenticateUser(message.UserId)) { yield return message.ReplyToChannel($"Sorry {message.Username}, only admins can use this function."); yield break; } var schedules = _schedulePlugin.ListAllSchedules(); string[] scheduleStrings = schedules.Select(x => $"Guid: '{x.Guid}' Channel: '{x.Channel}'.").ToArray(); yield return message.ReplyToChannel("All Schedules:"); yield return message.ReplyToChannel(">>>" + string.Join("\n", scheduleStrings)); }
private IEnumerable<ResponseMessage> DeleteSchedulesHandler(IncomingMessage message, string matchedHandle) { if (!_adminPlugin.AuthenticateUser(message.UserId)) { yield return message.ReplyToChannel($"Sorry {message.Username}, only admins can use this function."); yield break; } var schedules = _schedulePlugin.ListAllSchedules(); _schedulePlugin.DeleteSchedules(schedules.Select(x => x.Guid).ToArray()); yield return message.ReplyToChannel("All schedules deleted"); }
private IEnumerable<ResponseMessage> DeleteHandlerForChannel(IncomingMessage message, string matchedHandle) { string idString = message.TargetedText.Substring(matchedHandle.Length).Trim(); int? id = ConvertToInt(idString); if (id.HasValue) { SchedulePlugin.ScheduleEntry[] schedules = _schedulePlugin.ListSchedulesForChannel(message.Channel); if (id < 0 || id > (schedules.Length - 1)) { yield return message.ReplyToChannel($"Woops, unable to delete schedule with id of `{id.Value}`"); } else { _schedulePlugin.DeleteSchedule(schedules[id.Value]); yield return message.ReplyToChannel($"Removed schedule: {schedules[id.Value]}"); } } else { yield return message.ReplyToChannel($"Invalid id entered. Try using `schedule list`. ({idString})"); } }
private IEnumerable<ResponseMessage> CronHandler(IncomingMessage message, string matchedHandle) { string cronJob = message.TargetedText.Substring(matchedHandle.Length).Trim(); Match regexMatch = Regex.Match(cronJob); if (!regexMatch.Success) { yield return message.ReplyToChannel($"Error while parsing cron job. Your command should match something like `@{message.BotName} schedule cronjob '0 15 10 * * ?' @{message.BotName} tell me a joke``"); yield break; } string schedule = regexMatch.Groups[1].Value.Trim(); string command = regexMatch.Groups[2].Value.Trim(); yield return CreateSchedule(message, command, schedule); }
private ResponseMessage CreateSchedule(IncomingMessage message, string matchedHandle, TimeSpan timeSpan, bool runOnlyAtNight) { var schedule = new SchedulePlugin.ScheduleEntry { Channel = message.Channel, ChannelType = message.ChannelType, Command = message.TargetedText.Substring(matchedHandle.Length).Trim(), RunEvery = timeSpan, UserId = message.UserId, UserName = message.Username, LastRun = DateTime.Now, RunOnlyAtNight = runOnlyAtNight }; if (string.IsNullOrEmpty(schedule.Command)) { return message.ReplyToChannel("Please enter a command to be scheduled."); } else { _schedulePlugin.AddSchedule(schedule); return message.ReplyToChannel($"Schedule created for command '{schedule.Command}'."); } }
private ResponseMessage CreateSchedule(IncomingMessage message, string command, string cronSchedule) { var schedule = new ScheduleEntry { Guid = Guid.NewGuid(), Channel = message.Channel, ChannelType = message.ChannelType, Command = command, CronSchedule = cronSchedule, UserId = message.UserId, UserName = message.Username, Created = DateTime.Now }; if (!CronExpression.IsValidExpression(cronSchedule)) { return message.ReplyToChannel($"Unknown cron schedule `'{cronSchedule}'`"); } if (string.IsNullOrEmpty(schedule.Command)) { return message.ReplyToChannel("Please enter a command to be scheduled."); } _schedulePlugin.AddSchedule(schedule); return message.ReplyToChannel($"Schedule created for command '{schedule.Command}'."); }
private IEnumerable<ResponseMessage> PingHandler(IncomingMessage message, string matchedHandle) { yield return message.ReplyToChannel($"Ok, I will start pinging @{message.Username}"); _pingPlugin.StartPingingUser(message.UserId); }