<?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: Gilles Heinesch</title>
    <description>The latest articles on DEV Community by Gilles Heinesch (@gillesheinesch).</description>
    <link>https://dev.to/gillesheinesch</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%2F216518%2Fb14c84e9-8aec-4a09-8fa7-454adb9e4026.jpg</url>
      <title>DEV Community: Gilles Heinesch</title>
      <link>https://dev.to/gillesheinesch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gillesheinesch"/>
    <language>en</language>
    <item>
      <title>Add Your Open-Source Discord Bot To This List!</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 11 Apr 2021 17:31:34 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/add-your-open-source-discord-bot-to-this-list-3nbc</link>
      <guid>https://dev.to/gillesheinesch/add-your-open-source-discord-bot-to-this-list-3nbc</guid>
      <description>&lt;p&gt;I find it difficult to find a list where good open-source Discord Bot repositories are listed. Therefore I created a &lt;a href="https://github.com/gillesheinesch/opensource-discordbots"&gt;GitHub Repo&lt;/a&gt; with a list of Discord Bots that we believe have potential and that developers who are looking for open-source projects can help.&lt;/p&gt;

&lt;p&gt;Everyone can add their Discord Bot if it's open-source!&lt;br&gt;
LINK: &lt;a href="https://github.com/gillesheinesch/opensource-discordbots"&gt;https://github.com/gillesheinesch/opensource-discordbots&lt;/a&gt;&lt;/p&gt;

</description>
      <category>contributorswanted</category>
      <category>opensource</category>
      <category>discord</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How To Create Your First Express.js Web Application?</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 11 Apr 2021 15:25:46 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/how-to-create-your-first-express-js-web-application-5lp</link>
      <guid>https://dev.to/gillesheinesch/how-to-create-your-first-express-js-web-application-5lp</guid>
      <description>&lt;p&gt;More and more web applications are using ExpressJS. It’s an easy-to-use and powerful web framework for Node.js. What we’re learning today is how to set up a basic Express.js website.&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisites:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Node.js with NPM installed on your system&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Let’s start!
&lt;/h3&gt;

&lt;p&gt;First of all, create a new project folder. You can do this manually or with the following commands:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you’ve done, you should create a package.json using the command npm init in the console. If everything has worked fine, a new file should have been created called package.json . In this file, all project settings are saved like all dependencies the project needs, what the main file is, … You can read more about what the package.json is &lt;a href="https://blog.ezekielekunola.com/understanding-the-package.json-file"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now it’s time to install express.js. You can be doing this with the following command: npm install express --save With this command you install expressjs in your project folder and save it into your package.json .&lt;/p&gt;

&lt;p&gt;As in nearly every beginner tutorial there is a “Hello World” example that we’ll create now. Create a new file in your root folder (In this example it should be in ./myNewApp) called app.js . In this file we’ll setup express.js.&lt;/p&gt;

&lt;p&gt;If you’ve successfully created this file, you can start entering the following code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the first line we use require() which lets Node.js know that we need express for this file to work. In the second line we create a new web server using the express() function. In the third line we set the port which can be whatever you want.&lt;/p&gt;

&lt;p&gt;You can start your web application using the command node app.js . As you can notice, there is no output in the console. This means you have no response if the web application is running now or not. There’s an easy solution for that. Just add the following lines below your code from before:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;If you restart your express server now, you will see that in the console is written &lt;em&gt;“My first Express.js web application listening at &lt;a href="http://localhost:80%E2%80%9D"&gt;http://localhost:80”&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now your web server is running on port 80 but you don’t have any routes yet. Add the following 3 new lines to your code in your app.js :&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the first line we define that the web application should listen to every &lt;strong&gt;GET&lt;/strong&gt; request which is coming from / . This means that if you access your website locally using localhost:80/ the web application will listen to it and execute the code which is on line 2. On line 2 we define that whenever an user uses the URL localhost:80/ , then the response ( res ) should send() “Hello World!” to the client. The result will be the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ib9oZHSe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/449/1%2AL_jjPDM_frALNYZaY-zndA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ib9oZHSe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/449/1%2AL_jjPDM_frALNYZaY-zndA.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HURRAY! Your first web application with express.js is running! Now you can create as many routes as you want. You have so much you can do with express.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some websites where you can learn a lot about express:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://expressjs.com/"&gt;https://expressjs.com/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://expressjs.com/en/resources/learning.html"&gt;https://expressjs.com/en/resources/learning.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/de/docs/Learn/Server-side/Express_Nodejs"&gt;https://developer.mozilla.org/en/docs/Learn/Server-side/Express_Nodejs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tutorialspoint.com/expressjs/index.htm"&gt;https://www.tutorialspoint.com/expressjs/index.htm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I hope I could help you a bit setting up your first express.js web application. I am using express for many web application I’ve made so far, and not only for private projects but also for business projects. If you have any questions just comment below!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MgAyrZbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AhYfdBkfKgvtMoDcqk_LjWA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MgAyrZbI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AhYfdBkfKgvtMoDcqk_LjWA.png" alt=""&gt;&lt;/a&gt;&lt;a href="https://buttercms.com/static/images/tech_banners/ExpressJS.png"&gt;&lt;/a&gt;&lt;a href="https://buttercms.com/static/images/tech_banners/ExpressJS.png"&gt;https://buttercms.com/static/images/tech_banners/ExpressJS.png&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>express</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>LenoxBot, a Discord.js Open-Source bot</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 11 Apr 2021 14:09:24 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/lenoxbot-a-discord-js-open-source-bot-46ok</link>
      <guid>https://dev.to/gillesheinesch/lenoxbot-a-discord-js-open-source-bot-46ok</guid>
      <description>&lt;p&gt;A few years ago I started one of my first programming projects, namely LenoxBot, a Discord.js bot with a web interface. The bot was active on over 15,000 servers but unfortunately "died" due to too little time to develop and now needs a recode. That's why I thought Dev.to is a good place to ask for help from motivated developers who support open source.&lt;br&gt;
Every pull request is welcome!&lt;/p&gt;

&lt;p&gt;About the bot:&lt;br&gt;
The bot has no specific function but should be an "all-in-one" bot, that is, a bot that has a lot of functions.&lt;/p&gt;

&lt;p&gt;We would appreciate any help!&lt;br&gt;
Many Thanks!&lt;/p&gt;

&lt;p&gt;LINK: &lt;a href="https://github.com/LenoxBot/LenoxBot"&gt;https://github.com/LenoxBot/LenoxBot&lt;/a&gt;&lt;/p&gt;

</description>
      <category>discord</category>
      <category>programming</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>A Wonderful Ban Command For Your Discord.js Bot</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 04 Apr 2021 11:26:19 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/a-wonderful-ban-command-for-your-discord-js-bot-3abd</link>
      <guid>https://dev.to/gillesheinesch/a-wonderful-ban-command-for-your-discord-js-bot-3abd</guid>
      <description>&lt;h2&gt;
  
  
  What is our goal with this new command?
&lt;/h2&gt;

&lt;p&gt;Our goal is to program a &lt;strong&gt;good and well-developed&lt;/strong&gt; ban command. It gives you and your moderators the possibility to ban a Discord server member via your bot with a specific reason. All ban details can be posted in a pre-defined Discord channel (Modlog).&lt;br&gt;
What will the command look like when we are done?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;?ban {@USER/UserID} {reason for the ban}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;?ban @Monkeyyy11#0001 Spam&lt;/li&gt;
&lt;li&gt;?ban 238590234135101440 Spam&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Let’s start with programming
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First of all&lt;/strong&gt;, we start with the basic setup of our new command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this line of code we get all the content &lt;strong&gt;behind&lt;/strong&gt; the prefix with the commandname. In this case, everything behind &lt;code&gt;?ban&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example: If you enter the command in a Discord channel &lt;code&gt;?ban @Monkeyyy11#0001 Spam&lt;/code&gt; , args will be &lt;code&gt;@Monkeyyy11#0001 Spam&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const user = message.mentions.users.first(); // returns the user object if an user mention exists
const banReason = args.slice(1).join(' '); // Reason of the ban (Everything behind the mention)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second line of code we request the first mention of a Discord user from the message object. If there is an user mention, you will receive the user object of this Discord user.&lt;/p&gt;

&lt;p&gt;In the next line we slice the ban reason from our arguments of the command (args in this case).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const user = message.mentions.users.first(); // returns the user object if an user mention exists
const banReason = args.slice(1).join(' '); // Reason of the ban (Everything behind the mention)
// Check if an user mention exists in this message
if (!user) {
try {
// Check if a valid userID has been entered instead of a Discord user mention
if (!message.guild.members.get(args.slice(0, 1).join(' '))) throw new Error('Couldn\' get a Discord user with this userID!');
// If the client (bot) can get a user with this userID, it overwrites the current user variable to the user object that the client fetched
user = message.guild.members.get(args.slice(0, 1).join(' '));
user = user.user;
} catch (error) {
return message.reply('Couldn\' get a Discord user with this userID!');
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a very nice feature that allows your ban command to enter an &lt;strong&gt;userID&lt;/strong&gt; of a Discord server member instead of mentioning him.&lt;/p&gt;

&lt;p&gt;First we check if the message contains an user mention, if not; then check if a valid userID has been entered. If not; the client returns an error in form of a Discord channel message. If yes; the client overwrites the uservariable with the &lt;strong&gt;new user object&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const user = message.mentions.users.first(); // returns the user object if an user mention exists
const banReason = args.slice(1).join(' '); // Reason of the ban (Everything behind the mention)
​
// Check if an user mention exists in this message
if (!user) {
try {
// Check if a valid userID has been entered instead of a Discord user mention
if (!message.guild.members.get(args.slice(0, 1).join(' '))) throw new Error('Couldn\' get a Discord user with this userID!');
// If the client (bot) can get a user with this userID, it overwrites the current user variable to the user object that the client fetched
user = message.guild.members.get(args.slice(0, 1).join(' '));
user = user.user;
} catch (error) {
return message.reply('Couldn\' get a Discord user with this userID!');
}
}
if (user === message.author) return message.channel.send('You can\'t ban yourself'); // Check if the user mention or the entered userID is the message author himsmelf
if (!reason) return message.reply('You forgot to enter a reason for this ban!'); // Check if a reason has been given by the message author
if (!message.guild.member(user).bannable) return message.reply('You can\'t ban this user because you the bot has not sufficient permissions!'); // Check if the user is bannable with the bot's permissions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3 different checks have been added here before we continue with further code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;first if&lt;/strong&gt;, checks if the user variable is the same user object as the message author object.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;next code line&lt;/strong&gt; checks if the message author hasn’t forgotten to enter a reason for the ban of the Discord user.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;last line&lt;/strong&gt; that we’ve added, checks if the bot has even &lt;strong&gt;enough permissions&lt;/strong&gt; to ban this Discord user because otherwise the following code that we will cover as next will &lt;strong&gt;not&lt;/strong&gt; work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const user = message.mentions.users.first(); // returns the user object if an user mention exists
const banReason = args.slice(1).join(' '); // Reason of the ban (Everything behind the mention)
​
// Check if an user mention exists in this message
if (!user) {
try {
// Check if a valid userID has been entered instead of a Discord user mention
if (!message.guild.members.get(args.slice(0, 1).join(' '))) throw new Error('Couldn\' get a Discord user with this userID!');
// If the client (bot) can get a user with this userID, it overwrites the current user variable to the user object that the client fetched
user = message.guild.members.get(args.slice(0, 1).join(' '));
user = user.user;
} catch (error) {
return message.reply('Couldn\' get a Discord user with this userID!');
}
}
if (user === message.author) return message.channel.send('You can\'t ban yourself'); // Check if the user mention or the entered userID is the message author himsmelf
if (!reason) return message.reply('You forgot to enter a reason for this ban!'); // Check if a reason has been given by the message author
if (!message.guild.member(user).bannable) return message.reply('You can\'t ban this user because you the bot has not sufficient permissions!'); // Check if the user is bannable with the bot's permissions
​
await message.guild.ban(user) // Bans the user
​
const Discord = require('discord.js'); // We need Discord for our next RichEmbeds
const banConfirmationEmbed = new Discord.RichEmbed()
.setColor('RED')
.setDescription(`✅ ${user.tag} has been successfully banned!`);
message.channel.send({
embed: banConfirmationEmbed
}); // Sends a confirmation embed that the user has been successfully banned
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the new code in &lt;strong&gt;line 24&lt;/strong&gt;, we ban the Discord user from the &lt;strong&gt;current&lt;/strong&gt; Discord server where we enter the bot command.&lt;/p&gt;

&lt;p&gt;After this, we send a confirmation RichEmbed in the &lt;strong&gt;current&lt;/strong&gt; channel where we entered our command to confirm that the user has been &lt;strong&gt;successfully&lt;/strong&gt; banned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
​
const user = message.mentions.users.first(); // returns the user object if an user mention exists
const banReason = args.slice(1).join(' '); // Reason of the ban (Everything behind the mention)
​
// Check if an user mention exists in this message
if (!user) {
try {
// Check if a valid userID has been entered instead of a Discord user mention
if (!message.guild.members.get(args.slice(0, 1).join(' '))) throw new Error('Couldn\' get a Discord user with this userID!');
// If the client (bot) can get a user with this userID, it overwrites the current user variable to the user object that the client fetched
user = message.guild.members.get(args.slice(0, 1).join(' '));
user = user.user;
} catch (error) {
return message.reply('Couldn\' get a Discord user with this userID!');
}
}
if (user === message.author) return message.channel.send('You can\'t ban yourself'); // Check if the user mention or the entered userID is the message author himsmelf
if (!reason) return message.reply('You forgot to enter a reason for this ban!'); // Check if a reason has been given by the message author
if (!message.guild.member(user).bannable) return message.reply('You can\'t ban this user because you the bot has not sufficient permissions!'); // Check if the user is bannable with the bot's permissions
​
await message.guild.ban(user) // Bans the user
​
const Discord = require('discord.js'); // We need Discord for our next RichEmbeds
const banConfirmationEmbed = new Discord.RichEmbed()
.setColor('RED')
.setDescription(`✅ ${user.tag} has been successfully banned!`);
message.channel.send({
embed: banConfirmationEmbed
}); // Sends a confirmation embed that the user has been successfully banned
​
​
const modlogChannelID = ''; // Discord channel ID where you want to have logged the details about the ban
if (modlogChannelID.length !== 0) {
if (!client.channels.get(modlogChannelID )) return undefined; // Check if the modlogChannelID is a real Discord server channel that really exists
​
const banConfirmationEmbedModlog = new Discord.RichEmbed()
.setAuthor(`Banned by **${msg.author.username}#${msg.author.discriminator}**`, msg.author.displayAvatarURL)
.setThumbnail(user.displayAvatarURL)
.setColor('RED')
.setTimestamp()
.setDescription(`**Action**: Ban
**User**: ${user.username}#${user.discriminator} (${user.id})
**Reason**: ${reason}`);
client.channels.get(modlogChannelID).send({
embed: banConfirmationEmbedModlog
}); // Sends the RichEmbed in the modlogchannel
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the new code that we’ve added, we give your bot the possibility to modlog all details of the ban in a Discord channel of &lt;strong&gt;your choice.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope I could help you a little bit with your bot and the setup of your ban command. If you have any questions, just comment below!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ttjku4rukn3ybzwblub.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ttjku4rukn3ybzwblub.jpg" alt="Photo by Temple Cerulean on Unsplash" width="700" height="466"&gt;&lt;/a&gt;&lt;br&gt;
Photo by Temple Cerulean on Unsplash&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>discord</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>An Awesome Command Handler for your Discord.js Bot</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 04 Apr 2021 11:18:43 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/an-awesome-command-handler-for-your-discord-js-bot-a06</link>
      <guid>https://dev.to/gillesheinesch/an-awesome-command-handler-for-your-discord-js-bot-a06</guid>
      <description>&lt;h2&gt;
  
  
  What is our goal with this new command handler?
&lt;/h2&gt;

&lt;p&gt;This command handler offers you a &lt;strong&gt;much clearer&lt;/strong&gt; overview of everything in your bot directory. It allows you to have a &lt;strong&gt;seperated&lt;/strong&gt; JavaScript file for each command you want to have. You can easily define aliases, a fancy description, … for your new commands. It makes it much easier for you to manage everything and to find bugs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trust me, you will have a lot more fun with it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What benefits does the following command handler offer?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can create a file for each command you create&lt;/li&gt;
&lt;li&gt;You can use modules to organize your commands&lt;/li&gt;
&lt;li&gt;Much clearer overview&lt;/li&gt;
&lt;li&gt;Main file has much less lines of code&lt;/li&gt;
&lt;li&gt;Aliases for each command&lt;/li&gt;
&lt;li&gt;Defining as much as you want for each command (required bot permissions, required user permissions, description, …)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let’s start with programming
&lt;/h2&gt;

&lt;p&gt;Let’s start, as always, with the basic setup of our command handler. If I were you, I would put this code in the start file of your bot (Example: &lt;code&gt;bot.js&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Discord = require('discord.js'); // Require discord.js
const client = new Discord.Client(); // Discord.js Client (Bot)​

client.commands = new Discord.Collection(); // Collection for all commands

client.aliases = new Discord.Collection(); // Collection for all aliases of every command
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we definend Discord for our &lt;strong&gt;Discord client&lt;/strong&gt; and &lt;strong&gt;Collections&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the further code lines that’ve added, we define two new Discord collections. The first line saves all commands of the bot. The second line saves all aliases of each command of the bot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Discord = require('discord.js'); // Require discord.js
const client = new Discord.Client(); // Discord.js Client (Bot)​

client.commands = new Discord.Collection(); // Collection for all commands

client.aliases = new Discord.Collection(); // Collection for all aliases of every command​

const modules = ['administration', 'moderation']; // This will be the list of the names of all modules (folder) your bot owns
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we added the new variable &lt;strong&gt;modules&lt;/strong&gt; where you put the names of all modules in it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be aware that also your folder will be named like this!&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Discord = require('discord.js'); // Require discord.js
const client = new Discord.Client(); // Discord.js Client (Bot)​

client.commands = new Discord.Collection(); // Collection for all commands

client.aliases = new Discord.Collection(); // Collection for all aliases of every command​

const modules = ['administration', 'moderation']; // This will be the list of the names of all modules (folder) your bot owns

​const fs = require('fs'); // Require fs to go throw all folder and files​
modules.forEach(c =&amp;gt; {
fs.readdir(`./commands/${c}/`, (err, files) =&amp;gt; {
// Here we go through all folders (modules)
if (err) throw err; // If there is error, throw an error in the console
console.log(`[Commandlogs] Loaded ${files.length} commands of module ${c}`); // When commands of a module are successfully loaded, you can see it in the console

​files.forEach(f =&amp;gt; {
// Now we go through all files of a folder (module)

const props = require(`./commands/${c}/${f}`); // Location of the current command file
client.commands.set(props.help.name, props); // Now we add the commmand in the client.commands Collection which we defined in previous code
props.conf.aliases.forEach(alias =&amp;gt; { // It could be that the command has aliases, so we go through them too

client.aliases.set(alias, props.name); // If we find one, we add it to the client.aliases Collection
});
});
});
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first new code line we require &lt;code&gt;fs&lt;/code&gt;, which we need to go through all our files and folders of our bot.&lt;/p&gt;

&lt;p&gt;As you can notice, we do a &lt;strong&gt;.forEach&lt;/strong&gt; through our modules array which has all names of our bot’s modules. In further code we look into every folder which is located in &lt;strong&gt;./commands&lt;/strong&gt;. If we don’t find any folder, the bot throws an error in the console. If the bot finds a folder, he loads all the files which are located in &lt;strong&gt;this&lt;/strong&gt; folder. Then we do another .forEach through all files which are located in the current folder.&lt;/p&gt;

&lt;p&gt;In this &lt;strong&gt;.forEach&lt;/strong&gt; we first define the location of our command file with the name props. After this we add the command file in our client.commands Discord Collection which we defined in our previous code.&lt;/p&gt;

&lt;p&gt;The last thing that’ve added, is another &lt;strong&gt;.forEach&lt;/strong&gt; which we need to check for any aliases of our command file. If the command has alias(es) we add them to our &lt;strong&gt;client.aliases&lt;/strong&gt; Collection which we also defined in previous code.&lt;/p&gt;

&lt;h2&gt;
  
  
  I do not quite understand where the whole folder should be…
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wghV_bdf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or3u52uuzzvadz59jxem.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wghV_bdf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or3u52uuzzvadz59jxem.png" alt="Your bot directory"&gt;&lt;/a&gt;&lt;br&gt;
Your bot directory&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L2vhM8y3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ho12rq0uk9y188jwvznk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L2vhM8y3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ho12rq0uk9y188jwvznk.png" alt="Your folders named after your bot’s modules"&gt;&lt;/a&gt;&lt;br&gt;
Your folders named after your bot’s modules&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9NPa4qUA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yr6nevumkmkr8wdvp8ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9NPa4qUA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yr6nevumkmkr8wdvp8ow.png" alt="In this case we are in the moderation module (folder) and that’s the place where you have to create the command"&gt;&lt;/a&gt;&lt;br&gt;
In this case we are in the moderation module (folder) and that’s the place where you have to create the command&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope I could help you a little bit with your bot and the setup of your ban command. If you have any questions, just comment below!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--idSwFxOC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gthnh1aw2dlq5qenfb9s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--idSwFxOC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gthnh1aw2dlq5qenfb9s.jpg" alt="Photo by Hannah Joshua on Unsplash"&gt;&lt;/a&gt;&lt;br&gt;
Photo by Hannah Joshua on Unsplash&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>discord</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>An Awesome Clear Command For Your Discord.JS Bot</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Sun, 04 Apr 2021 11:05:36 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/an-awesome-clear-command-for-your-discord-js-bot-3i5h</link>
      <guid>https://dev.to/gillesheinesch/an-awesome-clear-command-for-your-discord-js-bot-3i5h</guid>
      <description>&lt;h2&gt;
  
  
  What is our goal with this new command?
&lt;/h2&gt;

&lt;p&gt;My goal with this post is, to show you how to program a well structured clear command. At the end it is able to clear a specific amount of messages posted in a Discord textchannel.&lt;br&gt;
What will the command look like when we are done?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;?clear {amount of messages that should be deleted}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; ?clear 50&lt;/p&gt;
&lt;h2&gt;
  
  
  Let’s start with programming
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First of all&lt;/strong&gt;, we start with the basic setup of our new command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this line of code we get all the content &lt;strong&gt;behind&lt;/strong&gt; the prefix with the commandname. In this case, &lt;strong&gt;everything behind&lt;/strong&gt; &lt;code&gt;?clear&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example&lt;/em&gt;: If you enter the command in a Discord textchannel &lt;code&gt;?clear 50&lt;/code&gt; , args will be &lt;code&gt;[ 50 ]&lt;/code&gt; (which is an array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now&lt;/strong&gt;, we’ve added a new line which just makes it &lt;strong&gt;simpler&lt;/strong&gt; to read the amount of messages which should be deleted. For this we simply need to &lt;code&gt;join()&lt;/code&gt; the array. If you don’t know the function of &lt;code&gt;join()&lt;/code&gt;, you can read more &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted

if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2 new lines. The first one&lt;/strong&gt; checks if the amount parameter is given. If not, the bot (command) &lt;strong&gt;throws an error&lt;/strong&gt; that this parameter is needed to execute this command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The next one&lt;/strong&gt; checks if the amount parameter is even a number because an amount &lt;strong&gt;can only be an integer&lt;/strong&gt; (a number) and can’t include letters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted

if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error

if (amount &amp;gt; 100) return msg.reply('You can`t delete more than 100 messages at once!'); // Checks if the `amount` integer is bigger than 100
if (amount &amp;lt; 1) return msg.reply('You have to delete at least 1 message!'); // Checks if the `amount` integer is smaller than 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Again 2 new lines&lt;/strong&gt;. These lines should not be complicated at all. &lt;strong&gt;The first one&lt;/strong&gt; checks if the amount integer &lt;strong&gt;is bigger than 100&lt;/strong&gt;, if yes the bot throws an error. &lt;strong&gt;The same for the next line&lt;/strong&gt;, only that it checks if the integer (amount parameter) is &lt;strong&gt;smaller than 1&lt;/strong&gt;. If you don’t know why the maximum can only be 100; &lt;strong&gt;this is due to the Discord API&lt;/strong&gt;, it only allows a bot application to fetch 100 messages at once. More can be found &lt;a href="https://discord.com/developers/docs/resources/channel#get-channel-messages" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted

if (!amount) return msg.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return msg.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error

if (amount &amp;gt; 100) return msg.reply('You can`t delete more than 100 messages at once!'); // Checks if the `amount` integer is bigger than 100
if (amount &amp;lt; 1) return msg.reply('You have to delete at least 1 message!'); // Checks if the `amount` integer is smaller than 1

await msg.channel.messages.fetch({ limit: amount }).then(messages =&amp;gt; { // Fetches the messages
    msg.channel.bulkDelete(messages // Bulk deletes all messages that have been fetched and are not older than 14 days (due to the Discord API)
)});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The first new line fetches all messages.&lt;/strong&gt; The limit is the above given amount parameter (To revise: This is the number behind the &lt;code&gt;?clear&lt;/code&gt; command). Then we continue to the main function of this command; Bulkdelete. The bot now bulkdeletes all messages that have been fetched.&lt;/p&gt;

&lt;h5&gt;
  
  
  Be careful! The messages can not be older than 14 days (again due to the Discord API).
&lt;/h5&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope I could help you a little bit with your bot and the setup of your clear command. If you have any questions, just comment below!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbmjibboh1n4mw3hgy4lx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbmjibboh1n4mw3hgy4lx.jpg" alt="Photo by Paweł Czerwiński on Unsplash"&gt;&lt;/a&gt;&lt;br&gt;
Photo by Paweł Czerwiński on Unsplash&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>discord</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Good Event Handler For Your First Discord.JS Bot</title>
      <dc:creator>Gilles Heinesch</dc:creator>
      <pubDate>Wed, 03 Feb 2021 21:06:40 +0000</pubDate>
      <link>https://dev.to/gillesheinesch/a-good-event-handler-for-your-first-discord-js-bot-48k6</link>
      <guid>https://dev.to/gillesheinesch/a-good-event-handler-for-your-first-discord-js-bot-48k6</guid>
      <description>&lt;h2&gt;
  
  
  Why Do I need This?
&lt;/h2&gt;

&lt;p&gt;Maybe you ask yourself, why do I even need an event handler. That's quite simple to answer. It makes it much simpler for you push an update for an event and fix bugs. You don't have to have all events in the starter javascript file any more. This means that your launcher (starter) file has much less code without all these events. You can create a file for each event you want to have.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs'); // fs is the package we need to read all files which are in folders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Benefits Does The Following Event Handler Offer?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You can create a file for each event you want to have (All events here)&lt;/li&gt;
&lt;li&gt;Much clearer overview of all events and easy to find bugs and add new features to your events&lt;/li&gt;
&lt;li&gt;Main launcher file has much less code&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's Start With Programming
&lt;/h2&gt;

&lt;p&gt;First of all we need a package named fs to read all files which are situated in folders. For this package, we don't need to install anything because this package is included in the Node.JS Bundle. You can read more about this here (This link is also the documentation of fs ).&lt;/p&gt;

&lt;p&gt;In this line we require the package fs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs'); // fs is the package we need to read all files which are in folders

fs.readdir('./events/', (err, files) =&amp;gt; { // We use the method readdir to read what is in the events folder.
    // Code
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we immediately start using our fs package. For our purposes, we use the method readdir() (If you want to read more about this method, you can do this here).&lt;br&gt;
In the third line, we use the readdir() method to read the contents of the ./events directory with a callback of 2 arguments (err (error) and files (files which are in this folder)).&lt;br&gt;
Our current folder structure does look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- main.js
    - events
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first new line (the 4th line) we have added, simply checks if there was an error during checking all contents of the ./events folder. If yes, throw an error in the console with the error message to know what went wrong.&lt;br&gt;
The next new line needs the files argument which we got from the readdir() function we used to check all contents of the ./events directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;files is an array of the names of the files in the directory excluding '.' and '..' .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means, that we can use the forEach() function to go through every single event file with the callback file .&lt;/p&gt;

&lt;p&gt;In this new line (6th line), we simply require the event file from the events folder. This returns us all about informations about the single event file.&lt;/p&gt;

&lt;p&gt;In the first line we added, we check if the eventFunction variable is disabled. If yes, return without throwing any error.&lt;br&gt;
Then we have 3 new variables. The first one defines the name of the event by checking if the eventFunction has a property event with the name of the event. If not, the variable returns the name of the event file. Example: For the ready.jsfile, the event variable would be ready .&lt;br&gt;
The next new variable defines the emitter. This is the "executor" or "engine" of the event. In our case, this is the client (the Discord bot).&lt;br&gt;
The last variable which is new, is once . This variable simply returns the once property of the eventFunction . This property determines if this event should run once.&lt;/p&gt;

&lt;p&gt;Next, we use a try catch block. If there is an error during the execution of the code in the try{} block, it immediately throws an error with the error stack ( error.stack )&lt;br&gt;
In the try{} block, the emitter (client) runs the event, using all above defined settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This event handler can really help you to better organize your files of your Discord bot. If you have any questions, you can visit our forum for further support!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>discord</category>
      <category>node</category>
      <category>discordapp</category>
    </item>
  </channel>
</rss>
