The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Timer.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Timer : Module
  8. {
  9. private List<System.Timers.Timer> timers;
  10. private ReaderWriterLockSlim listLock;
  11. public override void Initialize()
  12. {
  13. timers = new List<System.Timers.Timer>();
  14. listLock = new ReaderWriterLockSlim();
  15. Bot.CommandReceivedEvent += HandleCommandEvent;
  16. }
  17. public override void ParseCommand(CommandMessage command)
  18. {
  19. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  20. switch (foundCommand.Name)
  21. {
  22. case "Timer":
  23. double timeout;
  24. string message = command.Arguments.ContainsKey("Message") ? command.Arguments["Message"] : string.Empty;
  25. if (double.TryParse(command.Arguments["Time"], out timeout) && timeout > 0)
  26. {
  27. if (message.StartsWith(Bot.ServerConfig.CommandPrefix))
  28. {
  29. string cmd = message.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries).First();
  30. if (foundCommand.Triggers.Contains(cmd.TrimStart(Bot.ServerConfig.CommandPrefix.ToCharArray())))
  31. {
  32. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Recursion is bad.", true);
  33. break;
  34. }
  35. }
  36. System.Timers.Timer newTimer = new System.Timers.Timer();
  37. newTimer.Interval = (timeout * 1000.0);
  38. newTimer.Enabled = true;
  39. newTimer.AutoReset = false;
  40. newTimer.Elapsed += (sender, e) => TimerElapsed(sender, e, message, command);
  41. listLock.EnterWriteLock();
  42. timers.Add(newTimer);
  43. listLock.ExitWriteLock();
  44. string addedTimer = string.Format("Timer added for {0} seconds from now.", timeout);
  45. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, addedTimer);
  46. }
  47. else
  48. {
  49. string notValid = "Please enter a valid time.";
  50. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, notValid, true);
  51. }
  52. break;
  53. }
  54. }
  55. private void TimerElapsed(object sender, EventArgs e, string message, CommandMessage command)
  56. {
  57. System.Timers.Timer timer = (System.Timers.Timer) sender;
  58. timer.Enabled = false;
  59. listLock.EnterWriteLock();
  60. timers.Remove(timer);
  61. listLock.ExitWriteLock();
  62. if (message.StartsWith(Bot.ServerConfig.CommandPrefix))
  63. {
  64. Bot.ExecuteCommand(message, command.Location, command.MessageType, command.Nick);
  65. }
  66. else
  67. {
  68. if (string.IsNullOrEmpty(message))
  69. {
  70. message = "Your timer has elapsed!";
  71. }
  72. message = "\u0002RING RING RING\u0002 " + message;
  73. SendResponse(MessageType.Query, command.Location, command.Nick.Nickname, message, false);
  74. }
  75. }
  76. }
  77. }