<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: LordOfPolls</title>
    <description>The latest articles on DEV Community by LordOfPolls (@lordofpolls).</description>
    <link>https://dev.to/lordofpolls</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F733728%2Fc708b426-5ab1-4b08-a2cc-006bb692bb71.png</url>
      <title>DEV Community: LordOfPolls</title>
      <link>https://dev.to/lordofpolls</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lordofpolls"/>
    <language>en</language>
    <item>
      <title>Creating a Discord bot with cookiecutter</title>
      <dc:creator>LordOfPolls</dc:creator>
      <pubDate>Tue, 19 Jul 2022 12:57:42 +0000</pubDate>
      <link>https://dev.to/lordofpolls/creating-a-discord-bot-with-cookiecutter-74p</link>
      <guid>https://dev.to/lordofpolls/creating-a-discord-bot-with-cookiecutter-74p</guid>
      <description>&lt;h2&gt;
  
  
  Preamble
&lt;/h2&gt;

&lt;p&gt;Let's get a bot running as quick as possible. For this we're going to use &lt;a href="https://github.com/cookiecutter/cookiecutter"&gt;cookiecutter&lt;/a&gt; and &lt;a href="https://github.com/NAFTeam/NAFF"&gt;NAFF&lt;/a&gt;. Cookiecutter will build us an oven-ready project, and NAFF will facilitate using the Discord API. &lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;This tutorial does not aim to teach you how to program a bot, purely how to use cookiecutter to build a NAFF project. Check out the other guides in this series for that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get started
&lt;/h2&gt;

&lt;p&gt;First, let's get cookiecutter installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install &lt;/span&gt;cookiecutter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now let's build our project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;cookiecutter https://github.com/NAFTeam/Bot-Template
&amp;lt; There will now be a couple of terminal prompts to &lt;span class="nb"&gt;help &lt;/span&gt;you &lt;span class="nb"&gt;set &lt;/span&gt;up your personalised discord bot
&lt;span class="nt"&gt;----------------------------------------------&lt;/span&gt;
Arguments:
&lt;span class="s1"&gt;'project_name'&lt;/span&gt; - The name of your project
&lt;span class="s1"&gt;'project_slug'&lt;/span&gt; - A formatted version of your project name, which is used as a filepath and &lt;span class="k"&gt;for &lt;/span&gt;the docker container names
&lt;span class="s1"&gt;'license'&lt;/span&gt; - What license your project should have. Can be safely ignored should you not plan on openly publishing this project on the internet
&lt;span class="s1"&gt;'license_organization'&lt;/span&gt; - The name that should appear on the license
&lt;span class="s1"&gt;'load_debug_commands'&lt;/span&gt; - If some helpful debug commands should be loaded. These will only be accessible to the owner of the bot
&lt;span class="s1"&gt;'discord_token'&lt;/span&gt; - Your discord bot token. This will not be pushed &lt;span class="k"&gt;if &lt;/span&gt;your use this project with git
&lt;span class="nt"&gt;----------------------------------------------&lt;/span&gt;
Press &lt;span class="s1"&gt;'Enter'&lt;/span&gt; to start
&lt;span class="o"&gt;]&lt;/span&gt;:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here, answer the questions, and when you're done, your project will be ready.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt; project_name &lt;span class="o"&gt;[&lt;/span&gt;New Discord Bot]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;test &lt;/span&gt;project
&amp;lt; project_slug &lt;span class="o"&gt;[&lt;/span&gt;test-project]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;test-project
&amp;lt; Select license:
1 - MIT
2 - BSD-3
3 - GNU GPL v3.0
4 - Apache Software License 2.0
Choose from 1, 2, 3, 4 &lt;span class="o"&gt;[&lt;/span&gt;1]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;1
&amp;lt; license_organization &lt;span class="o"&gt;[&lt;/span&gt;Your Name]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;LordOfPolls
&amp;lt; Select load_debug_commands:
1 - &lt;span class="nb"&gt;true
&lt;/span&gt;2 - &lt;span class="nb"&gt;false
&lt;/span&gt;Choose from 1, 2 &lt;span class="o"&gt;[&lt;/span&gt;1]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;1
&amp;lt; discord_token &lt;span class="o"&gt;[&lt;/span&gt;changethis]: 
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;your_token_here]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now have this project structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test-project
├───core
└───extensions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;core&lt;/code&gt; contains the bot itself, logging, and an extension loader. &lt;code&gt;extensions&lt;/code&gt; holds all of your extensions which will be automatically loaded thanks to the extension loader. &lt;/p&gt;

&lt;p&gt;From here, you can run the bot by running this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;python3 main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Have a look around, and enjoy developing your bot! If you want more info, check out &lt;a href="https://naff-docs.readthedocs.io/en/latest/"&gt;NAFFs Docs&lt;/a&gt; or one of the other guides in this series. &lt;/p&gt;

</description>
      <category>python</category>
      <category>cookiecutter</category>
      <category>discord</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Making a NAFF Discord Bot</title>
      <dc:creator>LordOfPolls</dc:creator>
      <pubDate>Tue, 19 Jul 2022 10:14:24 +0000</pubDate>
      <link>https://dev.to/lordofpolls/making-a-naff-discord-bot-5h1b</link>
      <guid>https://dev.to/lordofpolls/making-a-naff-discord-bot-5h1b</guid>
      <description>&lt;h2&gt;
  
  
  Preamble
&lt;/h2&gt;

&lt;p&gt;So you fancy making your own bot for Discord? Got a fun idea for your community? Well let's get you started. &lt;/p&gt;

&lt;p&gt;For this tutorial I'll be using the python library &lt;a href="https://github.com/NAFTeam/NAFF"&gt;NAFF&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm going to skip past creating a Discord bot on the &lt;a href="https://discord.com/developers/applications"&gt;Developer Portal&lt;/a&gt;. If you're unsure check out &lt;a href="https://naff-docs.readthedocs.io/en/latest/Guides/02%20Creating%20Your%20Bot/"&gt;this guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment
&lt;/h2&gt;

&lt;p&gt;As with any python project, I &lt;strong&gt;strongly&lt;/strong&gt; recommend that you make use of Virtual Environments when working on any project. This means that each project will not affect anything else on your system. Don't worry, this isn't setting up a full-fledged virtual machine, just small python environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"[your bots directory]"&lt;/span&gt;
python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv-name

&lt;span class="c"&gt;# Linux&lt;/span&gt;
&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;span class="c"&gt;# Windows&lt;/span&gt;
venv/Scripts/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's that simple, now you're using a virtual environment. When you're donme, just type &lt;code&gt;deactivate&lt;/code&gt;. If you want to learn more about virtual environments, check out &lt;a href="https://docs.python.org/3/tutorial/venv.html"&gt;this page&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Installing python libraries is really easy! Just type this command and you're good to go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install &lt;/span&gt;naff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic Bot
&lt;/h2&gt;

&lt;p&gt;Let's make something really simple first, we just want to connect to Discord, and say we've logged in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;naff&lt;/span&gt;

&lt;span class="n"&gt;bot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Logged in as &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"your_bot_token_here"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now just run your code, and you should see it login!&lt;/p&gt;

&lt;h2&gt;
  
  
  Commands
&lt;/h2&gt;

&lt;p&gt;Its pretty boring to only see a logged in message, it would be nice to actually have commands; so lets do that now. &lt;/p&gt;

&lt;p&gt;For this, we're going to add a &lt;code&gt;ping&lt;/code&gt; &lt;em&gt;slash&lt;/em&gt; command to the code we just wrote.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;naff&lt;/span&gt;

&lt;span class="n"&gt;bot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;on_ready&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Logged in as &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;slash_command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ping"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"A ping command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scopes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;some_guild_id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;naff&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InteractionContext&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Pong"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"your_bot_token_here"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets break down that decorator. &lt;code&gt;@naff.slash_command&lt;/code&gt; tells NAFF the coroutine is a command. From there you have these parameters&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Name&lt;/code&gt; - The first parameter, this is the name of the command, here it is "ping"&lt;br&gt;
&lt;code&gt;Description&lt;/code&gt; - When a user views commands, this is the help text they'll see&lt;br&gt;
&lt;code&gt;Scopes&lt;/code&gt; - These are the guilds this command will be available in, by default they are available everywhere. For testing its recommended to use a testing guild as it will update faster, and keep your mistakes secret 😉&lt;/p&gt;

&lt;p&gt;There are &lt;a href="https://naff-docs.readthedocs.io/en/latest/API%20Reference/models/Naff/application_commands/#naff.models.naff.application_commands.SlashCommand"&gt;some more parameters&lt;/a&gt; you can play with, but those can be left for a future guide&lt;/p&gt;

&lt;p&gt;And there you have it, now you have a NAFF powered discord bot with slash commands. just start the bot and type &lt;code&gt;/ping&lt;/code&gt; in your guild!&lt;/p&gt;

</description>
      <category>discord</category>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
