Making discord bots

Making discord bots DEFAULT

🤖 Creating your own discord bot using JS 🤖

Introduction

Hey everyone, I have been developing my first discord bot from couple of days and I am finally here to share with you how you can create your bot very easily.

What will you build?

In this blog I am going to tell you how can you make a bot which replies 'Pong' when you say . Again this can be constructed into anything you want. Like you say and bot will say 'Hola' you get the idea right.

I have used Discord Js to build this bot. Yes you can build this in python as well using Discord py.

You can invite my bot here

Pre-requisites

  1. Computer (of-course)
  2. Text editor(Using sublime in this tutorial)
  3. Discord account and a discord server where you have admin access (You can create new one to test the bot out)
  4. A bit of node js and express knowledge. If not refer this

Let's start

Before you do anything make sure you have developer mode enable in your discord. We won't be needing it for this tutorial but you will eventually need it so why not enable it now :)

For this head over to in your discord account

Setting up application and creating bot account

Caution! This would give admin access to the bot.

  • Paste this edited link into the browser search-box and you should see something like below.

--:-> You will land here

--:-> Select the server

--:-> Click on and (Authorize would be on next window after clicking on continue)

--:-> If you did everything correctly then you should see something like this

  • Now you can see your bot on right hand side of your discord (Where all the participants are shown)

Let's code it now.

  • Create a folder with your bot name (Or anything where you will store the code potentially).
  • Open that folder with any text editor (I will be doing that in sublime text)
  • Now Also open a cmd (on windows) or terminal (on linux/mac) in that folder and type

Again here you can remove the flag to customize it. But I will keep it for now to make it a bit easy. After running command you might see something like this or similar

you should see something like this or similar

  • Now create 4 files , ,,
  • Now create a folder and add & file in it. Now your file/folder structure should look something like this (only and are in folder)

  • Now add the code to the respective files as heading

    In index.js :-

    Add the following code

In command.js

In pong.js

In config.js

In .gitignore

In .env

  • First go to the developer portal from where you created the bot application.
  • Then go to and then copy the token as shown in the below image

Caution! :- This is a secret key and should not be added to github or internet or told to anyone.

  • Now add it to your env file (replace XXXXX with your secret key don't add any spaces or something just add as it is.

- Now go to cmd/terminal again and run commands

-

  • Okay so now you are done with coding and it's time for checking it out.
  • Run command
  • Now go to your discord where this bot was invited. and send command you should receive pong in response like below.

But what did we do?

I will try to explain it in most simple way possible. We created which will be the entry point point of the application/bot. then we authenticatied it using and we checked if your application was online.

Our bot will monitor each and every message being posted on the server while it is online and it will check if the posted message was a valid command using if it was then we will run the operation present in file.

Next what? If you followed through then you have created somewhat scalable bot structure. Now everytime you have to add the command then you have to just make a file of the command. Import it to and write at what keyword would you like it to get triggered. and that's it.

Okay so now we have set up our project and you can do lot more with discord bots . Look at the references below to increase the commands and power of your bot.

If you would like me to tell you how to host your bot for free then please let me know in the comments below.

References:

Discord Js Doumentation
The coding train yt channel
My discord bot

❤️ Thank you for reading ❤️

🌏 Like | Follow | Share 🌏
My Socials:Twitter | LinkedIn | GitHub | Discord Server

Sours: https://dev.to/kaiwalyakoparkar/creating-your-own-discord-bot-using-js-2ep0

How to Make a Discord Bot

Image for article titled How to Make a Discord Bot

Once you’ve been on Discord long enough, you’ll encounter a Discord bot. Perhaps it welcomed you to a new server, or booted someone who violated the rules. Maybe it spouted off quotes from Zero Wing whenever anyone typed the word “base.” Some powerful variants of Discord bots have kept users in a given chat constantly up-to-date on local covid-19 vaccine availability.

Bots can vary broadly in function and complexity, and while you can go get the code base for one, you might want to make your own. Learning how to create one from scratch is not only going to ensure you can customize it to your own needs, but can help you understand the inner workings of a bot and, importantly, how to troubleshoot it when things go awry.

So today we’re going to be using Python, which is known for being a pretty good first language for anyone looking to learn to code, thanks to its relatively easy-to-read code and built-in, functional formatting (indenting is not only helpful for reading it; it’s necessary!). Here’s what you will need to get started:

  • A proper computer.
  • A Python install.
  • An Integrated Development Environment (IDE)—this is where you’ll be typing out your code.
  • A Discord account and server.
  • Coffee (optional, highly encouraged).

Creating the Bot Application

I’m operating under the assumption that if you’re here trying to figure this out, then you already have an account and are running a Discord server. If not, go sign up and make a server. Once you’re done with that, you’ll want to head to the Developer Portal. On the left, click the Applications tab, then the New Application button on the top right.

Image for article titled How to Make a Discord Bot

Here, you’re not actually naming the bot, per se—this is the application, which contains the bot. That said, there’s no reason the application and the bot can’t share the same name. Once you’ve created the application, click on it, then click on the Bot link in the sidebar—this is where you can use Discord’s Build-A-Bot process to birth the lil automaton.

G/O Media may get a commission

Image for article titled How to Make a Discord Bot

Once you’ve created the bot, you should get a message saying, “A wild bot has appeared!” Congratulations! It’s a bot! I’m proud of you. Name your bot, and below the Username field, you’ll see a token section, with your token hidden behind a link.

A Note on Bot Tokens

It is crucial that you keep this token secret and keep it safe. The token is a unique identifier especially for your bot, and should anyone else come by it, they can take control of your bot. It’s also how you connect the code you write to the bot. It’s such a sensitive piece of information that it’s not even that good an idea that you keep it directly in your code, but we’ll get to that later.

For now, open a plain text editor and paste the token there. Save it to whatever folder you plan to keep any bot-related materials in, preferably with a name like token.txt to alleviate any confusion down the road.

Giving Your Bot Permissions

Next, you’ll want to give your bot permissions. With your bot still open, click OAuth2 in the sidebar, and under the Scopes section on this screen, tick the box labeled bot and then scroll down to Bot Permissions and hook it up with some abilities. The boxes you check are up to you, and the ones I selected in the below screenshot are not in any way a prescription:

Image for article titled How to Make a Discord Bot

While you’re at it, click on bot in the sidebar again and scroll down to Privileged Gateway Intents. If you want your bot to respond in any way to the presence of users or use the member list, you’ll need to enable these:

Image for article titled How to Make a Discord Bot

Assigning the Bot to Your Server

After you’ve given the appropriate permissions, you’ll need to copy the link beneath the Scopes pane, open a new browser tab, and paste it in the address/search bar, then hit Enter. This will bring you to this screen, where you’ll choose the server (or what is sometimes referred to in the official Python Discord bot API as a “guild”) into which to drop your bot. Click Continue and you’ll be brought to a screen to confirm the bot’s permissions for that server:

Image for article titled How to Make a Discord Bot

Side note: Your bot’s permissions are server-specific, so it follows that if you want to view the permissions your bot has been given, you have to do it from that specific server. If you just want to review them, do the following: right click your server in the top left of the Discord window, click Server Settings, then Integrations. Here, you’ll see your bot under the heading Bots and Apps. Click on it and you’ll see all the permissions you’ve granted your bot. If you want to change them, you’ll need to go a different way, clicking Roles instead of Integrations and then clicking on your bot, and go to the Permissions tab on the next screen.

After you’ve confirmed the permissions you gave your bot, click Authorize, and voíla! You’ve now created a bot and assigned it to a server. You should see a message now that says “A wild [bot name] appeared.” Now it’s time to program the bot! If you’ve tried before and failed, this is very likely the point you gave up. Let’s try again—you’ve got this.

Breathing Life into Your Bot

To continue, you’ll need to make sure you have Python installed. There are many guides on doing this—I like this one. Once you’re set up with Python, you’ll need to get the Discord module. This contains all of the Discord-specific commands you’ll need. If you’re working in Windows, open Command Prompt using the steps outlined here and type the following, hitting Enter when done:

If you’re on macOS or Linux, open Terminal and type:

With that step completed, go ahead and open whatever IDE you’ve chosen to use. I like Sublime, personally, but you can use whatever you want. Heck, you can even use a plain text editor if you’re feeling spicy. With your editor open, create a new file and type this on the first line:

This brings the Discord library into your code. Next, you need a way to associate your bot with this code. This means calling the token for the bot - but remember when I said it’s a good idea not to store that token directly in your code, and to keep it in a .txt file instead? This next line is how you get it from that .txt file:

In this line, you’re creating your first variable: a string of characters you can type to reference a specific action. Here, TOKEN is the variable, the = sign tells the computer that it’s about to interpret that variable as something, and the text following that is that something. Here, open() tells the computer it’s going to open a file, and inside the parentheses is a pair of arguments, the first of which identifies the file you want it to open (if you didn’t call your file token.txt then replace token here with whatever you did name it), and the r tells it the mode you want it open in. We only want to read the token, so we use r to open it in Read mode. The .readline() bit tells it which line to read. Since there’s only one to read, you leave the parentheses empty.

Discord Intents

Due to somewhat recent changes in how Discord handles bot event subscriptions, you’ll need to take some extra steps to enable your bot to respond to certain events, like a member joining, for instance. Discord calls these special permissions Intents, with some higher-level ones like user-monitoring called Privileged Intents (the toggles you may have flipped earlier in the process). In addition to enabling them during your bot’s setup, you will also need to enable intents in your code. First, if you just want to run with the default event subscriptions, define Intents as the defaults:

Then, you’ll flip to True either of the additional intents you’d like. Additionally, you may not want all of the defaults, and may only want your bot to respond to a limited number of event types. In this case, instead of the above, you might do the following:

The final step we’ll take for this section is defining your client and ensuring:

Programming Actions

OK, now we’re ready to start telling our bot what to do. First, it’s helpful to add in a print() statement so that you have confirmation that your software has successfully logged in. This piece of code will print, or display, a statement in your runtime environment (terminal or otherwise) confirming that your bot is connected and running:

The reason we use async here is because Python by default is synchronous, meaning it runs a command and waits for it to complete before moving on. You don’t necessarily want this when communicating with a server, because if the server gets stuck or something, then so does your program. If subsequent code isn’t dependent at all on that line of code, why not let your program just keep running its other code? So you’ll use async (short for asynchronous) to enable your server to continue running your other code. When Discord responds, your code will pick up where it left off and execute whatever statements came after the asynchronous handler you used (which in this case is on_ready()).

Next, let’s go over how to get your bot to respond to commands. As a sample, let’s have the bot say hello to you. First, you need to call your message handler:

Then, to prevent your bot from responding to itself, have the code return without action if the bot says anything:

Finally, the command that prompts the bot’s response:

So the whole block, together, will look like this:

The loop prevention code here, of course, isn’t strictly necessary; the bot wouldn’t get stuck responding to itself here anyway. However, as your code grows more complicated, you may find yourself accidentally programming a bot that could potentially trigger itself and end up in a cycle of self-triggering and responding—say if you make a random movie quote bot, and one of your trigger words is actually in a quote from whatever library of quotes you grabbed.

This next bit of code allows your bot to send out a welcome message. This is where the intents we enabled at the top of our code is a necessity, as it requires the bot to monitor the member list, which Discord considers a privileged intent:

Some key things here:

That print statement is there for debugging purposes. While testing the welcome message, I had a dummy account joining and leaving the Discord server, and wasn’t able to trigger a welcome message for a long time. I put in the print statement to verify that async def on_member_join(member): was actually triggering the program to run subsequent code. If it was, the print statement would, in Terminal, say “got a thing.” It wasn’t, so I had to debug. The key line of code ended up being alllll the way back at the top:

I needed this code so that the bot (Client) would gain the permissions I gave it. You do that by putting an argument in the above parentheses that sets the bot’s intents equal to those as defined at the top of our code. I had of course left this entire line of code out, so my bot wasn’t actually using the intents I’d enabled for it.

To get the Channel ID: While you’re in the channel you’d like the ID for, you’ll want to look in your address bar, where the URL should be, followed by two long sets of numbers, separated by a /. The second set is your channel ID, while the first is your server, or guild, ID. Use the Channel ID in the argument above.

Lastly, the await component of that set of code is necessary when you use async. If your bot is going to respond, it needs some sort of callback function so that it knows what to do when the server does finally respond; await serves that function, in this case telling the bot to send the message, on the previously-defined channel, “Welcome to this channel!”

Let’s take a look at the final code, with all the appropriate (and very necessary!) formatting:

An image showing all of the code displayed so far together in one image.

Keeping the Lights On

So, you’re probably going to want your bot always running. There are a few ways to accomplish this: For one, you can just run it from your primary computer at home, but that requires you to keep the machine always on and always connected, and probably never really sleeping either (you can technically let it sleep if your computer is configured for wake on LAN, but this feature can be troublesome and unpredictable).

A better option could be to run the code on a Raspberry Pi or other dedicated low-power server machine. Basically, you would run your *.py program on the server, and assuming all goes well, it will just run for an indeterminate amount of time. This is fine when you’re starting out. As your bot becomes more complex and—if you allow it—starts being used in other Discord servers, you’re probably going to want it running as close to 24/7 as possible. For that, you’ll want to look into third-party hosting services. There are plenty of free and paid options out there, and here is a decent, lengthy guide to help you with that.

And that’s it! With this basic set of commands, you now have a functional bot. Of course, there is so very much more you can automate with a bot, and I would encourage you to poke around Stack Overflow and the discord.py API. Bots can be as powerful as you want, with automations that make your life as administrator far easier, or they can just be fun little quote machines or diversions for a small Discord server shared with friends. Whatever the case, it’s a fun way to not only manage your Discord server, but also learn key lessons about coding.

Sours: https://gizmodo.com/how-to-make-a-discord-bot-1847378375
  1. Prestige mills carpet prices
  2. Lack clutch size
  3. High profile staffing services
  4. Hello kitty goth shirt

This tutorial will show you how to build your own Discord bot completely in the cloud.

You do not need to install anything on your computer, and you do not need to pay anything to host your bot.

We are going to use a number of tools, including the Discord API, Python libraries, and a cloud computing platform called Repl.it.

There is also a video version of this written tutorial. The video is embedded below and the written version is after the video.

How to Create a Discord Bot Account

In order to work with the Python library and the Discord API, we must first create a Discord Bot account.

Here are the step to creating a Discord Bot account.

1. Make sure you’re logged on to the Discord website.

2. Navigate to the application page.

3. Click on the “New Application” button.

4. Give the application a name and click “Create”.

5. Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking "Yes, do it!"

Keep the default settings for Public Bot (checked) and Require OAuth2 Code Grant (unchecked).

Your bot has been created. The next step is to copy the token.

This token is your bot's password so don't share it with anybody. It could allow someone to log in to your bot and do all sorts of bad things.

You can regenerate the token if it accidentally gets shared.

How to Invite Your Bot to Join a Server

Now you have to get your Bot User into a server. To do this, you should create an invite URL for it.

Go to the "OAuth2" tab. Then select "bot" under the "scopes" section.

Now choose the permissions you want for the bot. Our bot is going to mainly use text messages so we don't need a lot of the permissions. You may need more depending on what you want your bot to do. Be careful with the "Administrator" permission.

After selecting the appropriate permissions, click the 'copy' button above the permissions. That will copy a URL which can be used to add the bot to a server.

Paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.

To add the bot, your account needs "Manage Server" permissions.

Now that you've created the bot user, we'll start writing the Python code for the bot.

How to Code a Basic Discord Bot with the discord.py Library

We'll be using the discord.py Python library to write the code for the bot. discord.py is an API wrapper for Discord that makes it easier to create a Discord bot in Python.

How to Create a Repl and Install discord.py

You can develop the bot on your local computer with any code editor. However, in this tutorial, we'll be using Repl.it because it will make it simpler for anyone to follow along. Repl.it is an online IDE that you can use in your web browser.

Start by going to Repl.it. Create a new Repl and choose "Python" as the language.

To use the discord.py library, just write at the top of . Repl.it will automatically install this dependency when you press the "run" button.

If you prefer to code the bot locally, you can use this command on MacOS to install discord.py:

You may have to use instead of .

If you are using Windows, then you should use the following line instead:

How to Set Up Discord Events for Your Bot

discord.py revolves around the concept of events. An event is something you listen to and then respond to. For example, when a message happens, you will receive an event about it that you can respond to.

Let’s make a bot that replies to a specific message. This simple bot code, along with the code explanation, is taken from the discord.py documentation. We will be adding more features to the bot later.

Add this code to main.py. (You can name the file something else if you like, just not discord.py.) I'll explain what all this code does shortly.

When you created your bot user on Discord, you copied a token. Now we are going to create a file to store the token. If you are running your code locally, you don't need the file. Just replace with the token.

files are used for declaring environment variables. On Repl.it, most files you create are visible to anyone but files are only visible to you.  Other people viewing a public repl will not be able to see the contents of the file.

So if you are developing on Repl.it, only include private information like tokens or keys in a file.

Click the "Add file" button and create a file named .

Inside the file add the following line, including your actual token you copied previously:

Now let's go over what each line of code is doing in your Discord bot code.

  1. The first line imports the discord.py library.
  2. The second line imports the os library, but this is only used for getting the variable from the file. If you are not using a file, you do not need this line.
  3. Next, we create an instance of a . This is the connection to Discord.
  4. The decorator is used to register an event. This is an asynchronous library, so things are done with callbacks. A callback is a function that is called when something else happens. In this code, the event is called when the bot is ready to start being used. Then, when the bot receives a message, the event is called.
  5. The event triggers each time a message is received but we don't want it to do anything if the message is from ourselves. So if the is the same as the the code just returns.
  6. Next, we check if the starts with . If so, then the bot replies with to the channel it was used in.
  7. Now that the bot is set up, the final line runs the bot with the login token. It gets the token from out file.

We have the code for the bot so now we just have to run it.

How to Run the Bot

Now click run button on the top to run your bot in repl.it.

If you are writing the bot locally, you can use these commands in the terminal to run the bot:

On Windows:

On other systems:

Now go to your Discord room and type "$hello". Your bot should return "Hello!".

How to Improve the Bot

Now that we have a basic bot working, we'll improve it. It is called "Encourage Bot" for a reason.

This bot will respond with a message of encouragement whenever someone sends a message containing a sad or depressing word.

Anyone will be able to add encouraging messages for the bot to use and the user-submitted messages will be stored in the Repl.it database.

The bot will also return a random inspirational quote from an API when someone types the message "$inspire" into the chat.

We'll start with adding the "$inspire" feature.

How to Add Inspirational Quotes to the Bot

We will get inspirational quotes from an API called zenquotes.io. We need to import a couple more Python modules, add a function, and update our bot code to call the function.

Here is the updated code. After the code, I'll explain the new parts.

We now have to import the module. This module allows our code to make an HTTP request to get data from the API. The API returns JSON, so the module makes it easier to work with the data returned.

The function is pretty straightforward. First, it uses the requests module to request data from the API URL. The API returns a random inspirational quote. This function could easily be rewritten to get quotes from a different API, if the current one stops working.

Next inside the function, we use to convert the response from the API to JSON. Through trial and error I figured out how to get the quote from the JSON into the string format I wanted. The quote is returned from the function as a string.

The final part updated in the code is toward the end. Previously it looked for a message that started with "$hello". Now it looks for "$inspire". Instead of returning "Hello!", it gets the quote with and returns the quote.

At this point you can run your code and try it out.

How to Add Encouraging Messages to the Bot

Now we will implement the feature where the bot responds with encouraging messages when a user posts a message with a sad word.

How to Add Sad Words to the Bot

First we need to create a Python list that contains the sad words that the bot will respond to.

Add the following line after the variable is created:

Feel free to add more words to the list.

How to Add Encouraging Messages to the Bot

Now we'll add a list of encouraging messages that the bot will respond with.

Add the following list after the list you created:

Like before, feel free to add more phrases of your choice to the list. I'm just using three items for now because later we'll add the ability for users to add more encouraging phrases for the bot to use.

How to Respond to Messages

Now we need to update our bot to use the two lists we created. First, import the random module because the bot will choose encouraging messages randomly. Add the following line to the import statements at the top of the code: .

Now we will update the function to check all messages to see if they contain a word from the list. If a sad word is found, the bot will send a random message of encouragement.

Here is the updated code:

This is a good time to test the bot. You know enough now to create your own bot. But next you'll learn how to implement more advanced features and store data using the Repl.it database.

How to Enable User-submitted Messages

The bot is completely functional, but now let's make it possible to update the bot right from Discord. A user should be able to add more encouraging messages for the bot to use when it detects a sad word.

We are going to use Repl.it's built-in database to store user-submitted messages. This database is a key-value store that’s built into every repl.

At the top of the code, under the other import statements, add . This will allow us to use the Repl.it database.

Users will be able to add custom encouraging messages for the bot to use directly from the Discord chat. Before we add new commands for the bot, let's create two helper functions that will add custom messages to the database and delete them.

Add the following code after the function:

The function accepts an encouraging message as an argument.

First it checks if "encouragements" is a key in the database. If so, it gets the list of encouragements already in the database, adds the new one to the list, and stores the updated list back in the database under the "encouragements" key.

If the database does not already contain "encouragements", a new key by that name is created and the new encouraging message is added as the first element in the list.

The function accepts an index as an argument.

It gets the list of encouragements from the database stored under the "encouragements" key. If the number of items in the encouragements list is greater than the index, then the list item at that index is deleted.

Finally, the updated list is stored back in the database under the "encouragements" key.

Here is the updated code for the function. After the code, I'll explain the new sections.

The first new line of code from above is . We are making a copy of because we are going to add the user-submitted messages to that list before choosing a random message for the bot to send.

We check if "encouragements" is already in the database keys (meaning that a user has submitted at least one custom message). If so, we add the user messages to the starter encouragements.

Then, instead of sending a random message from , the bot now sends a random message from .

The next new section of code is used to add a new user-submitted message to the database. If a Discord message starts with "$new", then the text after "$new" will be used as a new encouraging message.

The code splits off the message from the "$new" command and stores the message in a variable. In that line of code, take note of the space in . We want everything after the space.

We call the helper function with the new message, and then the bot sends a message to the discord chat confirming that the message was added.

The third new section (at the end of the code above) checks if a new Discord message starts with "$del". This is the command to delete an item from the "encouragements" list in the database.

First a new variable called is initialized as an empty array. The reason for this is that this section of code will send a message with an empty array if the database does not include an "encouragement" key.

If the "encouragement" key is in the database, the index will be split off from the Discord message starting with "$del". Then, the function is called passing in the index to delete. The updated list of encouragements is loaded into the variable, and then the bot sends a message to Discord with the current list.

Final Bot Features

The bot should work so this is a good time to test it. We will now add a few final features.

We will add the ability to get a list of user-submitted messages right from Discord and we will add the ability to turn off and on whether the bot responds to sad words.

I will give you the full final code of the program, and then I'll discuss the updates below the code.

The first section added to the code is right under the list:

We create a new key in the database called "responding" and set it to "True". We'll use this to determine if the bot should respond to sad words or not. Since the database is saved even after the program stops running, we only create the new key if it doesn't already exist.

The next new part of the code is that the section that responds to sad words is now inside this if statement: . The bot will only respond to sad words if . The ability to update this value comes after this next section.

Next, after the code to make the bot respond to the "$del" command, there is new code to respond to the "$list" command when sent as a Discord message.

This section starts with creating an empty list called . Then, if there are already encouragements in the database, those encouragements replace the empty list that was just created.

Finally, the bot sends the list of encouragements as a Discord message.

The final new section comes next. This code makes the bot respond to the "$responding" command. This command takes an argument of either "true" or "false". Here is a usage example: "$responding true".

The code first pulls off the argument with (like before, note the space in ). Then there is an if/else statement that appropriately sets the "responding" key in the database and sends a notification message back to Discord. If the argument is anything but "true", the code assumes "false".

The code for the bot is complete! You can now run the bot and try it out. But there is one more important step that we will discuss next.

How to Set Up the Bot to Run Continuously

If you run your bot in repl.it and then close the tab it is running in, your bot will stop running.

But there are two ways you can keep your bot running continuously, even after you close your web bowser.

The first way and simplest way is to sign up for paid plan in Repl.it. Their cheapest paid plan is called the Hacker Plan and it includes five always-on repls.

You can get three months free using this link (limited to first 1000 people):  https://repl.it/claim?code=tryalwayson2103

Once you have signed up for that plan, open your Repl and click the name at the top. Then select the "Always On" option.

There is another way to keep your code running even on the free tier but it is a little more complicated. Repl.it will continue running a web server even after the tab is closed. But even a web server will only run for up to an hour without any use.

Here is what the repl.it docs say:

Once deployed, the server will continue to run in the background, even after you close the browser tab. The server will stay awake and active until an hour after its last request, after which it will enter a sleeping stage. Sleeping repls will be woken up as soon as it receives another request; there is no need to re-run the repl. However, if you make changes to your server, you will need to restart the repl in order to see those changes reflected in the live version.

To keep the bot running continuously, we'll use another free service called Uptime Robot at https://uptimerobot.com/.

Uptime Robot can be set up to ping the bot's web server on repl.it every 5 minutes. With constant pings, the bot will never enter the sleeping stage and will just keep running.

So we have to do two more things to get our bot to run continuously:

  1. create a web server in repl.it and
  2. set up Uptime Robot to continuously ping the web server.

How to Create a Web Server in repl.it

Creating a web server is simpler than you may think.

To do it, create a new file in your project called .

Then add the following code:

In this code, we use Flask to start a web server. The server returns "Hello. I am alive." to anyone who visits it. The server will run on a separate thread from our bot. We won't discuss everything here since the rest is not really relevant to our bot.

Now we just need the bot to run this web server.

Add the following line toward the top of  to import the server.

To start the web server when is run, add the following line as the second-to-last line, right before the bot runs.

When you run the bot on repl.it after adding this code, a new web server window will open up. There is a URL shown for the web server. Copy the URL so you can use it in the next section.

How to Set Up Uptime Robot

Now we need to set up Uptime Robot to ping the web server every five minutes. This will cause the bot to run continuously.

Create a free account on https://uptimerobot.com/.

Once you are logged in to your account, click "Add New Monitor".

For the new monitor, select "HTTP(s)" as the Monitor Type and name it whatever you like. Then, paste in the URL of your web server from repl.it. Finally, click "Create Monitor".

We're done! Now the bot will run continuously so people can always interact with it on Repl.it.

Conclusion

You now know how to create a Discord bot with Python, and run it continuously in the cloud.

There are a lot of other things that the discord.py library can do. So if you want to give a Discord bot even more features, your next step is to check out the docs for discord.py.



If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Sours: https://www.freecodecamp.org/news/create-a-discord-bot-with-python/
Code a Discord Bot with JavaScript - Host for Free in the Cloud

Creating a Bot Account¶

In order to work with the library and the Discord API in general, we must first create a Discord Bot account.

Creating a Bot account is a pretty straightforward process.

  1. Make sure you’re logged on to the Discord website.

  2. Navigate to the application page

  3. Click on the “New Application” button.

    The new application button.
  4. Give the application a name and click “Create”.

    The new application form filled in.
  5. Create a Bot User by navigating to the “Bot” tab and clicking “Add Bot”.

    • Click “Yes, do it!” to continue.

    The Add Bot button.
  6. Make sure that Public Bot is ticked if you want others to invite your bot.

    • You should also make sure that Require OAuth2 Code Grant is unchecked unless you are developing a service that needs it. If you’re unsure, then leave it unchecked.

    How the Bot User options should look like for most people.
  7. Copy the token using the “Copy” button.

    • This is not the Client Secret at the General Information page.

    Warning

    It should be worth noting that this token is essentially your bot’s password. You should never share this to someone else. In doing so, someone can log in to your bot and do malicious things, such as leaving servers, ban all members inside a server, or pinging everyone maliciously.

    The possibilities are endless, so do not share this token.

    If you accidentally leaked your token, click the “Regenerate” button as soon as possible. This revokes your old token and re-generates a new one. Now you need to use the new token to login.

And that’s it. You now have a bot account and you can login with that token.

Inviting Your Bot¶

So you’ve made a Bot User but it’s not actually in any server.

If you want to invite your bot you must create an invite URL for it.

  1. Make sure you’re logged on to the Discord website.

  2. Navigate to the application page

  3. Click on your bot’s page.

  4. Go to the “OAuth2” tab.

    How the OAuth2 page should look like.
  5. Tick the “bot” checkbox under “scopes”.

    The scopes checkbox with "bot" ticked.
  6. Tick the permissions required for your bot to function under “Bot Permissions”.

    • Please be aware of the consequences of requiring your bot to have the “Administrator” permission.

    • Bot owners must have 2FA enabled for certain actions and permissions when added in servers that have Server-Wide 2FA enabled. Check the 2FA support page for more information.

    The permission checkboxes with some permissions checked.
  7. Now the resulting URL can be used to add your bot to a server. Copy and paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.

Note

The person adding the bot needs “Manage Server” permissions to do so.

If you want to generate this URL dynamically at run-time inside your bot and using the interface, you can use .

Sours: https://discordpy.readthedocs.io/en/stable/discord.html

Bots making discord

How to make a Discord bot

Over the last five or so years, Discord has consistently shown that it is the instant messaging platform for not only gamers, but anyone looking to message, video chat, or stream with friends online. Among the reasons why: Discord bots. Bots can help you do everything from automate mundane tasks to start playing music across your server, and in this tutorial, we’re going to show you how to make a Discord bot.

Although automation is the main reason to use a Discord bot, you can really program one to do anything (anything that you can cram in some JavaScript code, at least). You don’t need any programming knowledge to get started, either. Our guide will get you started making your own Discord bots, even if you’ve never touched a line of code before.

Step 1: Download Node.js and set up a Discord account

Node.js is a JavaScript runtime that’s free and open source, and you’ll need it to actually make your bot work. Download it at nodejs.org and install it before you get started on anything else.

Obviously, you’ll also need a Discord account, and your own server to use to test your bot. If you haven’t created one yet, go to Discord.com and create one. If you do have one, log in to your account and open up the server in which you want your bot to live.

You’ll also need a text editor program, like Notepad++ on Windows, to code with.

Step 2: Create your bot

how to make a discord bot my apps

Now you’ll need to create an “application” on Discord to make your bot work. This takes a little doing, but it’s not too complex. The goal here is to get an “authorization token” for the bot so that Discord recognizes your code and adds it to the bot on its servers.

First, head to discordapp.com/developers/applications/me. Your account should be logged in, so you’ll go straight to your account’s list of applications. Hit New Application to get started. Give the bot a name, then hit the button marked Save Changes.

Now, on the right-hand menu, click Bot. Once in the new menu, click Add Bot under the Build-a-Bot option. If you only have one application — the one we just made — it should appear automatically. Otherwise, select it.

Step 3: Get your bot’s authorization token

how to make a discord bot bot box

In the box marked App Bot User, look for the words Token: Click to Reveal. Click that link and you’ll reveal a string of text. That’s your bot’s authorization token, which allows you to send it code. Don’t share it with anyone — that token allows whoever has it to create code for the bot, which means whoever has it can control your bot. If you think the token has been compromised, the good news is that you can easily generate a new one with the Generate a New Token button. Mark down your token. You’ll need it in just a second.

Step 4: Send your bot to your server

how to make a discord bot connect to discord

Now scroll up to the box marked App Details and find your Client ID, a long number. Copy the number and add it to this URL, in the place of word CLIENTID.

https://discordapp.com/oauth2/authorize?&client_id=CLIENTID&scope=bot&permissions=8

The final URL should look like this, but with your client ID number in it instead of this fake one:

https://discordapp.com/oauth2/authorize?&client_id=000000000000000001&scope=bot&permissions=8

Copy the URL with your client ID number in it into your browser. That’ll take you to a website where you can tell Discord where to send your bot. You’ll know it worked if you open Discord in an app or your browser and navigate to your server. The channel will say a bot has joined the room, and you’ll see it on the right side menu under the list of online members.

Step 5: Create a ‘Bot’ folder on your computer

how to make a discord bot folder

While you’re doing that, you can also take a moment to create a folder in an easy-to-reach place on your computer where you can store all your bot’s files. Call it something simple, like “DiscordBot” or “MyBot,” so you know exactly what it is.

Step 6: Open your text editor and make your bot’s files

how to make a discord bot package code

You’re going to create three files for your bot from your text editor. In the first, paste this code:

{
“token”: “Your Bot Token”
}

Replace “Your Bot Token” with the token you generated earlier on your bot’s application page. Make sure the token is inside the quotation marks. Then save the file into the Discord bot folder you made on your desktop, using the filename “auth.json.” Remember not to save it as a .txt file — it won’t work if it’s .txt instead of .json.

Make a new file, and put in this code:

{
“name”: “greeter-bot”,
“version”: “1.0.0”,
“description”: “My First Discord Bot”,
“main”: “bot.js”,
“author”: “Your Name”,
“dependencies”: {}
}

Replace the author name with your name if you want; you can also change the description to something else if you want something more in line with what you’re making, which will be handy for remembering what your bot is supposed to do.

Save this file as “package.json” in your Discord bot folder.

Step 7: Define your bot’s code

how to make a discord bot botjs

There’s one more text file to make, and this is the important one that controls your bot’s behavior. You’ll want to be familiar with JavaScript to really have full control of your bot and know what you’re doing, but if you’re new to coding and just want to make something, you can copy and paste this code into the file to make a simple bot that will greet you in your server.

(Thanks to Medium user Renemari Padillo, whose bot tutorial helped us create this one. Check out his tutorial for code troubleshooting and other advice.)

This code sets up a Discord bot that will respond to certain messages: Specifically, anything that starts with a “!” character. In particular, we’re programming the bot to respond to the command “!intro”, so if anyone types that in your server while the bot is in it, the bot will respond with a programmed message. In our code, we defined the message as “Greetings! Welcome to the server!” You can change both the prompt message and the response message by redefining them in the code above. Just make sure to maintain the single quotation marks around the messages.

Save this last text file as “bot.js” in your Discord bot folder.

Step 8: Open your computer’s “Command Prompt” and navigate to your Discord bot folder

how to make a discord bot command prompt

On a Windows PC, you can easily get to the Command Prompt by clicking the Windows icon and typing Command Prompt in the field. Once it’s open, type “cd” followed by the file path to your folder. On our test computer, the command looks like this: “c:UsersPhil’s DesktopDesktopDiscordBot.” That should change the command prompt line to include the file path to your folder.

Alternatively, you can navigate to your folder in Windows and hold Shift while right-clicking on a blank area of the folder, and choosing Open Command Prompt.

Step 9: Use the Command Prompt to install your bot’s dependencies

how to make a discord bot command prompt node bot js

Now it’s time to make use of Node.js. In the Command Prompt, with your Discord bot folder in the file path line, type “npm install discord.io winston –save.” This will automatically install files you need to for your Discord bot into the folder directly.

Also use the following command line prompt to install additional dependencies: npm install https://github.com/woor/discord.io/tarball/gateway_v6

That should provide you with all the files you need.

Step 10: Run the bot

how to make a discord bot test

Now you’re ready to go. To try running your bot, type “node bot.js” in the Command Prompt (make sure you’re still navigated to your Discord bot folder).

To test your bot’s functionality, get back on your Discord server and try typing in “!intro,” or “!” followed by the prompt message you created in your “bot.js” file. If you coded your bot correctly, sending this command will cause your bot to reply to you with your set message.

Congratulations, you are the proud creator of a Discord bot.

Step 11: Figure out if your bot has been made by someone else

The great thing about Discord is the community of shared interest and skill. Users on Discord are always making new tools to improve the service, including bots. Some creators will upload their bots to public databases and allow others to download the bots and use them for their servers. The bots listed in databases can have a variety of functions coded into them, so you’ll likely be able to find what you need. Before making your bot, do a little exploring on Discord to see if someone else has already made just the bot you need. 

You can search Google for databases, as well as specific Discord bots. You can also try looking at Top.gg (formerly Discordbots) or Bots.ondiscord.xyz.

Editors' Recommendations

Sours: https://www.digitaltrends.com/gaming/how-to-make-a-discord-bot/
Create a Discord Bot With Node.js

Are you sure. - Sure. Why should I be disgusted.

You will also be interested:

My wife pulled away from me, and I felt the sheet rustling, as Karina lifts her legs. So she was taking off her panties. Kolya was already leaning on her, and his hairy hand wandered over his wife's body. I realized that this is it, it has begun. He gathered his willpower into a fist.



1586 1587 1588 1589 1590