1059 lines
30 KiB

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Creating and Running a Bot · Disco</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.2">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-hints/plugin-hints.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="building_block_plugins.html" />
<link rel="prev" href="intro.md" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../installation.html">
<a href="../installation.html">
Installation and Setup
</a>
</li>
<li class="chapter " data-level="1.3" data-path="intro.md">
<span>
Bot Tutorial
</a>
<ul class="articles">
<li class="chapter active" data-level="1.3.1" data-path="first_steps.html">
<a href="first_steps.html">
Creating and Running a Bot
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="building_block_plugins.html">
<a href="building_block_plugins.html">
Plugins
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="building_block_commands.html">
<a href="building_block_commands.html">
Commands
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="building_block_listeners.html">
<a href="building_block_listeners.html">
Listeners
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="message_embeds.html">
<a href="message_embeds.html">
Message Embeds
</a>
</li>
<li class="chapter " data-level="1.3.6" data-path="advanced.html">
<a href="advanced.html">
Advanced
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" >
<span>
API Docs
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../api/disco_client.html">
<a href="../api/disco_client.html">
disco.client
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../api/disco_state.html">
<a href="../api/disco_state.html">
disco.state
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../api/disco_cli.html">
<a href="../api/disco_cli.html">
disco.cli
</a>
</li>
<li class="chapter " data-level="1.4.4" >
<span>
Bot
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.4.1" data-path="../api/disco_bot_bot.html">
<a href="../api/disco_bot_bot.html">
disco.bot.bot
</a>
</li>
<li class="chapter " data-level="1.4.4.2" data-path="../api/disco_bot_plugin.html">
<a href="../api/disco_bot_plugin.html">
disco.bot.plugin
</a>
</li>
<li class="chapter " data-level="1.4.4.3" data-path="../api/disco_bot_command.html">
<a href="../api/disco_bot_command.html">
disco.bot.command
</a>
</li>
<li class="chapter " data-level="1.4.4.4" data-path="../api/disco_bot_storage.html">
<a href="../api/disco_bot_storage.html">
disco.bot.storage
</a>
</li>
<li class="chapter " data-level="1.4.4.5" data-path="../api/disco_bot_parser.html">
<a href="../api/disco_bot_parser.html">
disco.bot.parser
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.5" >
<span>
API
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.5.1" data-path="../api/disco_api_client.html">
<a href="../api/disco_api_client.html">
disco.api.client
</a>
</li>
<li class="chapter " data-level="1.4.5.2" data-path="../api/disco_api_http.html">
<a href="../api/disco_api_http.html">
disco.api.http
</a>
</li>
<li class="chapter " data-level="1.4.5.3" data-path="../api/disco_api_ratelimit.html">
<a href="../api/disco_api_ratelimit.html">
disco.api.ratelimit
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.6" >
<span>
Gateway
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.6.1" data-path="../api/disco_gateway_client.html">
<a href="../api/disco_gateway_client.html">
disco.gateway.client
</a>
</li>
<li class="chapter " data-level="1.4.6.2" data-path="../api/disco_gateway_events.html">
<a href="../api/disco_gateway_events.html">
disco.gateway.events
</a>
</li>
<li class="chapter " data-level="1.4.6.3" data-path="../api/disco_gateway_packets.html">
<a href="../api/disco_gateway_packets.html">
disco.gateway.packets
</a>
</li>
<li class="chapter " data-level="1.4.6.4" data-path="../api/disco_gateway_sharder.html">
<a href="../api/disco_gateway_sharder.html">
disco.gateway.sharder
</a>
</li>
<li class="chapter " data-level="1.4.6.5" data-path="../api/disco_gateway_ipc.html">
<a href="../api/disco_gateway_ipc.html">
disco.gateway.ipc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.7" >
<span>
Types
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.7.1" data-path="../api/disco_types_base.html">
<a href="../api/disco_types_base.html">
disco.types.base
</a>
</li>
<li class="chapter " data-level="1.4.7.2" data-path="../api/disco_types_channel.html">
<a href="../api/disco_types_channel.html">
disco.types.channel
</a>
</li>
<li class="chapter " data-level="1.4.7.3" data-path="../api/disco_types_guild.html">
<a href="../api/disco_types_guild.html">
disco.types.guild
</a>
</li>
<li class="chapter " data-level="1.4.7.4" data-path="../api/disco_types_invite.html">
<a href="../api/disco_types_invite.html">
disco.types.invite
</a>
</li>
<li class="chapter " data-level="1.4.7.5" data-path="../api/disco_types_message.html">
<a href="../api/disco_types_message.html">
disco.types.message
</a>
</li>
<li class="chapter " data-level="1.4.7.6" data-path="../api/disco_types_permissions.html">
<a href="../api/disco_types_permissions.html">
disco.types.permissions
</a>
</li>
<li class="chapter " data-level="1.4.7.7" data-path="../api/disco_types_user.html">
<a href="../api/disco_types_user.html">
disco.types.user
</a>
</li>
<li class="chapter " data-level="1.4.7.8" data-path="../api/disco_types_voice.html">
<a href="../api/disco_types_voice.html">
disco.types.voice
</a>
</li>
<li class="chapter " data-level="1.4.7.9" data-path="../api/disco_types_webhook.html">
<a href="../api/disco_types_webhook.html">
disco.types.webhook
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.8" >
<span>
Util
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.8.1" data-path="../api/disco_util.html">
<a href="../api/disco_util.html">
disco.util
</a>
</li>
<li class="chapter " data-level="1.4.8.2" data-path="../api/disco_util_backdoor.html">
<a href="../api/disco_util_backdoor.html">
disco.util.backdoor
</a>
</li>
<li class="chapter " data-level="1.4.8.3" data-path="../api/disco_util_chains.html">
<a href="../api/disco_util_chains.html">
disco.util.chains
</a>
</li>
<li class="chapter " data-level="1.4.8.4" data-path="../api/disco_util_config.html">
<a href="../api/disco_util_config.html">
disco.util.config
</a>
</li>
<li class="chapter " data-level="1.4.8.5" data-path="../api/disco_util_functional.html">
<a href="../api/disco_util_functional.html">
disco.util.functional
</a>
</li>
<li class="chapter " data-level="1.4.8.6" data-path="../api/disco_util_hashmap.html">
<a href="../api/disco_util_hashmap.html">
disco.util.hashmap
</a>
</li>
<li class="chapter " data-level="1.4.8.7" data-path="../api/disco_util_limiter.html">
<a href="../api/disco_util_limiter.html">
disco.util.limiter
</a>
</li>
<li class="chapter " data-level="1.4.8.8" data-path="../api/disco_util_logging.html">
<a href="../api/disco_util_logging.html">
disco.util.logging
</a>
</li>
<li class="chapter " data-level="1.4.8.9" data-path="../api/disco_util_sanitize.html">
<a href="../api/disco_util_sanitize.html">
disco.util.sanitize
</a>
</li>
<li class="chapter " data-level="1.4.8.10" data-path="../api/disco_util_snowflake.html">
<a href="../api/disco_util_snowflake.html">
disco.util.snowflake
</a>
</li>
<li class="chapter " data-level="1.4.8.11" data-path="../api/disco_util_token.html">
<a href="../api/disco_util_token.html">
disco.util.token
</a>
</li>
<li class="chapter " data-level="1.4.8.12" data-path="../api/disco_util_websocket.html">
<a href="../api/disco_util_websocket.html">
disco.util.websocket
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.9" >
<span>
Voice
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.9.1" data-path="../api/disco_voice_client.html">
<a href="../api/disco_voice_client.html">
disco.voice.client
</a>
</li>
<li class="chapter " data-level="1.4.9.2" data-path="../api/disco_voice_opus.html">
<a href="../api/disco_voice_opus.html">
disco.voice.opus
</a>
</li>
<li class="chapter " data-level="1.4.9.3" data-path="../api/disco_voice_packets.html">
<a href="../api/disco_voice_packets.html">
disco.voice.packets
</a>
</li>
<li class="chapter " data-level="1.4.9.4" data-path="../api/disco_voice_playable.html">
<a href="../api/disco_voice_playable.html">
disco.voice.playable
</a>
</li>
<li class="chapter " data-level="1.4.9.5" data-path="../api/disco_voice_player.html">
<a href="../api/disco_voice_player.html">
disco.voice.player
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >Creating and Running a Bot</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="bot-tutorial">Bot Tutorial</h1>
<p>Disco provides a built-in set of tools for building and running Discord bots which can be used to quickly and easily create integrations. Within this tutorial you&apos;ll be shown how to install Disco, write plugins, and run bots. This tutorial assumes you&apos;ve already followed the <a href="../installation.html">Installation Steps</a>.</p>
<h2 id="creating-a-bot">Creating a Bot</h2>
<p>The first step to creating bots is to actually register them on Discord itself. To do this, you&apos;ll need to be logged into your Discord account on the browser and then navigate to <a href="https://discordapp.com/developers/applications/me" target="_blank">My Apps</a>. Here you&apos;ll have the option to create a new application, and once created you can add a bot user (by clicking &quot;Create a Bot User&quot;) to your application. Finally, you&apos;ll want to keep track of the bot user token which can be shown by clicking the &quot;click to reveal&quot; link next to the token field.</p>
<p>Once you have a Discord bot account, you can then setup your workspace. For now we&apos;ll just need a folder (perhaps called <code>disco-tutorial</code>) with a few files in it:</p>
<pre class="language-"><code>disco-tutorial/
config.json
plugins/
__init__.py
tutorial.py
</code></pre><p><div class="alert alert-success hints-alert"><div class="hints-icon"><i class="fa fa-mortar-board"></i></div><div class="hints-container"><p>The __init__.py file is required for Python to find your plugin, but it can remain empty.</p>
</div></div></p>
<p>Now let&apos;s setup the configuration file. To start off with we&apos;ll paste the following template in and modify our token key (<code>MY_BOT_TOKEN_HERE</code>) to be the token we obtained above. The plugins section tells disco what plugins to load, based on a module path (similar to how Python imports work). In this example we&apos;re asking disco to load the plugin contained in the tutorial file within the plugins directory (or &quot;module&quot;). Disco by default loads the first plugin it finds within the module, so you want to make sure each plugin class is contained within its own file.</p>
<pre class="language-"><code class="lang-json"><span class="token punctuation">{</span>
<span class="token property">&quot;token&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MY_BOT_TOKEN_HERE&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;bot&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;plugins&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">&quot;plugins.tutorial&quot;</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Now we&apos;re ready to write our plugin. Plugins are used to isolate the functionality of your bot into components. Plugins can be dynamically loaded, unloaded and reloaded at runtime. Lets start off by writing a plugin with a &quot;ping&quot; command;</p>
<pre class="language-"><code class="lang-python"><span class="token keyword">from</span> disco<span class="token punctuation">.</span>bot <span class="token keyword">import</span> Plugin
<span class="token keyword">class</span> <span class="token class-name">TutorialPlugin</span><span class="token punctuation">(</span>Plugin<span class="token punctuation">)</span><span class="token punctuation">:</span>
@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">&apos;ping&apos;</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">command_ping</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span><span class="token string">&apos;Pong!&apos;</span><span class="token punctuation">)</span>
</code></pre>
<p>Now that we have a plugin setup and our configuration is ready, we can run and test the bot. We can do this by executing the following command from within our project directory:</p>
<pre class="language-"><code class="lang-sh">python -m disco.cli --config config.json
</code></pre>
<p>If all is successful, you can then test your bot by mentioning it with the command, like so:</p>
<pre class="language-"><code>@tutorial#1234 ping
</code></pre><p>At this point, you&apos;ve achieved the creation and setup of a very simple bot. Now lets work on understanding and working with more Disco features.</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="intro.md" class="navigation navigation-prev " aria-label="Previous page: Bot Tutorial">
<i class="fa fa-angle-left"></i>
</a>
<a href="building_block_plugins.html" class="navigation navigation-next " aria-label="Next page: Plugins">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Creating and Running a Bot","level":"1.3.1","depth":2,"next":{"title":"Plugins","level":"1.3.2","depth":2,"path":"bot_tutorial/building_block_plugins.md","ref":"bot_tutorial/building_block_plugins.md","articles":[]},"previous":{"title":"Bot Tutorial","level":"1.3","depth":1,"path":"bot_tutorial/intro.md","ref":"bot_tutorial/intro.md","articles":[{"title":"Creating and Running a Bot","level":"1.3.1","depth":2,"path":"bot_tutorial/first_steps.md","ref":"bot_tutorial/first_steps.md","articles":[]},{"title":"Plugins","level":"1.3.2","depth":2,"path":"bot_tutorial/building_block_plugins.md","ref":"bot_tutorial/building_block_plugins.md","articles":[]},{"title":"Commands","level":"1.3.3","depth":2,"path":"bot_tutorial/building_block_commands.md","ref":"bot_tutorial/building_block_commands.md","articles":[]},{"title":"Listeners","level":"1.3.4","depth":2,"path":"bot_tutorial/building_block_listeners.md","ref":"bot_tutorial/building_block_listeners.md","articles":[]},{"title":"Message Embeds","level":"1.3.5","depth":2,"path":"bot_tutorial/message_embeds.md","ref":"bot_tutorial/message_embeds.md","articles":[]},{"title":"Advanced","level":"1.3.6","depth":2,"path":"bot_tutorial/advanced.md","ref":"bot_tutorial/advanced.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["prism","-highlight","hints"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"anchorjs":{"placement":"left","visible":"always"},"prism":{},"hints":{"danger":"fa fa-exclamation-circle","info":"fa fa-info-circle","tip":"fa fa-mortar-board","working":"fa fa-wrench"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Disco","gitbook":"*"},"file":{"path":"bot_tutorial/first_steps.md","mtime":"2018-02-15T05:51:41.316Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-02-15T05:53:11.657Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>