The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Seen.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Seen : Module
  8. {
  9. public override void Initialize()
  10. {
  11. Bot.CommandReceivedEvent += HandleCommandEvent;
  12. }
  13. public override void ParseCommand(CommandMessage command)
  14. {
  15. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  16. switch (foundCommand.Name)
  17. {
  18. case "Seen":
  19. GetLastSeen(command);
  20. break;
  21. }
  22. }
  23. private void GetLastSeen(CommandMessage command)
  24. {
  25. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : null;
  26. List<Dictionary<string, object>> channelList = GetChannelList(channel, command.Arguments["Nickname"]);
  27. List<Dictionary<string, object>> partList = GetPartList(channel, command.Arguments["Nickname"]);
  28. List<Dictionary<string, object>> joinList = GetJoinList(channel, command.Arguments["Nickname"]);
  29. List<Dictionary<string, object>> kickList = GetKickList(channel, command.Arguments["Nickname"]);
  30. List<Dictionary<string, object>> quitList = GetQuitList(command.Arguments["Nickname"]);
  31. List<Dictionary<DateTime, string>> lastSeenList = new List<Dictionary<DateTime, string>>();
  32. if (channelList.Any())
  33. {
  34. DateTime chanTime = (DateTime)channelList.First()["date_added"];
  35. TimeSpan difference = DateTime.Now.Subtract(chanTime);
  36. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago saying the following in \u0002{2}\u0002: {3}", command.Arguments["Nickname"], ConvertToDifference(difference), channelList.First()["name"], channelList.First()["message"]);
  37. lastSeenList.Add(new Dictionary<DateTime, string>() { { chanTime, message } });
  38. }
  39. if (partList.Any())
  40. {
  41. DateTime partTime = (DateTime)partList.First()["date_added"];
  42. TimeSpan difference = DateTime.Now.Subtract(partTime);
  43. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago leaving \u0002{2}\u0002.", command.Arguments["Nickname"], ConvertToDifference(difference), partList.First()["name"]);
  44. lastSeenList.Add(new Dictionary<DateTime, string>() { { partTime, message } });
  45. }
  46. if (joinList.Any())
  47. {
  48. DateTime joinTime = (DateTime)joinList.First()["date_added"];
  49. TimeSpan difference = DateTime.Now.Subtract(joinTime);
  50. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago joining \u0002{2}\u0002.", command.Arguments["Nickname"], ConvertToDifference(difference), joinList.First()["name"]);
  51. lastSeenList.Add(new Dictionary<DateTime, string>() { { joinTime, message } });
  52. }
  53. if (kickList.Any())
  54. {
  55. DateTime kickTime = (DateTime)kickList.First()["date_added"];
  56. TimeSpan difference = DateTime.Now.Subtract(kickTime);
  57. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago being kicked from \u0002{2}\u0002 with the reason: {3}", command.Arguments["Nickname"], ConvertToDifference(difference), kickList.First()["name"], kickList.First()["reason"]);
  58. lastSeenList.Add(new Dictionary<DateTime, string>() { { kickTime, message } });
  59. }
  60. if (quitList.Any())
  61. {
  62. DateTime quitTime = (DateTime)quitList.First()["date_added"];
  63. TimeSpan difference = DateTime.Now.Subtract(quitTime);
  64. string message = string.Format("I last saw \u0002{0}\u0002 {1} ago quiting.", command.Arguments["Nickname"], ConvertToDifference(difference));
  65. lastSeenList.Add(new Dictionary<DateTime, string>() { { quitTime, message } });
  66. }
  67. if (lastSeenList.Count > 0)
  68. {
  69. DateTime bestTime = new DateTime(1990);
  70. string seenMessage = string.Empty;
  71. for (int i = 0; i < lastSeenList.Count; i++)
  72. {
  73. if (lastSeenList[i].Keys.First().CompareTo(bestTime) > 0)
  74. {
  75. bestTime = lastSeenList[i].Keys.First();
  76. seenMessage = lastSeenList[i].Values.First();
  77. }
  78. }
  79. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, seenMessage);
  80. }
  81. else
  82. {
  83. string notFound = string.Empty;
  84. if (channel != null)
  85. {
  86. notFound = string.Format("I have not seen \u0002{0}\u0002 in \u0002{1}\u0002.", command.Arguments["Nickname"], channel);
  87. }
  88. else
  89. {
  90. notFound = string.Format("I have not seen \u0002{0}\u0002.", command.Arguments["Nickname"]);
  91. }
  92. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, notFound);
  93. }
  94. }
  95. private List<Dictionary<string, object>> GetChannelList(string channel, string nickname)
  96. {
  97. if (channel != null)
  98. {
  99. string search = "SELECT `channelmessages`.`message`, `channelmessages`.`date_added`, `channels`.`name` FROM `channelmessages` " +
  100. "INNER JOIN `nicks` " +
  101. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  102. "INNER JOIN `channels` " +
  103. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  104. "INNER JOIN `servers` " +
  105. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  106. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  107. "ORDER BY date_added DESC LIMIT 1";
  108. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  109. }
  110. else
  111. {
  112. string search = "SELECT `channelmessages`.`message`, `channelmessages`.`date_added`, `channels`.`name` FROM `channelmessages` " +
  113. "INNER JOIN `nicks` " +
  114. "ON `channelmessages`.`nick_id` = `nicks`.`id` " +
  115. "INNER JOIN `channels` " +
  116. "ON `channelmessages`.`channel_id` = `channels`.`id` " +
  117. "INNER JOIN `servers` " +
  118. "ON `channelmessages`.`server_id` = `servers`.`id` " +
  119. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  120. "ORDER BY date_added DESC LIMIT 1";
  121. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  122. }
  123. }
  124. private List<Dictionary<string, object>> GetPartList(string channel, string nickname)
  125. {
  126. if (channel != null)
  127. {
  128. string search = "SELECT `channelparts`.`date_added`, `channels`.`name` FROM `channelparts` " +
  129. "INNER JOIN `nicks` " +
  130. "ON `channelparts`.`nick_id` = `nicks`.`id` " +
  131. "INNER JOIN `channels` " +
  132. "ON `channelparts`.`channel_id` = `channels`.`id` " +
  133. "INNER JOIN `servers` " +
  134. "ON `channelparts`.`server_id` = `servers`.`id` " +
  135. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  136. "ORDER BY date_added DESC LIMIT 1";
  137. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  138. }
  139. else
  140. {
  141. string search = "SELECT `channelparts`.`date_added`, `channels`.`name` FROM `channelparts` " +
  142. "INNER JOIN `nicks` " +
  143. "ON `channelparts`.`nick_id` = `nicks`.`id` " +
  144. "INNER JOIN `channels` " +
  145. "ON `channelparts`.`channel_id` = `channels`.`id` " +
  146. "INNER JOIN `servers` " +
  147. "ON `channelparts`.`server_id` = `servers`.`id` " +
  148. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  149. "ORDER BY date_added DESC LIMIT 1";
  150. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  151. }
  152. }
  153. private List<Dictionary<string, object>> GetJoinList(string channel, string nickname)
  154. {
  155. if (channel != null)
  156. {
  157. string search = "SELECT `channeljoins`.`date_added`, `channels`.`name` FROM `channeljoins` " +
  158. "INNER JOIN `nicks` " +
  159. "ON `channeljoins`.`nick_id` = `nicks`.`id` " +
  160. "INNER JOIN `channels` " +
  161. "ON `channeljoins`.`channel_id` = `channels`.`id` " +
  162. "INNER JOIN `servers` " +
  163. "ON `channeljoins`.`server_id` = `servers`.`id` " +
  164. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  165. "ORDER BY date_added DESC LIMIT 1";
  166. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  167. }
  168. else
  169. {
  170. string search = "SELECT `channeljoins`.`date_added`, `channels`.`name` FROM `channeljoins` " +
  171. "INNER JOIN `nicks` " +
  172. "ON `channeljoins`.`nick_id` = `nicks`.`id` " +
  173. "INNER JOIN `channels` " +
  174. "ON `channeljoins`.`channel_id` = `channels`.`id` " +
  175. "INNER JOIN `servers` " +
  176. "ON `channeljoins`.`server_id` = `servers`.`id` " +
  177. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  178. "ORDER BY date_added DESC LIMIT 1";
  179. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  180. }
  181. }
  182. private List<Dictionary<string, object>> GetKickList(string channel, string nickname)
  183. {
  184. if (channel != null)
  185. {
  186. string search = "SELECT `channelkicks`.`date_added`, `channelkicks`.`reason`, `channels`.`name` FROM `channelkicks` " +
  187. "INNER JOIN `nicks` " +
  188. "ON `channelkicks`.`kicked_nick_id` = `nicks`.`id` " +
  189. "INNER JOIN `channels` " +
  190. "ON `channelkicks`.`channel_id` = `channels`.`id` " +
  191. "INNER JOIN `servers` " +
  192. "ON `channelkicks`.`server_id` = `servers`.`id` " +
  193. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2} " +
  194. "ORDER BY date_added DESC LIMIT 1";
  195. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  196. }
  197. else
  198. {
  199. string search = "SELECT `channelkicks`.`date_added`, `channelkicks`.`reason`, `channels`.`name` FROM `channelkicks` " +
  200. "INNER JOIN `nicks` " +
  201. "ON `channelkicks`.`kicked_nick_id` = `nicks`.`id` " +
  202. "INNER JOIN `channels` " +
  203. "ON `channelkicks`.`channel_id` = `channels`.`id` " +
  204. "INNER JOIN `servers` " +
  205. "ON `channelkicks`.`server_id` = `servers`.`id` " +
  206. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  207. "ORDER BY date_added DESC LIMIT 1";
  208. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  209. }
  210. }
  211. private List<Dictionary<string, object>> GetQuitList(string nickname)
  212. {
  213. string search = "SELECT `quits`.`date_added` FROM `quits` " +
  214. "INNER JOIN `nicks` " +
  215. "ON `quits`.`nick_id` = `nicks`.`id` " +
  216. "INNER JOIN `servers` " +
  217. "ON `quits`.`server_id` = `servers`.`id` " +
  218. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1} " +
  219. "ORDER BY date_added DESC LIMIT 1";
  220. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nickname });
  221. }
  222. private string ConvertToDifference(TimeSpan time)
  223. {
  224. string timeString = string.Empty;
  225. if (time.Days != 0)
  226. {
  227. string plural = string.Empty;
  228. if (time.Days > 1)
  229. {
  230. plural = "s";
  231. }
  232. timeString += string.Format("{0} Day{1} ", time.Days, plural);
  233. }
  234. if (time.Hours != 0)
  235. {
  236. string plural = string.Empty;
  237. if (time.Hours > 1)
  238. {
  239. plural = "s";
  240. }
  241. timeString += string.Format("{0} Hour{1} ", time.Hours, plural);
  242. }
  243. if (time.Minutes != 0)
  244. {
  245. string plural = string.Empty;
  246. if (time.Minutes > 1)
  247. {
  248. plural = "s";
  249. }
  250. timeString += string.Format("{0} Minute{1} ", time.Minutes, plural);
  251. }
  252. if (time.Seconds != 0)
  253. {
  254. string plural = string.Empty;
  255. if (time.Seconds > 1)
  256. {
  257. plural = "s";
  258. }
  259. timeString += string.Format("{0} Second{1} ", time.Seconds, plural);
  260. }
  261. return timeString.Trim();
  262. }
  263. }
  264. }