mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Update docs
This commit is contained in:
parent
addff10e63
commit
0b739574ab
7 changed files with 56 additions and 23 deletions
Binary file not shown.
Binary file not shown.
|
@ -64,19 +64,23 @@ To send a message in the chat the command was called in, you can use the :py:met
|
|||
|
||||
And... it's done! The command is ready to be added to a bot!
|
||||
|
||||
Command arguments
|
||||
------------------------------------
|
||||
|
||||
|
||||
Coroutines and slow operations
|
||||
------------------------------------
|
||||
|
||||
You may have noticed that in the previous example I wrote ``await data.reply("🍝")`` instead of just ``data.reply("🍝")``.
|
||||
|
||||
This is because :py:meth:`CommandData.reply` isn't a simple method: it is a coroutine, a special kind of function that
|
||||
can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.
|
||||
can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.
|
||||
|
||||
By adding the ``await`` keyword before the ``data.reply("🍝")``, we tell the bot that it can do other things, like
|
||||
receiving new messages, while the message is being sent.
|
||||
receiving new messages, while the message is being sent.
|
||||
|
||||
You should avoid running slow normal functions inside bot commands, as they will stop the bot from working until they
|
||||
are finished and may cause bugs in other parts of the code! ::
|
||||
are finished and may cause bugs in other parts of the code! ::
|
||||
|
||||
async def run(self, args, data):
|
||||
# Don't do this!
|
||||
|
@ -84,7 +88,7 @@ You should avoid running slow normal functions inside bot commands, as they will
|
|||
...
|
||||
|
||||
If the slow function you want does not cause any side effect, you can wrap it with the :ref:`royalnet.utils.asyncify`
|
||||
function: ::
|
||||
function: ::
|
||||
|
||||
async def run(self, args, data):
|
||||
# If the called function has no side effect, you can do this!
|
||||
|
@ -92,4 +96,10 @@ If the slow function you want does not cause any side effect, you can wrap it wi
|
|||
...
|
||||
|
||||
Avoid using :py:func:`time.sleep` function, as it is considered a slow operation: use instead :py:func:`asyncio.sleep`,
|
||||
a coroutine that does the same exact thing.
|
||||
a coroutine that does the same exact thing.
|
||||
|
||||
Accessing the database
|
||||
------------------------------------
|
||||
|
||||
Comunicating via Royalnet
|
||||
------------------------------------
|
||||
|
|
|
@ -84,7 +84,10 @@
|
|||
<ul class="current">
|
||||
<li class="toctree-l1 current"><a class="current reference internal" href="#">Royalnet Commands</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="#creating-a-new-command">Creating a new Command</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="#command-arguments">Command arguments</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="#coroutines-and-slow-operations">Coroutines and slow operations</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="#accessing-the-database">Accessing the database</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="#comunicating-via-royalnet">Comunicating via Royalnet</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="apireference.html">API Reference</a></li>
|
||||
|
@ -210,33 +213,40 @@ The previously mentioned “spaghetti” command should have a file called <code
|
|||
</div>
|
||||
<p>And… it’s done! The command is ready to be added to a bot!</p>
|
||||
</div>
|
||||
<div class="section" id="command-arguments">
|
||||
<h2>Command arguments<a class="headerlink" href="#command-arguments" title="Permalink to this headline">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="coroutines-and-slow-operations">
|
||||
<h2>Coroutines and slow operations<a class="headerlink" href="#coroutines-and-slow-operations" title="Permalink to this headline">¶</a></h2>
|
||||
<p>You may have noticed that in the previous example I wrote <code class="docutils literal notranslate"><span class="pre">await</span> <span class="pre">data.reply("🍝")</span></code> instead of just <code class="docutils literal notranslate"><span class="pre">data.reply("🍝")</span></code>.</p>
|
||||
<dl>
|
||||
<dt>This is because <a class="reference internal" href="apireference.html#royalnet.commands.CommandData.reply" title="royalnet.commands.CommandData.reply"><code class="xref py py-meth docutils literal notranslate"><span class="pre">CommandData.reply()</span></code></a> isn’t a simple method: it is a coroutine, a special kind of function that</dt><dd><p>can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.</p>
|
||||
</dd>
|
||||
<dt>By adding the <code class="docutils literal notranslate"><span class="pre">await</span></code> keyword before the <code class="docutils literal notranslate"><span class="pre">data.reply("🍝")</span></code>, we tell the bot that it can do other things, like</dt><dd><p>receiving new messages, while the message is being sent.</p>
|
||||
</dd>
|
||||
<dt>You should avoid running slow normal functions inside bot commands, as they will stop the bot from working until they</dt><dd><p>are finished and may cause bugs in other parts of the code!</p>
|
||||
<p>This is because <a class="reference internal" href="apireference.html#royalnet.commands.CommandData.reply" title="royalnet.commands.CommandData.reply"><code class="xref py py-meth docutils literal notranslate"><span class="pre">CommandData.reply()</span></code></a> isn’t a simple method: it is a coroutine, a special kind of function that
|
||||
can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.</p>
|
||||
<p>By adding the <code class="docutils literal notranslate"><span class="pre">await</span></code> keyword before the <code class="docutils literal notranslate"><span class="pre">data.reply("🍝")</span></code>, we tell the bot that it can do other things, like
|
||||
receiving new messages, while the message is being sent.</p>
|
||||
<p>You should avoid running slow normal functions inside bot commands, as they will stop the bot from working until they
|
||||
are finished and may cause bugs in other parts of the code!</p>
|
||||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
|
||||
<span class="c1"># Don't do this!</span>
|
||||
<span class="n">image</span> <span class="o">=</span> <span class="n">download_1_terabyte_of_spaghetti</span><span class="p">(</span><span class="s2">"right_now"</span><span class="p">,</span> <span class="n">from</span><span class="o">=</span><span class="s2">"italy"</span><span class="p">)</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</dd>
|
||||
<dt>If the slow function you want does not cause any side effect, you can wrap it with the <span class="xref std std-ref">royalnet.utils.asyncify</span></dt><dd><p>function:</p>
|
||||
<p>If the slow function you want does not cause any side effect, you can wrap it with the <span class="xref std std-ref">royalnet.utils.asyncify</span>
|
||||
function:</p>
|
||||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
|
||||
<span class="c1"># If the called function has no side effect, you can do this!</span>
|
||||
<span class="n">image</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncify</span><span class="p">(</span><span class="n">download_1_terabyte_of_spaghetti</span><span class="p">,</span> <span class="s2">"right_now"</span><span class="p">,</span> <span class="n">from</span><span class="o">=</span><span class="s2">"italy"</span><span class="p">)</span>
|
||||
<span class="o">...</span>
|
||||
</pre></div>
|
||||
</div>
|
||||
</dd>
|
||||
<dt>Avoid using <a class="reference external" href="https://docs.python.org/3.7/library/time.html#time.sleep" title="(in Python v3.7)"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.sleep()</span></code></a> function, as it is considered a slow operation: use instead <a class="reference external" href="https://docs.python.org/3.7/library/asyncio-task.html#asyncio.sleep" title="(in Python v3.7)"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.sleep()</span></code></a>,</dt><dd><p>a coroutine that does the same exact thing.</p>
|
||||
</dd>
|
||||
</dl>
|
||||
<p>Avoid using <a class="reference external" href="https://docs.python.org/3.7/library/time.html#time.sleep" title="(in Python v3.7)"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.sleep()</span></code></a> function, as it is considered a slow operation: use instead <a class="reference external" href="https://docs.python.org/3.7/library/asyncio-task.html#asyncio.sleep" title="(in Python v3.7)"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.sleep()</span></code></a>,
|
||||
a coroutine that does the same exact thing.</p>
|
||||
</div>
|
||||
<div class="section" id="accessing-the-database">
|
||||
<h2>Accessing the database<a class="headerlink" href="#accessing-the-database" title="Permalink to this headline">¶</a></h2>
|
||||
</div>
|
||||
<div class="section" id="comunicating-via-royalnet">
|
||||
<h2>Comunicating via Royalnet<a class="headerlink" href="#comunicating-via-royalnet" title="Permalink to this headline">¶</a></h2>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -154,7 +154,10 @@
|
|||
<ul>
|
||||
<li class="toctree-l1"><a class="reference internal" href="creatingacommand.html">Royalnet Commands</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="creatingacommand.html#creating-a-new-command">Creating a new Command</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="creatingacommand.html#command-arguments">Command arguments</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="creatingacommand.html#coroutines-and-slow-operations">Coroutines and slow operations</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="creatingacommand.html#accessing-the-database">Accessing the database</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="creatingacommand.html#comunicating-via-royalnet">Comunicating via Royalnet</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="apireference.html">API Reference</a><ul>
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -64,19 +64,23 @@ To send a message in the chat the command was called in, you can use the :py:met
|
|||
|
||||
And... it's done! The command is ready to be added to a bot!
|
||||
|
||||
Command arguments
|
||||
------------------------------------
|
||||
|
||||
|
||||
Coroutines and slow operations
|
||||
------------------------------------
|
||||
|
||||
You may have noticed that in the previous example I wrote ``await data.reply("🍝")`` instead of just ``data.reply("🍝")``.
|
||||
|
||||
This is because :py:meth:`CommandData.reply` isn't a simple method: it is a coroutine, a special kind of function that
|
||||
can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.
|
||||
can be executed separately from the rest of the code, allowing the bot to do other things in the meantime.
|
||||
|
||||
By adding the ``await`` keyword before the ``data.reply("🍝")``, we tell the bot that it can do other things, like
|
||||
receiving new messages, while the message is being sent.
|
||||
receiving new messages, while the message is being sent.
|
||||
|
||||
You should avoid running slow normal functions inside bot commands, as they will stop the bot from working until they
|
||||
are finished and may cause bugs in other parts of the code! ::
|
||||
are finished and may cause bugs in other parts of the code! ::
|
||||
|
||||
async def run(self, args, data):
|
||||
# Don't do this!
|
||||
|
@ -84,7 +88,7 @@ You should avoid running slow normal functions inside bot commands, as they will
|
|||
...
|
||||
|
||||
If the slow function you want does not cause any side effect, you can wrap it with the :ref:`royalnet.utils.asyncify`
|
||||
function: ::
|
||||
function: ::
|
||||
|
||||
async def run(self, args, data):
|
||||
# If the called function has no side effect, you can do this!
|
||||
|
@ -92,4 +96,10 @@ If the slow function you want does not cause any side effect, you can wrap it wi
|
|||
...
|
||||
|
||||
Avoid using :py:func:`time.sleep` function, as it is considered a slow operation: use instead :py:func:`asyncio.sleep`,
|
||||
a coroutine that does the same exact thing.
|
||||
a coroutine that does the same exact thing.
|
||||
|
||||
Accessing the database
|
||||
------------------------------------
|
||||
|
||||
Comunicating via Royalnet
|
||||
------------------------------------
|
||||
|
|
Loading…
Reference in a new issue