The easy to use and full featured Irc Bot everyone is talking about!
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Messaging.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 Messaging : Module
  9. {
  10. public override void Initialize()
  11. {
  12. Bot.CommandReceivedEvent += HandleCommandEvent;
  13. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  14. Bot.IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessage;
  15. Bot.IRC.Message.ChannelNoticeReceivedEvent += HandleChannelNotice;
  16. Bot.IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNotice;
  17. }
  18. public override void ParseCommand(CommandMessage command)
  19. {
  20. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  21. switch (foundCommand.Name)
  22. {
  23. case "Message":
  24. AddMessage(command);
  25. break;
  26. case "Anonymous Message":
  27. AddMessage(command, true);
  28. break;
  29. }
  30. }
  31. private void HandleChannelMessage(object sender, ChannelMessage message)
  32. {
  33. CheckMessages(message.Sender.Nickname);
  34. }
  35. private void HandlePrivateMessage(object sender, PrivateMessage message)
  36. {
  37. CheckMessages(message.Sender.Nickname);
  38. }
  39. private void HandleChannelNotice(object sender, ChannelNotice message)
  40. {
  41. CheckMessages(message.Sender.Nickname);
  42. }
  43. private void HandlePrivateNotice(object sender, PrivateNotice message)
  44. {
  45. CheckMessages(message.Sender.Nickname);
  46. }
  47. private void AddMessage(CommandMessage command, bool anonymous = false)
  48. {
  49. List<Dictionary<string, object>> currentMessages = GetSentMessages(command.Arguments["Nickname"]);
  50. int numMessages = currentMessages.Select(msg => GetNickname((int) msg["nick_id"]) == command.Nick.Nickname).Count();
  51. if (numMessages < GetOptionValue("Max Messages"))
  52. {
  53. AddNick(command.Nick.Nickname);
  54. AddNick(command.Arguments["Nickname"]);
  55. Database database = new Database(Bot.ServerConfig.Database);
  56. string query = "INSERT INTO `messages` SET " +
  57. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  58. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  59. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  60. "`message` = {5}, " +
  61. "`anonymous` = {6}, " +
  62. "`date_posted` = {7}";
  63. database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, command.Arguments["Nickname"], Bot.ServerConfig.Name, command.Nick.Nickname, command.Arguments["Message"], anonymous, command.TimeStamp });
  64. string message = string.Format("I will send your message to \u0002{0}\u0002 as soon as I see them.", command.Arguments["Nickname"]);
  65. switch (command.MessageType)
  66. {
  67. case MessageType.Channel:
  68. Bot.IRC.SendPrivateMessage(command.Location, message);
  69. break;
  70. case MessageType.Query:
  71. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, message);
  72. break;
  73. case MessageType.Notice:
  74. Bot.IRC.SendNotice(command.Nick.Nickname, message);
  75. break;
  76. }
  77. }
  78. else
  79. {
  80. string maxMessage = string.Format("You already have sent the maximum number of messages to \u0002{0}\u0002. Wait until they have read their messages before sending another.");
  81. switch (command.MessageType)
  82. {
  83. case MessageType.Channel:
  84. Bot.IRC.SendPrivateMessage(command.Location, maxMessage);
  85. break;
  86. case MessageType.Query:
  87. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, maxMessage);
  88. break;
  89. case MessageType.Notice:
  90. Bot.IRC.SendNotice(command.Nick.Nickname, maxMessage);
  91. break;
  92. }
  93. }
  94. }
  95. private void CheckMessages(string nickname)
  96. {
  97. List<Dictionary<string, object>> receivedMessages = GetReceivedMessages(nickname);
  98. if (receivedMessages.Any())
  99. {
  100. for (int i = 0; i < receivedMessages.Count; i++)
  101. {
  102. DateTime dateSent = (DateTime)receivedMessages[i]["date_posted"];
  103. string message = receivedMessages[i]["message"].ToString();
  104. if ((bool) receivedMessages[i]["anonymous"])
  105. {
  106. Bot.IRC.SendPrivateMessage(nickname, string.Format("An anonymous sender has left you a message on \u0002{0}\u0002", dateSent.ToString("MMMM d, yyyy h:mm:ss tt")));
  107. Bot.IRC.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  108. }
  109. else
  110. {
  111. string sentNick = GetNickname((int) receivedMessages[i]["sender_nick_id"]);
  112. Bot.IRC.SendPrivateMessage(nickname, string.Format("\u0002{0}\u0002 has left you a message on \u0002{1}\u0002", sentNick, dateSent.ToString("MMMM d, yyyy h:mm:ss tt")));
  113. Bot.IRC.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  114. Bot.IRC.SendPrivateMessage(nickname, string.Format("If you would like to reply to them, please type \u0002{0}{1} {2} \u001FMessage\u001F\u0002", Bot.ServerConfig.CommandPrefix, Commands.Find(cmd => cmd.Name == "Message").Triggers.First(), sentNick));
  115. }
  116. DeleteMessage((int) receivedMessages[i]["id"]);
  117. }
  118. }
  119. }
  120. private List<Dictionary<string, object>> GetSentMessages(string nick)
  121. {
  122. Database database = new Database(Bot.ServerConfig.Database);
  123. string search = "SELECT `messages`.`message`, `messages`.`nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  124. "INNER JOIN `nicks` " +
  125. "ON `messages`.`sender_nick_id` = `nicks`.`id` " +
  126. "INNER JOIN `servers` " +
  127. "ON `messages`.`server_id` = `servers`.`id` " +
  128. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  129. return database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  130. }
  131. private List<Dictionary<string, object>> GetReceivedMessages(string nick)
  132. {
  133. Database database = new Database(Bot.ServerConfig.Database);
  134. string search = "SELECT `messages`.`id`, `messages`.`message`, `messages`.`sender_nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  135. "INNER JOIN `nicks` " +
  136. "ON `messages`.`nick_id` = `nicks`.`id` " +
  137. "INNER JOIN `servers` " +
  138. "ON `messages`.`server_id` = `servers`.`id` " +
  139. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  140. return database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  141. }
  142. private void DeleteMessage(int messageId)
  143. {
  144. Database database = new Database(Bot.ServerConfig.Database);
  145. string query = "DELETE FROM `messages` " +
  146. "WHERE `id` = {0}";
  147. database.Execute(query, new object[] { messageId });
  148. }
  149. }
  150. }