The easy to use and full featured Irc Bot everyone is talking about!
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using Combot.IRCServices;
  8. namespace Combot.Modules
  9. {
  10. public class Module
  11. {
  12. public string Name { get; set; }
  13. public string ClassName { get; set; }
  14. public bool Enabled { get; set; }
  15. public List<string> ChannelBlacklist { get; set; }
  16. public List<string> NickBlacklist { get; set; }
  17. public List<Command> Commands { get; set; }
  18. public List<Option> Options { get; set; }
  19. public bool Loaded { get; set; }
  20. public bool ShouldSerializeLoaded()
  21. {
  22. return false;
  23. }
  24. protected Bot Bot;
  25. public Module()
  26. {
  27. SetDefaults();
  28. }
  29. public void HandleCommandEvent(CommandMessage command)
  30. {
  31. // Check to make sure the command exists, the nick or channel isn't on a blacklist, and the module is loaded.
  32. if (Loaded
  33. && !ChannelBlacklist.Contains(command.Location)
  34. && !NickBlacklist.Contains(command.Nick.Nickname)
  35. && Commands.Exists(c => c.Triggers.Contains(command.Command)
  36. && c.Enabled
  37. && !c.ChannelBlacklist.Contains(command.Location)
  38. && !c.NickBlacklist.Contains(command.Nick.Nickname)
  39. )
  40. )
  41. {
  42. // Figure out access of the nick
  43. Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
  44. List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
  45. foreach (PrivilegeMode privilege in command.Nick.Privileges)
  46. {
  47. nickAccessTypes.Add(Bot.PrivilegeModeMapping[privilege]);
  48. }
  49. if (Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Modes.Contains(UserMode.r))
  50. {
  51. nickAccessTypes.Add(AccessType.Owner);
  52. }
  53. command.Access.AddRange(nickAccessTypes);
  54. // If they have the correct access for the command, send it
  55. if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
  56. {
  57. ParseCommand(command);
  58. }
  59. else
  60. {
  61. string noAccessMessage = string.Format("You do not have access to use \u0002{0}\u000F.", command.Command);
  62. switch (command.MessageType)
  63. {
  64. case MessageType.Channel:
  65. Bot.IRC.SendPrivateMessage(command.Location, noAccessMessage);
  66. break;
  67. case MessageType.Query:
  68. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noAccessMessage);
  69. break;
  70. case MessageType.Notice:
  71. Bot.IRC.SendNotice(command.Nick.Nickname, noAccessMessage);
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. virtual public void Initialize() { }
  78. virtual public void ParseCommand(CommandMessage command) { }
  79. public void SetDefaults()
  80. {
  81. Name = string.Empty;
  82. ClassName = string.Empty;
  83. Enabled = false;
  84. ChannelBlacklist = new List<string>();
  85. NickBlacklist = new List<string>();
  86. Loaded = false;
  87. Commands = new List<Command>();
  88. Options = new List<Option>();
  89. }
  90. public void Copy(Module module)
  91. {
  92. Name = module.Name;
  93. ClassName = module.ClassName;
  94. Enabled = module.Enabled;
  95. ChannelBlacklist = new List<string>();
  96. foreach (string channel in module.ChannelBlacklist)
  97. {
  98. ChannelBlacklist.Add(channel);
  99. }
  100. NickBlacklist = new List<string>();
  101. foreach (string nick in module.NickBlacklist)
  102. {
  103. NickBlacklist.Add(nick);
  104. }
  105. Commands = new List<Command>();
  106. foreach (Command command in module.Commands)
  107. {
  108. Command newCommand = new Command();
  109. newCommand.Copy(command);
  110. Commands.Add(newCommand);
  111. }
  112. Options = new List<Option>();
  113. foreach (Option option in module.Options)
  114. {
  115. Option newOption = new Option();
  116. newOption.Copy(option);
  117. Options.Add(newOption);
  118. }
  119. }
  120. public Module CreateInstance(Bot bot)
  121. {
  122. Module newModule = new Module();
  123. if (!Loaded)
  124. {
  125. //create the class base on string
  126. //note : include the namespace and class name (namespace=Bot.Modules, class name=<class_name>)
  127. Assembly a = Assembly.Load("Combot");
  128. Type t = a.GetType("Combot.Modules.ModuleClasses." + ClassName);
  129. //check to see if the class is instantiated or not
  130. if (t != null)
  131. {
  132. newModule = (Module)Activator.CreateInstance(t);
  133. newModule.Copy(this);
  134. newModule.Loaded = true;
  135. newModule.Bot = bot;
  136. newModule.Initialize();
  137. }
  138. }
  139. return newModule;
  140. }
  141. public dynamic GetOptionValue(string name)
  142. {
  143. dynamic foundValue = null;
  144. Option foundOption = Options.Find(opt => opt.Name == name);
  145. if (foundOption != null)
  146. {
  147. foundValue = foundOption.Value;
  148. if (foundValue == null)
  149. {
  150. foundValue = string.Empty;
  151. }
  152. }
  153. return foundValue;
  154. }
  155. }
  156. }