The easy to use and full featured Irc Bot everyone is talking about!
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PingMe.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Combot.IRCServices;
  6. using Combot.IRCServices.Messaging;
  7. namespace Combot.Modules.ModuleClasses
  8. {
  9. public class PingMe : Module
  10. {
  11. private List<PingItem> pingList;
  12. private ReaderWriterLockSlim listLock;
  13. public override void Initialize()
  14. {
  15. listLock = new ReaderWriterLockSlim();
  16. pingList = new List<PingItem>();
  17. Bot.IRC.Message.CTCPNoticeRecievedEvent += HandlePingResponse;
  18. Bot.CommandReceivedEvent += HandleCommandEvent;
  19. }
  20. public override void ParseCommand(CommandMessage command)
  21. {
  22. if (Commands.Find(cmd => cmd.Name == "Ping Me").Triggers.Contains(command.Command))
  23. {
  24. int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  25. PingItem tmpItem = new PingItem();
  26. tmpItem.Nick = command.Nick.Nickname;
  27. tmpItem.Location = command.Location;
  28. tmpItem.LocationType = command.LocationType;
  29. tmpItem.Timestamp = DateTime.Now;
  30. listLock.EnterWriteLock();
  31. if (pingList.Exists(item => item.Nick == command.Nick.Nickname))
  32. {
  33. pingList.RemoveAll(item => item.Nick == command.Nick.Nickname);
  34. }
  35. pingList.Add(tmpItem);
  36. listLock.ExitWriteLock();
  37. Bot.IRC.SendCTCP(command.Nick.Nickname, "PING", epoch.ToString());
  38. }
  39. }
  40. private void HandlePingResponse(object sender, CTCPMessage e)
  41. {
  42. if (e.Command == "PING")
  43. {
  44. listLock.EnterReadLock();
  45. PingItem pingItem = pingList.Find(item => item.Nick == e.Sender.Nickname);
  46. listLock.ExitReadLock();
  47. if (pingItem != null)
  48. {
  49. DateTime curTime = DateTime.Now;
  50. DateTime prevTime = pingItem.Timestamp;
  51. TimeSpan difTime = curTime.Subtract(prevTime);
  52. string timeString = string.Empty;
  53. if (difTime.Days > 0)
  54. {
  55. timeString += difTime.Days.ToString() + " Days, ";
  56. }
  57. if (difTime.Hours > 0)
  58. {
  59. timeString += difTime.Hours.ToString() + " Hours, ";
  60. }
  61. if (difTime.Minutes > 0)
  62. {
  63. timeString += difTime.Minutes.ToString() + " Minutes, ";
  64. }
  65. if (difTime.Seconds > 0)
  66. {
  67. timeString += difTime.Seconds.ToString() + " Seconds, ";
  68. }
  69. if (difTime.Milliseconds > 0)
  70. {
  71. timeString += difTime.Milliseconds.ToString() + " Milliseconds";
  72. }
  73. switch (pingItem.LocationType)
  74. {
  75. case LocationType.Channel:
  76. Bot.IRC.SendPrivateMessage(pingItem.Location, string.Format("{0}, your ping is {1}", pingItem.Nick, timeString));
  77. break;
  78. case LocationType.Notice:
  79. Bot.IRC.SendNotice(pingItem.Nick, string.Format("Your ping is {0}", timeString));
  80. break;
  81. case LocationType.Query:
  82. Bot.IRC.SendPrivateMessage(pingItem.Nick, string.Format("Your ping is {0}", timeString));
  83. break;
  84. }
  85. listLock.EnterWriteLock();
  86. pingList.RemoveAll(item => item.Nick == pingItem.Nick);
  87. listLock.ExitWriteLock();
  88. }
  89. }
  90. }
  91. private class PingItem
  92. {
  93. public string Nick { get; set; }
  94. public string Location { get; set; }
  95. public LocationType LocationType { get; set; }
  96. public DateTime Timestamp { get; set; }
  97. public PingItem()
  98. {
  99. Nick = string.Empty;
  100. Location = string.Empty;
  101. LocationType = LocationType.Channel;
  102. Timestamp = DateTime.Now;
  103. }
  104. }
  105. }
  106. }