The easy to use and full featured Irc Bot everyone is talking about!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Messaging.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Combot.Databases;
  5. using Combot.IRCServices;
  6. using Combot.IRCServices.Messaging;
  7. namespace Combot.Modules.Plugins
  8. {
  9. public class Messaging : Module
  10. {
  11. public override void Initialize()
  12. {
  13. Bot.CommandReceivedEvent += HandleCommandEvent;
  14. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  15. Bot.IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessage;
  16. Bot.IRC.Message.ChannelNoticeReceivedEvent += HandleChannelNotice;
  17. Bot.IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNotice;
  18. }
  19. public override void ParseCommand(CommandMessage command)
  20. {
  21. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  22. switch (foundCommand.Name)
  23. {
  24. case "Message":
  25. AddMessage(command);
  26. break;
  27. case "Anonymous Message":
  28. AddMessage(command, true);
  29. break;
  30. }
  31. }
  32. private void HandleChannelMessage(object sender, ChannelMessage message)
  33. {
  34. if (Enabled)
  35. {
  36. CheckMessages(message.Sender.Nickname);
  37. }
  38. }
  39. private void HandlePrivateMessage(object sender, PrivateMessage message)
  40. {
  41. if (Enabled)
  42. {
  43. CheckMessages(message.Sender.Nickname);
  44. }
  45. }
  46. private void HandleChannelNotice(object sender, ChannelNotice message)
  47. {
  48. if (Enabled)
  49. {
  50. CheckMessages(message.Sender.Nickname);
  51. }
  52. }
  53. private void HandlePrivateNotice(object sender, PrivateNotice message)
  54. {
  55. if (Enabled)
  56. {
  57. CheckMessages(message.Sender.Nickname);
  58. }
  59. }
  60. private void AddMessage(CommandMessage command, bool anonymous = false)
  61. {
  62. List<Dictionary<string, object>> currentMessages = GetSentMessages(command.Arguments["Nickname"], command.Nick.Nickname);
  63. int numMessages = currentMessages.Count();
  64. int maxMessages = Convert.ToInt32(GetOptionValue("Max Messages"));
  65. if (numMessages < maxMessages)
  66. {
  67. AddNick(command.Nick);
  68. Nick newNick = new Nick();
  69. Channel foundChannel = Bot.IRC.Channels.Find(chan => chan.Nicks.Exists(nick => nick.Nickname == command.Arguments["Nickname"]));
  70. if (foundChannel != null)
  71. {
  72. newNick = foundChannel.GetNick(command.Arguments["Nickname"]);
  73. }
  74. else
  75. {
  76. newNick.Nickname = command.Arguments["Nickname"];
  77. }
  78. AddNick(newNick);
  79. string query = "INSERT INTO `messages` SET " +
  80. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  81. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nicks`.`nickname` = {2}), " +
  82. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nicks`.`nickname` = {4}), " +
  83. "`message` = {5}, " +
  84. "`anonymous` = {6}, " +
  85. "`date_posted` = {7}";
  86. Bot.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 });
  87. string message = string.Format("I will send your message to \u0002{0}\u0002 as soon as I see them.", command.Arguments["Nickname"]);
  88. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, message);
  89. }
  90. else
  91. {
  92. 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.", command.Arguments["Nickname"]);
  93. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, maxMessage);
  94. }
  95. }
  96. private void CheckMessages(string nickname)
  97. {
  98. List<Dictionary<string, object>> receivedMessages = GetReceivedMessages(nickname);
  99. if (receivedMessages.Any())
  100. {
  101. for (int i = 0; i < receivedMessages.Count; i++)
  102. {
  103. DateTime dateSent = (DateTime)receivedMessages[i]["date_posted"];
  104. string message = receivedMessages[i]["message"].ToString();
  105. if ((bool) receivedMessages[i]["anonymous"])
  106. {
  107. Bot.IRC.Command.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")));
  108. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  109. }
  110. else
  111. {
  112. string sentNick = GetNickname((int) receivedMessages[i]["sender_nick_id"]);
  113. Bot.IRC.Command.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")));
  114. Bot.IRC.Command.SendPrivateMessage(nickname, string.Format("\"{0}\"", message));
  115. Bot.IRC.Command.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));
  116. }
  117. DeleteMessage((int) receivedMessages[i]["id"]);
  118. }
  119. }
  120. }
  121. private List<Dictionary<string, object>> GetSentMessages(string nick, string sender)
  122. {
  123. string search = "SELECT `messages`.`message`, `messages`.`nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` WHERE " +
  124. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}) AND " +
  125. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}) AND " +
  126. "`sender_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4})";
  127. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, nick, Bot.ServerConfig.Name, sender });
  128. }
  129. private List<Dictionary<string, object>> GetReceivedMessages(string nick)
  130. {
  131. string search = "SELECT `messages`.`id`, `messages`.`message`, `messages`.`sender_nick_id`, `messages`.`date_posted`, `messages`.`anonymous` FROM `messages` " +
  132. "INNER JOIN `nicks` " +
  133. "ON `messages`.`nick_id` = `nicks`.`id` " +
  134. "INNER JOIN `servers` " +
  135. "ON `messages`.`server_id` = `servers`.`id` " +
  136. "WHERE `servers`.`name` = {0} AND `nicks`.`nickname` = {1}";
  137. return Bot.Database.Query(search, new object[] { Bot.ServerConfig.Name, nick });
  138. }
  139. private void DeleteMessage(int messageId)
  140. {
  141. string query = "DELETE FROM `messages` " +
  142. "WHERE `id` = {0}";
  143. Bot.Database.Execute(query, new object[] { messageId });
  144. }
  145. }
  146. }