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 7.4KB

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