DEV Community

Build A Modern Discord Bot from Scratch. Learn the basics

Elijah Trillionz on October 28, 2021

Discord bots help you interact with members of a server as well as moderate the server. A discord bot can send messages on the server, message a us...
Collapse
 
ramkevo1 profile image
Ramkevo • Edited

I'm working on this piece of code for an upcoming test. This is also a useful source of knowledge for me. Fall guys will have you spinning in colorful chaotic battles. Where many players participate in fun combat and defeat the rest to win

Collapse
 
enwoke505 profile image
enwoke505

Hi, I'm pretty new to coding and I am stuck. When attempting to run the app for the first time, I get.

TypeError: Cannot read properties of undefined (reading 'FLAGS')

Thanks for any help.

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Hello, if you're still having issues with this, here is the updated code for intents:

const { GatewayIntentBits } = require('discord.js');

const {
  Guilds,
  GuildMessages,
  DirectMessages,
  MessageContent
} = GatewayIntentBits;

module.exports = [Guilds, GuildMessages, DirectMessages, MessageContent]
Enter fullscreen mode Exit fullscreen mode

For the MessageContent intent, you'd have to give your bot that permission in the developers dashboard.

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Oh yeah, that's probably because there have been updates to the discord library making some methods or properties in this article outdated.
So try going to the docs and check for any updates.

I'll try and update the article soon

Collapse
 
lioness100 profile image
Lioness100

Very well written! Thanks for this great article. A few notes, though:

Intents are new, but they state the permissions your bot requires.

This isn't true. Intents allow you to subscribe to only the events you want to receive from the discord API to decrease memory usage. Also, why did you include the typing and reaction intents? It didn't seem like those were used in the guide code (and would in fact inflate memory usage unneededly).

if (!msg.content.includes(prefix)) return;

Especially since you're assuming the prefix is at the beginning when you slice it off of the message, you should probably use startsWith, not includes. For example, this would currently pass the "it's a command" check: sad is what I am!

Collapse
 
elijahtrillionz profile image
Elijah Trillionz • Edited

Oh thanks for this. I never really understood the intents yet. Will check more on it and make the changes.

Also the intents I stated were originally to be used, but the article had grown to large already.

And thanks for the correction on the prefix. I checked the message properties, but that of content doesn't seem to have any prop (from the docs). Or is it msg.startsWith?, because i still can't find that.

Update: I just verified the startsWith method on VSCode, but I still can't find it in the docs though. It's like it's hidden or something

Collapse
 
lioness100 profile image
Lioness100 • Edited

message.content is a string. You can use any string method on it. It wouldn't be listed on discord.js docs

developer.mozilla.org/en-US/docs/W...

Thread Thread
 
elijahtrillionz profile image
Elijah Trillionz

oh wow! I didn't know about this method before. Thanks for sharing

Collapse
 
abodactyl profile image
Abby Redwood

Just like to stress that "YouTube fetchers" as I believe the author phrases it, are against YouTube's API terms of service and risk being sent a Cease and Desist letter (as happened in the cases of Groovy and Rythm).

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Ok let me explain what I mean by YouTube fetchers.

You can actually fetch new videos from a channel as the videos are uploaded. And it usually comes as a link to the video on youtube.

It could also be in an analytical sense, as a channel owner i can connect with the youtube api to see my analytics in the bot.

I really don't know about groovy so well, but from what I have read, I mean nothing like that in that statement.

Collapse
 
powerstm profile image
Powers

Thanks for this, got me up and running nicely.

One tweak:

// export startBot as default
module.export = startBot;

to

// export startBot as default
module.exports = startBot;

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Thanks for the correction.

Collapse
 
krishna_bollina profile image
BSK-git11

I get some errors from node modules files showing the use of "??=" in the place of "=",
Is any one facing the same?

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

can you describe the issue on github, try pasting the error and how we can reproduce the error.
This is the repo

Collapse
 
krishna_bollina profile image
BSK-git11

Done

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Not all the time though.
I believe it depends on the purpose of the bot really.
There are some boys that are dedicated to lofi music which is great for coding in my opinion.

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Don't get your comment

Collapse
 
jscoder07 profile image
Jeremiah Adeboye

Great job. Thanks for sharing. Very well written

Collapse
 
elijahtrillionz profile image
Elijah Trillionz

Glad you liked it