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.

Module.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.ChannelBlacklist.Contains(command.Location)
  37. && !c.NickBlacklist.Contains(command.Nick.Nickname)
  38. )
  39. )
  40. {
  41. // Figure out access of the nick
  42. Command cmd = Commands.Find(c => c.Triggers.Contains(command.Command));
  43. List<AccessType> nickAccessTypes = new List<AccessType>() { AccessType.User };
  44. foreach (PrivilegeMode privilege in command.Nick.Privileges)
  45. {
  46. nickAccessTypes.Add(Bot.AccessTypeMapping[privilege]);
  47. }
  48. if (Bot.ServerConfig.Owners.Contains(command.Nick.Nickname) && command.Nick.Identified)
  49. {
  50. nickAccessTypes.Add(AccessType.Owner);
  51. }
  52. // If they have the correct access for the command, send it
  53. if (cmd.AllowedAccess.Exists(access => nickAccessTypes.Contains(access)))
  54. {
  55. ParseCommand(command);
  56. }
  57. }
  58. }
  59. virtual public void Initialize() { }
  60. virtual public void ParseCommand(CommandMessage command) { }
  61. public void SetDefaults()
  62. {
  63. Name = string.Empty;
  64. ClassName = string.Empty;
  65. Enabled = false;
  66. ChannelBlacklist = new List<string>();
  67. NickBlacklist = new List<string>();
  68. Loaded = false;
  69. Commands = new List<Command>();
  70. Options = new List<Option>();
  71. }
  72. public void Copy(Module module)
  73. {
  74. Name = module.Name;
  75. ClassName = module.ClassName;
  76. Enabled = module.Enabled;
  77. ChannelBlacklist = new List<string>();
  78. foreach (string channel in module.ChannelBlacklist)
  79. {
  80. ChannelBlacklist.Add(channel);
  81. }
  82. NickBlacklist = new List<string>();
  83. foreach (string nick in module.NickBlacklist)
  84. {
  85. NickBlacklist.Add(nick);
  86. }
  87. Commands = new List<Command>();
  88. foreach (Command command in module.Commands)
  89. {
  90. Command newCommand = new Command();
  91. newCommand.Copy(command);
  92. Commands.Add(newCommand);
  93. }
  94. Options = new List<Option>();
  95. foreach (Option option in module.Options)
  96. {
  97. Option newOption = new Option();
  98. newOption.Copy(option);
  99. Options.Add(newOption);
  100. }
  101. }
  102. public Module CreateInstance(Bot bot)
  103. {
  104. Module newModule = new Module();
  105. if (!Loaded)
  106. {
  107. //create the class base on string
  108. //note : include the namespace and class name (namespace=Bot.Modules, class name=<class_name>)
  109. Assembly a = Assembly.Load("Combot");
  110. Type t = a.GetType("Combot.Modules.ModuleClasses." + ClassName);
  111. //check to see if the class is instantiated or not
  112. if (t != null)
  113. {
  114. newModule = (Module)Activator.CreateInstance(t);
  115. newModule.Copy(this);
  116. newModule.Loaded = true;
  117. newModule.Bot = bot;
  118. newModule.Initialize();
  119. }
  120. }
  121. return newModule;
  122. }
  123. }
  124. }