A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

229 lignes
12 KiB

  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.bot.BotConfig`, for accessing the bot's configuration
  19. 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 :py:meth:`bot.commands.call`, which you shouldn't
  23. have to use); you can safely reload all commands with
  24. :py:meth:`~earwigbot.bot.Bot.commands.load`.
  25. - :py:attr:`~earwigbot.bot.Bot.tasks`: the bot's
  26. :py:class:`~earwigbot.managers.TaskManager`, which can be used to start tasks
  27. with :py:attr:`~earwigbot.bot.Bot.tasks.start(task_name, **kwargs)`.
  28. :py:meth:`~earwigbot.bot.Bot.tasks.load` can be used to safely reload all
  29. tasks.
  30. - :py:attr:`~earwigbot.bot.Bot.frontend` /
  31. :py:attr:`~earwigbot.bot.Bot.watcher`: instances of
  32. :py:class:`earwigbot.irc.Frontend` and :py:class:`earwigbot.irc.Watcher`,
  33. respectively, which represent the bot's connections to these two servers; you
  34. can, for example, send a message to the frontend with
  35. :py:attr:`~earwigbot.bot.Bot.frontend.say(chan, msg)` (more on communicating
  36. with IRC below).
  37. - :py:attr:`~earwigbot.bot.Bot.wiki`: interface with the
  38. :doc:`Wiki Toolset <toolset>`.
  39. - Finally, :py:meth:`~earwigbot.bot.Bot.restart` (restarts IRC components and
  40. reloads config, commands, and tasks) and :py:meth:`~earwigbot.bot.Bot.stop`
  41. can be used almost anywhere. Both take an optional "reason" that will be
  42. logged and used as the quit message when disconnecting from IRC.
  43. :py:class:`earwigbot.config.BotConfig` stores configuration information for the
  44. bot. Its docstring explains what each attribute is used for, but essentially
  45. each "node" (one of :py:attr:`config.components`, :py:attr:`wiki`,
  46. :py:attr:`tasks`, :py:attr:`tasks`, or :py:attr:`metadata`) maps to a section
  47. of the bot's :file:`config.yml` file. For example, if :file:`config.yml`
  48. includes something like::
  49. irc:
  50. frontend:
  51. nick: MyAwesomeBot
  52. channels:
  53. - "##earwigbot"
  54. - "#channel"
  55. - "#other-channel"
  56. ...then :py:attr:`config.irc["frontend"]["nick"]` will be ``"MyAwesomeBot"``
  57. and :py:attr:`config.irc["frontend"]["channels"]` will be
  58. ``["##earwigbot", "#channel", "#other-channel"]``.
  59. Custom IRC commands
  60. -------------------
  61. Custom commands are subclasses of :py:class:`earwigbot.commands.BaseCommand`
  62. that override :py:class:`~earwigbot.commands.BaseCommand`'s
  63. :py:meth:`~earwigbot.commands.BaseCommand.process` (and optionally
  64. :py:meth:`~earwigbot.commands.BaseCommand.check`) methods.
  65. :py:class:`~earwigbot.commands.BaseCommand`'s docstrings should explain what
  66. each attribute and method is for and what they should be overridden with, but
  67. these are the basics:
  68. - Class attribute :py:attr:`~earwigbot.commands.BaseCommand.name` is the name
  69. of the command. This must be specified.
  70. - Class attribute :py:attr:`~earwigbot.commands.BaseCommand.hooks` is a list of
  71. the "IRC events" that this command might respond to. It defaults to
  72. ``["msg"]``, but options include ``"msg_private"`` (for private messages
  73. only), ``"msg_public"`` (for channel messages only), and ``"join"`` (for when
  74. a user joins a channel). See the afc_status_ plugin for a command that
  75. responds to other hook types.
  76. - Method :py:meth:`~earwigbot.commands.BaseCommand.check` is passed a
  77. :py:class:`~earwigbot.irc.Data` [1]_ object, and should return ``True`` if
  78. you want to respond to this message, or ``False`` otherwise. The default
  79. behavior is to return ``True`` only if
  80. :py:attr:`data.is_command` is ``True`` and :py:attr:`data.command` ==
  81. :py:attr:`~earwigbot.commands.BaseCommand.name`, which is suitable for most
  82. cases. A common, straightforward reason for overriding is if a command has
  83. aliases (see chanops_ for an example). Note that by returning ``True``, you
  84. prevent any other commands from responding to this message.
  85. - Method :py:meth:`~earwigbot.commands.BaseCommand.process` is passed the same
  86. :py:class:`~earwigbot.irc.Data` object as
  87. :py:meth:`~earwigbot.commands.BaseCommand.check`, but only if
  88. :py:meth:`~earwigbot.commands.BaseCommand.check` returned ``True``. This is
  89. where the bulk of your command goes. To respond to IRC messages, there are a
  90. number of methods of :py:class:`~earwigbot.commands.BaseCommand` at your
  91. disposal. See the the test_ command for a simple example, or look in
  92. :py:class:`~earwigbot.commands.BaseCommand`'s
  93. :py:meth:`~earwigbot.commands.BaseCommand.__init__` method for the full list.
  94. The most common ones are :py:attr:`self.say(chan_or_user, msg)`,
  95. :py:attr:`self.reply(data, msg)` (convenience function; sends a reply to the
  96. issuer of the command in the channel it was received),
  97. :py:attr:`self.action(chan_or_user, msg)`,
  98. :py:attr:`self.notice(chan_or_user, msg)`, :py:attr:`self.join(chan)`, and
  99. :py:attr:`self.part(chan)`.
  100. It's important to name the command class :py:class:`Command` within the file,
  101. or else the bot might not recognize it as a command. The name of the file
  102. doesn't really matter and need not match the command's name, but this is
  103. recommended for readability.
  104. The bot has a wide selection of built-in commands and plugins to act as sample
  105. code and/or to give ideas. Start with test_, and then check out chanops_ and
  106. afc_status_ for some more complicated scripts.
  107. Custom bot tasks
  108. ----------------
  109. Custom tasks are subclasses of :py:class:`earwigbot.tasks.BaseTask` that
  110. override :py:class:`~earwigbot.tasks.BaseTask`'s
  111. :py:meth:`~earwigbot.tasks.BaseTask.run` (and optionally
  112. :py:meth:`~earwigbot.tasks.BaseTask.setup`) methods.
  113. :py:class:`~earwigbot.tasks.BaseTask`'s docstrings should explain what each
  114. attribute and method is for and what they should be overridden with, but these
  115. are the basics:
  116. - Class attribute :py:attr:`~earwigbot.tasks.BaseTask.name` is the name of the
  117. task. This must be specified.
  118. - Class attribute :py:attr:`~earwigbot.tasks.BaseTask.number` can be used to
  119. store an optional "task number", possibly for use in edit summaries (to be
  120. generated with :py:meth:`~earwigbot.tasks.BaseTask.make_summary`). For
  121. example, EarwigBot's :py:attr:`config.wiki["summary"]` is
  122. ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the
  123. task class's :py:attr:`~earwigbot.tasks.BaseTask.make_summary(comment)`
  124. method will take and replace ``$1`` with the task number and ``$2`` with the
  125. details of the edit.
  126. Additionally, :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` (which
  127. checks whether the bot has been told to stop on-wiki by checking the content
  128. of a particular page) can check a different page for each task using similar
  129. variables. EarwigBot's :py:attr:`config.wiki["shutoff"]["page"]` is
  130. ``"User:$1/Shutoff/Task $2"``; ``$1`` is substituted with the bot's username,
  131. and ``$2`` is substituted with the task number, so, e.g., task #14 checks the
  132. page ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not*
  133. match :py:attr:`config.wiki["shutoff"]["disabled"]` (``"run"`` by default),
  134. then shutoff is considered to be *enabled* and
  135. :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` will return ``True``,
  136. indicating the task should not run. If you don't intend to use either of
  137. these methods, feel free to leave this attribute blank.
  138. - Method :py:meth:`~earwigbot.tasks.BaseTask.setup` is called *once* with no
  139. arguments immediately after the task is first loaded. Does nothing by
  140. default; treat it like an :py:meth:`__init__` if you want
  141. (:py:meth:`~earwigbot.tasks.BaseTask.__init__` does things by default and a
  142. dedicated setup method is often easier than overriding
  143. :py:meth:`~earwigbot.tasks.BaseTask.__init__` and using :py:obj:`super`).
  144. - Method :py:meth:`~earwigbot.tasks.BaseTask.run` is called with any number of
  145. keyword arguments every time the task is executed (by
  146. :py:attr:`bot.tasks.start(task_name, **kwargs)`, usually). This is where the
  147. bulk of the task's code goes. For interfacing with MediaWiki sites, read up
  148. on the :doc:`Wiki Toolset <toolset>`.
  149. Tasks have access to :py:attr:`config.tasks[task_name]` for config information,
  150. which is a node in :file:`config.yml` like every other attribute of
  151. :py:attr:`bot.config`. This can be used to store, for example, edit summaries,
  152. or templates to append to user talk pages, so that these can be easily changed
  153. without modifying the task itself.
  154. It's important to name the task class :py:class:`Task` within the file, or else
  155. the bot might not recognize it as a task. The name of the file doesn't really
  156. matter and need not match the task's name, but this is recommended for
  157. readability.
  158. See the built-in wikiproject_tagger_ task for a relatively straightforward
  159. task, or the afc_statistics_ plugin for a more complicated one.
  160. .. rubric:: Footnotes
  161. .. [1] :py:class:`~earwigbot.irc.Data` objects are instances of
  162. :py:class:`earwigbot.irc.Data` that contain information about a single
  163. message sent on IRC. Their useful attributes are
  164. :py:attr:`~earwigbot.irc.Data.chan` (channel the message was sent from,
  165. equal to :py:attr:`~earwigbot.irc.Data.nick` if it's a private message),
  166. :py:attr:`~earwigbot.irc.Data.nick` (nickname of the sender),
  167. :py:attr:`~earwigbot.irc.Data.ident` (ident_ of the sender),
  168. :py:attr:`~earwigbot.irc.Data.host` (hostname of the sender),
  169. :py:attr:`~earwigbot.irc.Data.msg` (text of the sent message),
  170. :py:attr:`~earwigbot.irc.Data.is_command` (boolean telling whether or
  171. not this message is a bot command, e.g., whether it is prefixed by
  172. ``!``), :py:attr:`~earwigbot.irc.Data.command` (if the message is a
  173. command, this is the name of the command used), and
  174. :py:attr:`~earwigbot.irc.Data.args` (if the message is a command, this
  175. is a list of the command arguments - for example, if issuing
  176. "``!part ##earwig Goodbye guys``", :py:attr:`~earwigbot.irc.Data.args`
  177. will equal ``["##earwig", "Goodbye", "guys"]``). Note that not all
  178. :py:class:`~earwigbot.irc.Data` objects will have all of these
  179. attributes: :py:class:`~earwigbot.irc.Data` objects generated by private
  180. messages will, but ones generated by joins will only have
  181. :py:attr:`~earwigbot.irc.Data.chan`,
  182. :py:attr:`~earwigbot.irc.Data.nick`,
  183. :py:attr:`~earwigbot.irc.Data.ident`,
  184. and :py:attr:`~earwigbot.irc.Data.host`.
  185. .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py
  186. .. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py
  187. .. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.py
  188. .. _wikiproject_tagger: https://github.com/earwig/earwigbot/blob/develop/earwigbot/tasks/wikiproject_tagger.py
  189. .. _afc_statistics: https://github.com/earwig/earwigbot-plugins/blob/develop/tasks/afc_statistics.py
  190. .. _ident: http://en.wikipedia.org/wiki/Ident