You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1115 lines
43 KiB
1115 lines
43 KiB
|
|
<!DOCTYPE HTML>
|
|
<html lang="" >
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<title>Commands · 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_listeners.html" />
|
|
|
|
|
|
<link rel="prev" href="building_block_plugins.html" />
|
|
|
|
|
|
</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 " 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 active" 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=".." >Commands</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="commands">Commands</h1>
|
|
<p>Commands are a big part of the Discord bot usage. A command can be defined as an order you give to a bot. Basic examples of commands are:
|
|
<code>!help</code> or <code>!info</code>, most bots have either of the two.
|
|
In the case of these examples, when you send <code>!help</code> or <code>!info</code> the bot will reply with a help or info message.</p>
|
|
<h2 id="basic-commands">Basic commands</h2>
|
|
<p>Creating commands in Disco is really easy because of the <a href="https://b1naryth1ef.github.io/disco/bot_tutorial/building_block_plugins.html" target="_blank">Plugins</a> that are a core fundamential of Disco. For more info on them, read back in the <a href="https://b1naryth1ef.github.io/disco/bot_tutorial/building_block_plugins.html" target="_blank">Plugins</a> section of this tutorial. Creating a basic command is done as follows:
|
|
First, create a Plugin class:</p>
|
|
<pre class="language-"><code class="lang-py"><span class="token keyword">class</span> <span class="token class-name">myPlugin</span><span class="token punctuation">(</span>Plugin<span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
</code></pre>
|
|
<p>Now, we can add a command to it. The command will be named ping, and it will simply reply with <code>pong!</code></p>
|
|
<pre class="language-"><code class="lang-py">@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'ping'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_ping_command</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">'Pong!'</span><span class="token punctuation">)</span>
|
|
</code></pre>
|
|
<p>And there we go! Our very first command!</p>
|
|
<h2 id="command-arguments">Command arguments</h2>
|
|
<p>Next, lets go on to some more advanced commands. Wye'll create an echo command that will respond with whatever we put in to it.</p>
|
|
<pre class="language-"><code class="lang-py">@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'echo'</span><span class="token punctuation">,</span> <span class="token string">'<content:str...>'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_echo_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> content<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>content<span class="token punctuation">)</span>
|
|
</code></pre>
|
|
<p>What we did here, was add an argument to our command. The argument we created here, content, is required. This means the command won't work if you don't pass in data for the <code>content</code> argument.
|
|
You can also add optional arguments to a command. Instead of surrounding the name and type in angle brackets, you'd surround them in square brackets like this: <code>[content:str...]</code>
|
|
Keep in mind that arguments that are optional might not be there. You'll have to create some checks so that your program doesn't crash on unexpected null values.</p>
|
|
<h2 id="command-groups">Command groups</h2>
|
|
<p>Now that we have 2 basic commands and we know to create basic commands and add some arguments to it. Let's create a more advanced command utilizing what we just learned.
|
|
The command will take 2 numbers (integers) and simply adds them together. It will work like this: <code>!math add 1 4</code> and it would return 5. Instead of passing <code>'math add'</code> as the command name, we'll be using command groups here.
|
|
Using command groups you can easily group commands together and create sub commands. Now, here comes our math command:</p>
|
|
<pre class="language-"><code class="lang-py">@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'add'</span><span class="token punctuation">,</span> <span class="token string">'<a:int> <b:int>'</span><span class="token punctuation">,</span> group<span class="token operator">=</span><span class="token string">'math'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_add_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<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">'{}'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
</code></pre>
|
|
<p>Here, we added multiple arguments to our command. Namely, number a and number b, that we add together and return back. Of course, you can do loads more fun things with the Disco command handler.</p>
|
|
<h2 id="optional-arguments">Optional arguments</h2>
|
|
<p>Lets create a tag system, that can either store a tag if you'd use it like this: <code>!tag name value</code> or retrieve a tag if you'd use it like this: <code>!tag name</code></p>
|
|
<p>We'll need 2 arguments. A name argument that's required, and an optional value argument. Inside the command we'll check if a <code>value</code> is provided. If there is, we'll store the tag. Otherwise, we'll try to retrieve the previously set value for that tag and return it.
|
|
For the sake of this example, we'll asume that the <code>tags</code> dict gets stored somewhere so it doesn't get removed after a restart.</p>
|
|
<pre class="language-"><code class="lang-py">tags <span class="token operator">=</span> <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">'tag'</span><span class="token punctuation">,</span> <span class="token string">'<name:str> [value:str...]'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_tag_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> name<span class="token punctuation">,</span> value<span class="token operator">=</span>None<span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
|
|
<span class="token keyword">if</span> value<span class="token punctuation">:</span>
|
|
tags<span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> value
|
|
event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span><span class="token string">':ok_hand: created tag `{}`'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> name <span class="token keyword">in</span> tags<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span>tags<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span><span class="token string">'Unknown tag: `{}`'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
</code></pre>
|
|
<h2 id="argumentparser">ArgumentParser</h2>
|
|
<p>A different way of adding arguments to a command is by using <code>argparse.ArgumentParser</code>. With <code>argparser</code> it's easier to create more complicated commands with many options or flags.
|
|
Let's put this into practice by recreating our math add command, but using <code>argparser</code>. More info on <code>argparser</code> and the <code>add_argument()</code> method can be found <a href="https://docs.python.org/2/library/argparse.html#the-add-argument-method" target="_blank">here</a></p>
|
|
<pre class="language-"><code class="lang-py">@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'add'</span><span class="token punctuation">,</span> parser<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> group<span class="token operator">=</span><span class="token string">'math'</span><span class="token punctuation">)</span>
|
|
@Plugin<span class="token punctuation">.</span>parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> type<span class="token operator">=</span>int<span class="token punctuation">)</span>
|
|
@Plugin<span class="token punctuation">.</span>parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">,</span> type<span class="token operator">=</span>int<span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_add_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> args<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">'{}'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>args<span class="token punctuation">.</span>a <span class="token operator">+</span> args<span class="token punctuation">.</span>b<span class="token punctuation">)</span>
|
|
</code></pre>
|
|
<p>These are all the commands we created in this tutorial:</p>
|
|
<pre class="language-"><code class="lang-py"><span class="token keyword">class</span> <span class="token class-name">myPlugin</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">'ping'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_ping_command</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">'Pong!'</span><span class="token punctuation">)</span>
|
|
|
|
@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'echo'</span><span class="token punctuation">,</span> <span class="token string">'<content:str...>'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_echo_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> content<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>content<span class="token punctuation">)</span>
|
|
|
|
@Plugin<span class="token punctuation">.</span>command<span class="token punctuation">(</span><span class="token string">'add'</span><span class="token punctuation">,</span> <span class="token string">'<a:int> <b:int>'</span><span class="token punctuation">,</span> group<span class="token operator">=</span><span class="token string">'math'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_add_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<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">'{}'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>a<span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
|
|
tags <span class="token operator">=</span> <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">'tag'</span><span class="token punctuation">,</span> <span class="token string">'<name:str> [value:str...]'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_tag_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> name<span class="token punctuation">,</span> value<span class="token operator">=</span>None<span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
|
|
<span class="token keyword">if</span> value<span class="token punctuation">:</span>
|
|
tags<span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> value
|
|
event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span><span class="token string">':ok_hand: created tag `{}`'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> name <span class="token keyword">in</span> tags<span class="token punctuation">.</span>keys<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span>tags<span class="token punctuation">[</span>name<span class="token punctuation">]</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> event<span class="token punctuation">.</span>msg<span class="token punctuation">.</span>reply<span class="token punctuation">(</span><span class="token string">'Unknown tag: `{}`'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>name<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">'add'</span><span class="token punctuation">,</span> parser<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> group<span class="token operator">=</span><span class="token string">'math'</span><span class="token punctuation">)</span>
|
|
@Plugin<span class="token punctuation">.</span>parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> type<span class="token operator">=</span>int<span class="token punctuation">)</span>
|
|
@Plugin<span class="token punctuation">.</span>parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">,</span> type<span class="token operator">=</span>int<span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_add_command</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">,</span> args<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">'{}'</span><span class="token punctuation">.</span>format<span class="token punctuation">(</span>args<span class="token punctuation">.</span>a <span class="token operator">+</span> args<span class="token punctuation">.</span>b<span class="token punctuation">)</span>
|
|
</code></pre>
|
|
|
|
|
|
</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="building_block_plugins.html" class="navigation navigation-prev " aria-label="Previous page: Plugins">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
<a href="building_block_listeners.html" class="navigation navigation-next " aria-label="Next page: Listeners">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<script>
|
|
var gitbook = gitbook || [];
|
|
gitbook.push(function() {
|
|
gitbook.page.hasChanged({"page":{"title":"Commands","level":"1.3.3","depth":2,"next":{"title":"Listeners","level":"1.3.4","depth":2,"path":"bot_tutorial/building_block_listeners.md","ref":"bot_tutorial/building_block_listeners.md","articles":[]},"previous":{"title":"Plugins","level":"1.3.2","depth":2,"path":"bot_tutorial/building_block_plugins.md","ref":"bot_tutorial/building_block_plugins.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/building_block_commands.md","mtime":"2018-02-15T05:52:56.547Z","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>
|
|
|
|
|