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.

Introductions.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. using Combot.IRCServices.Messaging;
  6. namespace Combot.Modules.Plugins
  7. {
  8. public class Introductions : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. Bot.IRC.Message.JoinChannelEvent += HandleJoinEvent;
  14. }
  15. public override void ParseCommand(CommandMessage command)
  16. {
  17. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  18. switch (foundCommand.Name)
  19. {
  20. case "Introduction":
  21. string method = command.Arguments["Method"];
  22. switch (method.ToLower())
  23. {
  24. case "add":
  25. AddIntroduction(command);
  26. break;
  27. case "edit":
  28. EditIntroduction(command);
  29. break;
  30. case "delete":
  31. DeleteIntroduction(command);
  32. break;
  33. case "view":
  34. ViewIntroduction(command);
  35. break;
  36. }
  37. break;
  38. }
  39. }
  40. private void HandleJoinEvent(object sender, JoinChannelInfo info)
  41. {
  42. List<Dictionary<string, object>> results = GetIntroductionList(info.Channel, info.Nick.Nickname);
  43. if (results.Any())
  44. {
  45. Random randNum = new Random();
  46. int index = randNum.Next(results.Count - 1);
  47. Dictionary<string, object> intro = results[index];
  48. Bot.IRC.SendPrivateMessage(info.Channel, string.Format("\u200B{0}", intro["message"]));
  49. }
  50. }
  51. private void AddIntroduction(CommandMessage command)
  52. {
  53. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  54. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  55. if (results.Count < Convert.ToInt32(GetOptionValue("Max Introductions")))
  56. {
  57. AddChannel(channel);
  58. AddNick(command.Nick.Nickname);
  59. Database database = new Database(Bot.ServerConfig.Database);
  60. string query = "INSERT INTO `introductions` SET " +
  61. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  62. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  63. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  64. "`message` = {5}, " +
  65. "`date_added` = {6}";
  66. database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, channel, Bot.ServerConfig.Name, command.Nick.Nickname, command.Arguments["Message"], command.TimeStamp });
  67. string introMessage = string.Format("Added introduction. You now have \u0002{0}\u0002 introductions set.", results.Count + 1);
  68. switch (command.MessageType)
  69. {
  70. case MessageType.Channel:
  71. Bot.IRC.SendPrivateMessage(command.Location, introMessage);
  72. break;
  73. case MessageType.Query:
  74. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, introMessage);
  75. break;
  76. case MessageType.Notice:
  77. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  78. break;
  79. }
  80. }
  81. else
  82. {
  83. string maxMessage = "You already have the maximum number of introductions for this channel. Delete one before trying to add another.";
  84. switch (command.MessageType)
  85. {
  86. case MessageType.Channel:
  87. Bot.IRC.SendPrivateMessage(command.Location, maxMessage);
  88. break;
  89. case MessageType.Query:
  90. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, maxMessage);
  91. break;
  92. case MessageType.Notice:
  93. Bot.IRC.SendNotice(command.Nick.Nickname, maxMessage);
  94. break;
  95. }
  96. }
  97. }
  98. private void EditIntroduction(CommandMessage command)
  99. {
  100. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  101. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  102. int num = 0;
  103. if (int.TryParse(command.Arguments["ID"], out num))
  104. {
  105. if (results.Count >= num)
  106. {
  107. int id = Convert.ToInt32(results[num - 1]["id"]);
  108. Database database = new Database(Bot.ServerConfig.Database);
  109. string query = "UPDATE `introductions` SET " +
  110. "`message` = {0} " +
  111. "WHERE `id` = {1}";
  112. database.Execute(query, new object[] { command.Arguments["Message"], id });
  113. string introMessage = string.Format("Introduction #\u0002{0}\u0002 is now: {1}", num, command.Arguments["Message"]);
  114. switch (command.MessageType)
  115. {
  116. case MessageType.Channel:
  117. Bot.IRC.SendPrivateMessage(command.Location, introMessage);
  118. break;
  119. case MessageType.Query:
  120. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, introMessage);
  121. break;
  122. case MessageType.Notice:
  123. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. string invalid = "Invalid introduction ID.";
  130. switch (command.MessageType)
  131. {
  132. case MessageType.Channel:
  133. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  134. break;
  135. case MessageType.Query:
  136. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  137. break;
  138. case MessageType.Notice:
  139. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  140. break;
  141. }
  142. }
  143. }
  144. else
  145. {
  146. string invalid = "Invalid introduction ID.";
  147. switch (command.MessageType)
  148. {
  149. case MessageType.Channel:
  150. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  151. break;
  152. case MessageType.Query:
  153. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  154. break;
  155. case MessageType.Notice:
  156. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  157. break;
  158. }
  159. }
  160. }
  161. private void DeleteIntroduction(CommandMessage command)
  162. {
  163. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  164. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  165. int num = 0;
  166. if (int.TryParse(command.Arguments["ID"], out num))
  167. {
  168. if (results.Count >= num)
  169. {
  170. int id = Convert.ToInt32(results[num - 1]["id"]);
  171. Database database = new Database(Bot.ServerConfig.Database);
  172. string query = "DELETE FROM `introductions` " +
  173. "WHERE `id` = {0}";
  174. database.Execute(query, new object[] { id });
  175. string introMessage = string.Format("Introduction #\u0002{0}\u0002 has been deleted.", num);
  176. switch (command.MessageType)
  177. {
  178. case MessageType.Channel:
  179. Bot.IRC.SendPrivateMessage(command.Location, introMessage);
  180. break;
  181. case MessageType.Query:
  182. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, introMessage);
  183. break;
  184. case MessageType.Notice:
  185. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  186. break;
  187. }
  188. }
  189. else
  190. {
  191. string invalid = "Invalid introduction ID.";
  192. switch (command.MessageType)
  193. {
  194. case MessageType.Channel:
  195. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  196. break;
  197. case MessageType.Query:
  198. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  199. break;
  200. case MessageType.Notice:
  201. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  202. break;
  203. }
  204. }
  205. }
  206. else
  207. {
  208. string invalid = "Invalid introduction ID.";
  209. switch (command.MessageType)
  210. {
  211. case MessageType.Channel:
  212. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  213. break;
  214. case MessageType.Query:
  215. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  216. break;
  217. case MessageType.Notice:
  218. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  219. break;
  220. }
  221. }
  222. }
  223. private void ViewIntroduction(CommandMessage command)
  224. {
  225. string channel = command.Arguments.ContainsKey("Channel") ? command.Arguments["Channel"] : command.Location;
  226. List<Dictionary<string, object>> results = GetIntroductionList(channel, command.Nick.Nickname);
  227. int num = 0;
  228. if (command.Arguments.ContainsKey("ID"))
  229. {
  230. if (int.TryParse(command.Arguments["ID"], out num))
  231. {
  232. if (results.Count >= num)
  233. {
  234. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", num, results[num - 1]["message"]);
  235. switch (command.MessageType)
  236. {
  237. case MessageType.Channel:
  238. Bot.IRC.SendPrivateMessage(command.Location, introMessage);
  239. break;
  240. case MessageType.Query:
  241. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, introMessage);
  242. break;
  243. case MessageType.Notice:
  244. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  245. break;
  246. }
  247. }
  248. else
  249. {
  250. string invalid = "Invalid introduction ID.";
  251. switch (command.MessageType)
  252. {
  253. case MessageType.Channel:
  254. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  255. break;
  256. case MessageType.Query:
  257. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  258. break;
  259. case MessageType.Notice:
  260. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  261. break;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. string invalid = "Invalid introduction ID.";
  268. switch (command.MessageType)
  269. {
  270. case MessageType.Channel:
  271. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  272. break;
  273. case MessageType.Query:
  274. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  275. break;
  276. case MessageType.Notice:
  277. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  278. break;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. if (results.Any())
  285. {
  286. for (int i = 0; i < results.Count; i++)
  287. {
  288. string introMessage = string.Format("Introduction #\u0002{0}\u0002: {1}", i + 1, results[i]["message"]);
  289. switch (command.MessageType)
  290. {
  291. case MessageType.Channel:
  292. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  293. break;
  294. case MessageType.Query:
  295. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, introMessage);
  296. break;
  297. case MessageType.Notice:
  298. Bot.IRC.SendNotice(command.Nick.Nickname, introMessage);
  299. break;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. string invalid = "You do not have any introductions set.";
  306. switch (command.MessageType)
  307. {
  308. case MessageType.Channel:
  309. Bot.IRC.SendPrivateMessage(command.Location, invalid);
  310. break;
  311. case MessageType.Query:
  312. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, invalid);
  313. break;
  314. case MessageType.Notice:
  315. Bot.IRC.SendNotice(command.Nick.Nickname, invalid);
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. private List<Dictionary<string, object>> GetIntroductionList(string channel, string nickname)
  322. {
  323. Database database = new Database(Bot.ServerConfig.Database);
  324. // Check to see if they have reached the max number of introductions
  325. string search = "SELECT `introductions`.`id`, `introductions`.`message` FROM `introductions` INNER JOIN `nicks` " +
  326. "ON `introductions`.`nick_id` = `nicks`.`id` " +
  327. "INNER JOIN `channels` " +
  328. "ON `introductions`.`channel_id` = `channels`.`id` " +
  329. "INNER JOIN `servers` " +
  330. "ON `nicks`.`server_id` = `servers`.`id` " +
  331. "WHERE `servers`.`name` = {0} AND `channels`.`name` = {1} AND `nicks`.`nickname` = {2}";
  332. return database.Query(search, new object[] { Bot.ServerConfig.Name, channel, nickname });
  333. }
  334. }
  335. }