A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

customizing.rst 13 KiB

12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. Customizing
  2. ===========
  3. The bot's working directory contains a :file:`commands` subdirectory and a
  4. :file:`tasks` subdirectory. Custom IRC commands can be placed in the former,
  5. whereas custom wiki bot tasks go into the latter. Developing custom modules is
  6. explained in detail in this documentation.
  7. Note that custom commands will override built-in commands and tasks with the
  8. same name.
  9. :py:class:`~earwigbot.bot.Bot` and :py:class:`~earwigbot.bot.BotConfig`
  10. -----------------------------------------------------------------------
  11. :py:class:`earwigbot.bot.Bot` is EarwigBot's main class. You don't have to
  12. instantiate this yourself, but it's good to be familiar with its attributes and
  13. methods, because it is the main way to communicate with other parts of the bot.
  14. A :py:class:`~earwigbot.bot.Bot` object is accessible as an attribute of
  15. commands and tasks (i.e., :py:attr:`self.bot`).
  16. The most useful attributes are:
  17. - :py:attr:`~earwigbot.bot.Bot.config`: an instance of
  18. :py:class:`~earwigbot.config.BotConfig`, for accessing the bot's
  19. configuration data (see below).
  20. - :py:attr:`~earwigbot.bot.Bot.commands`: the bot's
  21. :py:class:`~earwigbot.managers.CommandManager`, which is used internally to
  22. run IRC commands (through
  23. :py:meth:`commands.call() <earwigbot.managers.CommandManager.call>`, which
  24. you shouldn't have to use); you can safely reload all commands with
  25. :py:meth:`commands.load() <earwigbot.managers._ResourceManager.load>`.
  26. - :py:attr:`~earwigbot.bot.Bot.tasks`: the bot's
  27. :py:class:`~earwigbot.managers.TaskManager`, which can be used to start tasks
  28. with :py:meth:`tasks.start(task_name, **kwargs)
  29. <earwigbot.managers.TaskManager.start>`. :py:meth:`tasks.load()
  30. <earwigbot.managers._ResourceManager.load>` can be used to safely reload all
  31. tasks.
  32. - :py:attr:`~earwigbot.bot.Bot.frontend` /
  33. :py:attr:`~earwigbot.bot.Bot.watcher`: instances of
  34. :py:class:`earwigbot.irc.Frontend <earwigbot.irc.frontend.Frontend>` and
  35. :py:class:`earwigbot.irc.Watcher <earwigbot.irc.watcher.Watcher>`,
  36. respectively, which represent the bot's connections to these two servers; you
  37. can, for example, send a message to the frontend with
  38. :py:meth:`frontend.say(chan, msg)
  39. <earwigbot.irc.connection.IRCConnection.say>` (more on communicating with IRC
  40. below).
  41. - :py:attr:`~earwigbot.bot.Bot.wiki`: interface with the
  42. :doc:`Wiki Toolset <toolset>`.
  43. - Finally, :py:meth:`~earwigbot.bot.Bot.restart` (restarts IRC components and
  44. reloads config, commands, and tasks) and :py:meth:`~earwigbot.bot.Bot.stop`
  45. can be used almost anywhere. Both take an optional "reason" that will be
  46. logged and used as the quit message when disconnecting from IRC.
  47. :py:class:`earwigbot.config.BotConfig` stores configuration information for the
  48. bot. Its docstrings explains what each attribute is used for, but essentially
  49. each "node" (one of :py:attr:`config.components
  50. <earwigbot.config.BotConfig.components>`,
  51. :py:attr:`~earwigbot.config.BotConfig.wiki`,
  52. :py:attr:`~earwigbot.config.BotConfig.irc`,
  53. :py:attr:`~earwigbot.config.BotConfig.commands`,
  54. :py:attr:`~earwigbot.config.BotConfig.tasks`, or
  55. :py:attr:`~earwigbot.config.BotConfig.metadata`) maps to a section
  56. of the bot's :file:`config.yml` file. For example, if :file:`config.yml`
  57. includes something like::
  58. irc:
  59. frontend:
  60. nick: MyAwesomeBot
  61. channels:
  62. - "##earwigbot"
  63. - "#channel"
  64. - "#other-channel"
  65. ...then :py:attr:`config.irc["frontend"]["nick"]` will be ``"MyAwesomeBot"``
  66. and :py:attr:`config.irc["frontend"]["channels"]` will be
  67. ``["##earwigbot", "#channel", "#other-channel"]``.
  68. Custom IRC commands
  69. -------------------
  70. Custom commands are subclasses of :py:class:`earwigbot.commands.Command` that
  71. override :py:class:`~earwigbot.commands.Command`'s
  72. :py:meth:`~earwigbot.commands.Command.process` (and optionally
  73. :py:meth:`~earwigbot.commands.Command.check`,
  74. :py:meth:`~earwigbot.commands.Command.setup`, or
  75. :py:meth:`~earwigbot.commands.Command.unload`) methods.
  76. :py:class:`~earwigbot.commands.Command`'s docstrings should explain what each
  77. attribute and method is for and what they should be overridden with, but these
  78. are the basics:
  79. - Class attribute :py:attr:`~earwigbot.commands.Command.name` is the name of
  80. the command. This must be specified.
  81. - Class attribute :py:attr:`~earwigbot.commands.Command.commands` is a list of
  82. names that will trigger this command. It defaults to the command's
  83. :py:attr:`~earwigbot.commands.Command.name`, but you can override it with
  84. multiple names to serve as aliases. This is handled by the default
  85. :py:meth:`~earwigbot.commands.Command.check` implementation (see below), so
  86. if :py:meth:`~earwigbot.commands.Command.check` is overridden, this is
  87. ignored by everything except the help_ command (so ``!help alias`` will
  88. trigger help for the actual command).
  89. - Class attribute :py:attr:`~earwigbot.commands.Command.hooks` is a list of the
  90. "IRC events" that this command might respond to. It defaults to ``["msg"]``,
  91. but options include ``"msg_private"`` (for private messages only),
  92. ``"msg_public"`` (for channel messages only), ``"join"`` (for when a user
  93. joins a channel), ``"part"`` (for when a user parts a channel), and ``"rc"``
  94. (for recent change messages from the IRC watcher). See the afc_status_ plugin
  95. for a command that responds to other hook types.
  96. - Method :py:meth:`~earwigbot.commands.Command.setup` is called *once* with no
  97. arguments immediately after the command is first loaded. Does nothing by
  98. default; treat it like an :py:meth:`__init__` if you want
  99. (:py:meth:`~earwigbot.tasks.Command.__init__` does things by default and a
  100. dedicated setup method is often easier than overriding
  101. :py:meth:`~earwigbot.tasks.Command.__init__` and using :py:obj:`super`).
  102. - Method :py:meth:`~earwigbot.commands.Command.check` is passed a
  103. :py:class:`~earwigbot.irc.data.Data` object, and should return ``True`` if
  104. you want to respond to this message, or ``False`` otherwise. The default
  105. behavior is to return ``True`` only if :py:attr:`data.is_command` is ``True``
  106. and :py:attr:`data.command` ``==``
  107. :py:attr:`~earwigbot.commands.Command.name` (or :py:attr:`data.command
  108. <earwigbot.irc.data.Data.command>` is in
  109. :py:attr:`~earwigbot.commands.Command.commands` if that list is overriden;
  110. see above), which is suitable for most cases. A possible reason for
  111. overriding is if you want to do something in response to events from a
  112. specific channel only. Note that by returning ``True``, you prevent any other
  113. commands from responding to this message.
  114. - Method :py:meth:`~earwigbot.commands.Command.process` is passed the same
  115. :py:class:`~earwigbot.irc.data.Data` object as
  116. :py:meth:`~earwigbot.commands.Command.check`, but only if
  117. :py:meth:`~earwigbot.commands.Command.check` returned ``True``. This is where
  118. the bulk of your command goes. To respond to IRC messages, there are a number
  119. of methods of :py:class:`~earwigbot.commands.Command` at your disposal. See
  120. the test_ command for a simple example, or look in
  121. :py:class:`~earwigbot.commands.Command`'s
  122. :py:meth:`~earwigbot.commands.Command.__init__` method for the full list.
  123. The most common ones are :py:meth:`say(chan_or_user, msg)
  124. <earwigbot.irc.connection.IRCConnection.say>`, :py:meth:`reply(data, msg)
  125. <earwigbot.irc.connection.IRCConnection.reply>` (convenience function; sends
  126. a reply to the issuer of the command in the channel it was received),
  127. :py:meth:`action(chan_or_user, msg)
  128. <earwigbot.irc.connection.IRCConnection.action>`,
  129. :py:meth:`notice(chan_or_user, msg)
  130. <earwigbot.irc.connection.IRCConnection.notice>`, :py:meth:`join(chan)
  131. <earwigbot.irc.connection.IRCConnection.join>`, and
  132. :py:meth:`part(chan) <earwigbot.irc.connection.IRCConnection.part>`.
  133. - Method :py:meth:`~earwigbot.commands.Command.unload` is called *once* with no
  134. arguments immediately before the command is unloaded, such as when someone
  135. uses ``!reload``. Does nothing by default.
  136. Commands have access to :py:attr:`config.commands[command_name]` for config
  137. information, which is a node in :file:`config.yml` like every other attribute
  138. of :py:attr:`bot.config`. This can be used to store, for example, API keys or
  139. SQL connection info, so that these can be easily changed without modifying the
  140. command itself.
  141. The command *class* doesn't need a specific name, but it should logically
  142. follow the command's name. The filename doesn't matter, but it is recommended
  143. to match the command name for readability. Multiple command classes are allowed
  144. in one file.
  145. The bot has a wide selection of built-in commands and plugins to act as sample
  146. code and/or to give ideas. Start with test_, and then check out chanops_ and
  147. afc_status_ for some more complicated scripts.
  148. By default, the bot loads every built-in and custom command available. You can
  149. disable *all* built-in commands with the config entry
  150. :py:attr:`config.commands["disable"]` set to ``True``, or a subset of commands
  151. by setting it to a list of command class names or module names. If using the
  152. former method, you can specifically enable certain built-in commands with
  153. :py:attr:`config.commands["enable"]` set to a list of command module names.
  154. Custom bot tasks
  155. ----------------
  156. Custom tasks are subclasses of :py:class:`earwigbot.tasks.Task` that
  157. override :py:class:`~earwigbot.tasks.Task`'s
  158. :py:meth:`~earwigbot.tasks.Task.run` (and optionally
  159. :py:meth:`~earwigbot.tasks.Task.setup` or
  160. :py:meth:`~earwigbot.tasks.Task.unload`) methods.
  161. :py:class:`~earwigbot.tasks.Task`'s docstrings should explain what each
  162. attribute and method is for and what they should be overridden with, but these
  163. are the basics:
  164. - Class attribute :py:attr:`~earwigbot.tasks.Task.name` is the name of the
  165. task. This must be specified.
  166. - Class attribute :py:attr:`~earwigbot.tasks.Task.number` can be used to store
  167. an optional "task number", possibly for use in edit summaries (to be
  168. generated with :py:meth:`~earwigbot.tasks.Task.make_summary`). For
  169. example, EarwigBot's :py:attr:`config.wiki["summary"]` is
  170. ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the
  171. task class's :py:meth:`make_summary(comment)
  172. <earwigbot.tasks.Task.make_summary>` method will take and replace
  173. ``$1`` with the task number and ``$2`` with the details of the edit.
  174. Additionally, :py:meth:`~earwigbot.tasks.Task.shutoff_enabled` (which checks
  175. whether the bot has been told to stop on-wiki by checking the content of a
  176. particular page) can check a different page for each task using similar
  177. variables. EarwigBot's :py:attr:`config.wiki["shutoff"]["page"]` is
  178. ``"User:$1/Shutoff/Task $2"``; ``$1`` is substituted with the bot's username,
  179. and ``$2`` is substituted with the task number, so, e.g., task #14 checks the
  180. page ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not*
  181. match :py:attr:`config.wiki["shutoff"]["disabled"]` (``"run"`` by default),
  182. then shutoff is considered to be *enabled* and
  183. :py:meth:`~earwigbot.tasks.Task.shutoff_enabled` will return ``True``,
  184. indicating the task should not run. If you don't intend to use either of
  185. these methods, feel free to leave this attribute blank.
  186. - Method :py:meth:`~earwigbot.tasks.Task.setup` is called *once* with no
  187. arguments immediately after the task is first loaded. Does nothing by
  188. default; treat it like an :py:meth:`__init__` if you want
  189. (:py:meth:`~earwigbot.tasks.Task.__init__` does things by default and a
  190. dedicated setup method is often easier than overriding
  191. :py:meth:`~earwigbot.tasks.Task.__init__` and using :py:obj:`super`).
  192. - Method :py:meth:`~earwigbot.tasks.Task.run` is called with any number of
  193. keyword arguments every time the task is executed (by
  194. :py:meth:`tasks.start(task_name, **kwargs)
  195. <earwigbot.managers.TaskManager.start>`, usually). This is where the bulk of
  196. the task's code goes. For interfacing with MediaWiki sites, read up on the
  197. :doc:`Wiki Toolset <toolset>`.
  198. - Method :py:meth:`~earwigbot.tasks.Task.unload` is called *once* with no
  199. arguments immediately before the task is unloaded. Does nothing by
  200. default.
  201. Tasks have access to :py:attr:`config.tasks[task_name]` for config information,
  202. which is a node in :file:`config.yml` like every other attribute of
  203. :py:attr:`bot.config`. This can be used to store, for example, edit summaries
  204. or templates to append to user talk pages, so that these can be easily changed
  205. without modifying the task itself.
  206. The task *class* doesn't need a specific name, but it should logically follow
  207. the task's name. The filename doesn't matter, but it is recommended to match
  208. the task name for readability. Multiple tasks classes are allowed in one file.
  209. See the built-in wikiproject_tagger_ task for a relatively straightforward
  210. task, or the afc_statistics_ plugin for a more complicated one.
  211. .. _help: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/help.py
  212. .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py
  213. .. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py
  214. .. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py
  215. .. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py
  216. .. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py