A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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