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.

Help.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Runtime.Remoting.Messaging;
  4. namespace Combot.Modules.Plugins
  5. {
  6. public class Help : Module
  7. {
  8. public override void Initialize()
  9. {
  10. Bot.CommandReceivedEvent += HandleCommandEvent;
  11. }
  12. public override void ParseCommand(CommandMessage command)
  13. {
  14. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  15. if (foundCommand.Name == "Help")
  16. {
  17. if (command.Arguments.Count == 0)
  18. {
  19. SendFullHelp(command.Nick.Nickname, command.Access);
  20. }
  21. else if (command.Arguments.ContainsKey("Command"))
  22. {
  23. SendCommandHelp(command);
  24. }
  25. }
  26. }
  27. private void SendFullHelp(string recipient, List<AccessType> access)
  28. {
  29. Bot.IRC.Command.SendNotice(recipient, string.Format("You have the following commands available to use. " +
  30. "To use them either type \u0002{1}\u001Fcommand trigger\u001F\u0002 into a channel, send a private message by typing \u0002/msg {0} \u001Fcommand trigger\u001F\u0002, or send a notice by typing \u0002/notice {0} \u001Fcommand trigger\u001F\u0002.",
  31. Bot.IRC.Nickname, Bot.ServerConfig.CommandPrefix));
  32. Bot.IRC.Command.SendNotice(recipient, "\u200B");
  33. List<string> commandList = new List<string>();
  34. foreach (Module module in Bot.Modules)
  35. {
  36. module.Commands.ForEach(command =>
  37. {
  38. if (command.AllowedAccess.Exists(allowed => access.Contains(allowed)) && command.ShowHelp)
  39. {
  40. commandList.Add(command.Name);
  41. }
  42. });
  43. }
  44. Bot.IRC.Command.SendNotice(recipient, string.Format("\u0002{0}\u0002", string.Join("\u0002, \u0002", commandList)));
  45. Bot.IRC.Command.SendNotice(recipient, "\u200B");
  46. Bot.IRC.Command.SendNotice(recipient, string.Format("For more information on a specific command, including viewing the triggers, type \u0002{0}help \u001Fcommand\u001F\u0002.", Bot.ServerConfig.CommandPrefix));
  47. }
  48. private void SendCommandHelp(CommandMessage command)
  49. {
  50. string helpCommand = command.Arguments["Command"].ToString();
  51. Module foundModule = Bot.Modules.Find(mod => mod.Commands.Exists(cmd => (cmd.Name.ToLower() == helpCommand.ToLower() || cmd.Triggers.Contains(helpCommand)) && cmd.ShowHelp));
  52. if (foundModule != null)
  53. {
  54. Command foundCommand = foundModule.Commands.Find(cmd => (cmd.Name.ToLower() == helpCommand.ToLower() || cmd.Triggers.Contains(helpCommand)));
  55. if (foundCommand != null)
  56. {
  57. if (foundCommand.AllowedAccess.Exists(allowed => command.Access.Contains(allowed)))
  58. {
  59. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("Help information for \u0002{0}\u0002", foundCommand.Name));
  60. if (foundCommand.Description != string.Empty)
  61. {
  62. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("{0}", foundCommand.Description));
  63. }
  64. Bot.IRC.Command.SendNotice(command.Nick.Nickname, "\u200B");
  65. Dictionary<string, string> argDescriptions = new Dictionary<string, string>();
  66. int index = 0;
  67. for (int i = 0; i < foundCommand.AllowedMessageTypes.Count; i++)
  68. {
  69. MessageType messageType = foundCommand.AllowedMessageTypes[i];
  70. // Generate Message Syntax
  71. string messageSyntax = string.Empty;
  72. switch (messageType)
  73. {
  74. case MessageType.Channel:
  75. messageSyntax = "\u0002/msg \u001Fchannel\u001F\u0002";
  76. break;
  77. case MessageType.Query:
  78. messageSyntax = string.Format("\u0002/msg {0}\u0002", Bot.IRC.Nickname);
  79. break;
  80. case MessageType.Notice:
  81. messageSyntax = string.Format("\u0002/notice {0}\u0002", Bot.IRC.Nickname);
  82. break;
  83. }
  84. List<CommandArgument> validArguments = foundCommand.Arguments.FindAll(arg => arg.MessageTypes.Contains(messageType));
  85. string argHelp = string.Empty;
  86. if (validArguments.Count > 0)
  87. {
  88. argHelp = string.Format(" \u0002{0}\u0002", string.Join(" ", validArguments.Select(arg =>
  89. {
  90. string argString = string.Empty;
  91. if (arg.DependentArguments.Count > 0)
  92. {
  93. argString = "(";
  94. }
  95. if (arg.Required)
  96. {
  97. argString += "\u001F" + arg.Name + "\u001F";
  98. }
  99. else
  100. {
  101. argString += "[\u001F" + arg.Name + "\u001F]";
  102. }
  103. if (arg.DependentArguments.Count > 0)
  104. {
  105. argString += string.Format("\u0002:When {0}\u0002)", string.Join(" or ", arg.DependentArguments.Select(dep => { return string.Format("\u0002\u001F{0}\u001F\u0002=\u0002{1}\u0002", dep.Name, string.Join(",", dep.Values)); })));
  106. }
  107. return argString;
  108. })));
  109. }
  110. if (foundCommand.Triggers.Any())
  111. {
  112. string triggerString = (foundCommand.Triggers.Count > 1) ? string.Format("({0})", string.Join("|", foundCommand.Triggers)) : foundCommand.Triggers.First();
  113. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("Syntax: {0} {1}\u0002{2}\u0002{3}", messageSyntax, Bot.ServerConfig.CommandPrefix, triggerString, argHelp));
  114. }
  115. // Display argument help
  116. if (validArguments.Count > 0)
  117. {
  118. validArguments.ForEach(arg =>
  119. {
  120. string commandDesc = string.Empty;
  121. if (arg.Description != string.Empty)
  122. {
  123. commandDesc = string.Format(" - {0}", arg.Description);
  124. }
  125. if (!argDescriptions.ContainsKey(arg.Name))
  126. {
  127. argDescriptions.Add(arg.Name, string.Format("\t\t\u0002{0}\u0002{1}", arg.Name, commandDesc));
  128. }
  129. if (arg.AllowedValues.Count > 0)
  130. {
  131. string argAllowed = string.Format("\t\t\t\tAllowed Values: \u0002{0}\u0002", string.Join(", ", arg.AllowedValues));
  132. bool containsAllowed = false;
  133. for (int x = 0; x < index; x++)
  134. {
  135. if (argDescriptions.ContainsKey(arg.Name + " - Allowed Values " + x))
  136. {
  137. if (argDescriptions[arg.Name + " - Allowed Values " + x] == argAllowed)
  138. {
  139. containsAllowed = true;
  140. break;
  141. }
  142. }
  143. }
  144. if (!containsAllowed)
  145. {
  146. argDescriptions.Add(arg.Name + " - Allowed Values " + index, argAllowed);
  147. index++;
  148. }
  149. }
  150. });
  151. }
  152. }
  153. Bot.IRC.Command.SendNotice(command.Nick.Nickname, "\u200B");
  154. if (argDescriptions.Count > 0)
  155. {
  156. foreach (KeyValuePair<string, string> argDescription in argDescriptions)
  157. {
  158. Bot.IRC.Command.SendNotice(command.Nick.Nickname, argDescription.Value);
  159. }
  160. }
  161. }
  162. else
  163. {
  164. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("You do not have access to view help on \u0002{0}\u0002.", helpCommand));
  165. }
  166. }
  167. else
  168. {
  169. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("The command \u0002{0}\u0002 does not exist.", helpCommand));
  170. }
  171. }
  172. else
  173. {
  174. Bot.IRC.Command.SendNotice(command.Nick.Nickname, string.Format("The command \u0002{0}\u0002 does not exist.", helpCommand));
  175. }
  176. }
  177. }
  178. }