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.

258 lignes
13 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.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.BaseCommand`
  71. that override :py:class:`~earwigbot.commands.BaseCommand`'s
  72. :py:meth:`~earwigbot.commands.BaseCommand.process` (and optionally
  73. :py:meth:`~earwigbot.commands.BaseCommand.check` or
  74. :py:meth:`~earwigbot.commands.BaseCommand.setup`) methods.
  75. :py:class:`~earwigbot.commands.BaseCommand`'s docstrings should explain what
  76. each attribute and method is for and what they should be overridden with, but
  77. these are the basics:
  78. - Class attribute :py:attr:`~earwigbot.commands.BaseCommand.name` is the name
  79. of the command. This must be specified.
  80. - Class attribute :py:attr:`~earwigbot.commands.BaseCommand.hooks` is a list of
  81. the "IRC events" that this command might respond to. It defaults to
  82. ``["msg"]``, but options include ``"msg_private"`` (for private messages
  83. only), ``"msg_public"`` (for channel messages only), and ``"join"`` (for when
  84. a user joins a channel). See the afc_status_ plugin for a command that
  85. responds to other hook types.
  86. - Method :py:meth:`~earwigbot.commands.BaseCommand.setup` is called *once* with
  87. no arguments immediately after the command is first loaded. Does nothing by
  88. default; treat it like an :py:meth:`__init__` if you want
  89. (:py:meth:`~earwigbot.tasks.BaseCommand.__init__` does things by default and
  90. a dedicated setup method is often easier than overriding
  91. :py:meth:`~earwigbot.tasks.BaseCommand.__init__` and using :py:obj:`super`).
  92. - Method :py:meth:`~earwigbot.commands.BaseCommand.check` is passed a
  93. :py:class:`~earwigbot.irc.data.Data` [1]_ object, and should return ``True``
  94. if you want to respond to this message, or ``False`` otherwise. The default
  95. behavior is to return ``True`` only if
  96. :py:attr:`data.is_command` is ``True`` and :py:attr:`data.command` ==
  97. :py:attr:`~earwigbot.commands.BaseCommand.name`, which is suitable for most
  98. cases. A common, straightforward reason for overriding is if a command has
  99. aliases (see chanops_ for an example). Note that by returning ``True``, you
  100. prevent any other commands from responding to this message.
  101. - Method :py:meth:`~earwigbot.commands.BaseCommand.process` is passed the same
  102. :py:class:`~earwigbot.irc.data.Data` object as
  103. :py:meth:`~earwigbot.commands.BaseCommand.check`, but only if
  104. :py:meth:`~earwigbot.commands.BaseCommand.check` returned ``True``. This is
  105. where the bulk of your command goes. To respond to IRC messages, there are a
  106. number of methods of :py:class:`~earwigbot.commands.BaseCommand` at your
  107. disposal. See the the test_ command for a simple example, or look in
  108. :py:class:`~earwigbot.commands.BaseCommand`'s
  109. :py:meth:`~earwigbot.commands.BaseCommand.__init__` method for the full list.
  110. The most common ones are :py:meth:`say(chan_or_user, msg)
  111. <earwigbot.irc.connection.IRCConnection.say>`, :py:meth:`reply(data, msg)
  112. <earwigbot.irc.connection.IRCConnection.reply>` (convenience function; sends
  113. a reply to the issuer of the command in the channel it was received),
  114. :py:meth:`action(chan_or_user, msg)
  115. <earwigbot.irc.connection.IRCConnection.action>`,
  116. :py:meth:`notice(chan_or_user, msg)
  117. <earwigbot.irc.connection.IRCConnection.notice>`, :py:meth:`join(chan)
  118. <earwigbot.irc.connection.IRCConnection.join>`, and
  119. :py:meth:`part(chan) <earwigbot.irc.connection.IRCConnection.part>`.
  120. Commands have access to :py:attr:`config.commands[command_name]` for config
  121. information, which is a node in :file:`config.yml` like every other attribute
  122. of :py:attr:`bot.config`. This can be used to store, for example, API keys or
  123. SQL connection info, so that these can be easily changed without modifying the
  124. command itself.
  125. It's important to name the command class :py:class:`Command` within the file,
  126. or else the bot might not recognize it as a command. The name of the file
  127. doesn't really matter and need not match the command's name, but this is
  128. recommended for readability.
  129. The bot has a wide selection of built-in commands and plugins to act as sample
  130. code and/or to give ideas. Start with test_, and then check out chanops_ and
  131. afc_status_ for some more complicated scripts.
  132. Custom bot tasks
  133. ----------------
  134. Custom tasks are subclasses of :py:class:`earwigbot.tasks.BaseTask` that
  135. override :py:class:`~earwigbot.tasks.BaseTask`'s
  136. :py:meth:`~earwigbot.tasks.BaseTask.run` (and optionally
  137. :py:meth:`~earwigbot.tasks.BaseTask.setup`) methods.
  138. :py:class:`~earwigbot.tasks.BaseTask`'s docstrings should explain what each
  139. attribute and method is for and what they should be overridden with, but these
  140. are the basics:
  141. - Class attribute :py:attr:`~earwigbot.tasks.BaseTask.name` is the name of the
  142. task. This must be specified.
  143. - Class attribute :py:attr:`~earwigbot.tasks.BaseTask.number` can be used to
  144. store an optional "task number", possibly for use in edit summaries (to be
  145. generated with :py:meth:`~earwigbot.tasks.BaseTask.make_summary`). For
  146. example, EarwigBot's :py:attr:`config.wiki["summary"]` is
  147. ``"([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2"``, which the
  148. task class's :py:meth:`make_summary(comment)
  149. <earwigbot.tasks.BaseTask.make_summary>` method will take and replace
  150. ``$1`` with the task number and ``$2`` with the details of the edit.
  151. Additionally, :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` (which
  152. checks whether the bot has been told to stop on-wiki by checking the content
  153. of a particular page) can check a different page for each task using similar
  154. variables. EarwigBot's :py:attr:`config.wiki["shutoff"]["page"]` is
  155. ``"User:$1/Shutoff/Task $2"``; ``$1`` is substituted with the bot's username,
  156. and ``$2`` is substituted with the task number, so, e.g., task #14 checks the
  157. page ``[[User:EarwigBot/Shutoff/Task 14]].`` If the page's content does *not*
  158. match :py:attr:`config.wiki["shutoff"]["disabled"]` (``"run"`` by default),
  159. then shutoff is considered to be *enabled* and
  160. :py:meth:`~earwigbot.tasks.BaseTask.shutoff_enabled` will return ``True``,
  161. indicating the task should not run. If you don't intend to use either of
  162. these methods, feel free to leave this attribute blank.
  163. - Method :py:meth:`~earwigbot.tasks.BaseTask.setup` is called *once* with no
  164. arguments immediately after the task is first loaded. Does nothing by
  165. default; treat it like an :py:meth:`__init__` if you want
  166. (:py:meth:`~earwigbot.tasks.BaseTask.__init__` does things by default and a
  167. dedicated setup method is often easier than overriding
  168. :py:meth:`~earwigbot.tasks.BaseTask.__init__` and using :py:obj:`super`).
  169. - Method :py:meth:`~earwigbot.tasks.BaseTask.run` is called with any number of
  170. keyword arguments every time the task is executed (by
  171. :py:meth:`tasks.start(task_name, **kwargs)
  172. <earwigbot.managers.TaskManager.start>`, usually). This is where the bulk of
  173. the task's code goes. For interfacing with MediaWiki sites, read up on the
  174. :doc:`Wiki Toolset <toolset>`.
  175. Tasks have access to :py:attr:`config.tasks[task_name]` for config information,
  176. which is a node in :file:`config.yml` like every other attribute of
  177. :py:attr:`bot.config`. This can be used to store, for example, edit summaries
  178. or templates to append to user talk pages, so that these can be easily changed
  179. without modifying the task itself.
  180. It's important to name the task class :py:class:`Task` within the file, or else
  181. the bot might not recognize it as a task. The name of the file doesn't really
  182. matter and need not match the task's name, but this is recommended for
  183. readability.
  184. See the built-in wikiproject_tagger_ task for a relatively straightforward
  185. task, or the afc_statistics_ plugin for a more complicated one.
  186. .. rubric:: Footnotes
  187. .. [1] :py:class:`~earwigbot.irc.data.Data` objects are instances of
  188. :py:class:`earwigbot.irc.Data <earwigbot.irc.data.Data>` that contain
  189. information about a single message sent on IRC. Their useful attributes
  190. are :py:attr:`~earwigbot.irc.data.Data.chan` (channel the message was
  191. sent from, equal to :py:attr:`~earwigbot.irc.data.Data.nick` if it's a
  192. private message), :py:attr:`~earwigbot.irc.data.Data.nick` (nickname of
  193. the sender), :py:attr:`~earwigbot.irc.data.Data.ident` (ident_ of the
  194. sender), :py:attr:`~earwigbot.irc.data.Data.host` (hostname of the
  195. sender), :py:attr:`~earwigbot.irc.data.Data.msg` (text of the sent
  196. message), :py:attr:`~earwigbot.irc.data.Data.is_command` (boolean
  197. telling whether or not this message is a bot command, e.g., whether it
  198. is prefixed by ``!``), :py:attr:`~earwigbot.irc.data.Data.command` (if
  199. the message is a command, this is the name of the command used), and
  200. :py:attr:`~earwigbot.irc.data.Data.args` (if the message is a command,
  201. this is a list of the command arguments - for example, if issuing
  202. "``!part ##earwig Goodbye guys``",
  203. :py:attr:`~earwigbot.irc.data.Data.args` will equal
  204. ``["##earwig", "Goodbye", "guys"]``). Note that not all
  205. :py:class:`~earwigbot.irc.data.Data` objects will have all of these
  206. attributes: :py:class:`~earwigbot.irc.data.Data` objects generated by
  207. private messages will, but ones generated by joins will only have
  208. :py:attr:`~earwigbot.irc.data.Data.chan`,
  209. :py:attr:`~earwigbot.irc.data.Data.nick`,
  210. :py:attr:`~earwigbot.irc.data.Data.ident`,
  211. and :py:attr:`~earwigbot.irc.data.Data.host`.
  212. .. _afc_status: https://github.com/earwig/earwigbot-plugins/blob/develop/commands/afc_status.py
  213. .. _chanops: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/chanops.py
  214. .. _test: https://github.com/earwig/earwigbot/blob/develop/earwigbot/commands/test.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
  217. .. _ident: http://en.wikipedia.org/wiki/Ident