For convenient server management, both large and local, you will definitely need boots. You can simply use ready-made ones or choose a more complicated option – creating a discord bot with your own hands. This method allows you to make more precise settings and adapt the bot to your requirements.
We’ll go into detail in the guide. how to make a bot in discord and how to add the created bot to the discord.
How to enable developer mode in Discord
If enable developer mode, the discord will allow you to perform additional manipulations, due to which it will become more convenient to set up and work with the bot. Open the settings, then find the section “Settings apps“, at the very bottom, click”Extended“. In the window that opens, turn on the developer mode located at the top.
How to make a server on Discord
To make a server in Discord, scroll down the list of your servers (located on the panel on the left) to the very bottom. Second button from the bottom a plus, is responsible for creating new servers. Then a panel will open in which you can either create your own template or use ready-made ones: we recommend our own for servers that you plan to develop by attracting participants from outside, for a server designed for gatherings with comrades, you can use a ready-made one.
When you decide on a template, you will need to select the name of the server and put (optional) an icon for it. This completes the creation, in the future you will need to configure – the most difficult part.
How to make a bot on Discord
Where to get a Discord bot token
The first step in creating a bot is getting a token. To get started, go to the official discord development site.
Click on the blue button in the upper right corner “New Application“, a window will open with the creation of an application: in the line”Name» enter a name for the future bot, then click «Create».
In the menu on the left, go to the “Bot“, then click on the button on the right”Add bot“. The site will issue a warning that after the creation of the bot, the application will receive “public life”, accept and continue.
Under the name of the bot there is a section “Token“. It must be created by clicking “Reset Token”, then be sure to save and write somewhere. Token shown only once, if you lose it, you will have to recreate it, and this process entails the suspension of the bot’s performance. Click “Copy‘ and write it down somewhere.
Do not share your bot token with third parties.
You can also grant permissions to the bot in the section below. We recommend doing everything for a specific situation, but if you want to give all rights, then just check the box next to “Administrator».
How to install the Python library
Before you make a bot in the Discord server, you need to install the Python library, thanks to which writing will take place.
Go to the official Python website, hover over “Downloads“, then click on “Python 3.11.1”, we recommend installing this version. You can check by writing the command “py —version“. We also highly recommend installing a programming environment (Wing or PyCharm).
The bot will be able to work on Discord.py only after installing the library. You can install the development version by issuing the command:
py -m pip install git+https://github.com/rapptz/discord.py
For the stable version write
py -3 -m pip install -U discord.py
py -3 -m pip install -U discord.py[voice]
Creating a Discord Bot in Python
Having opened the project, you need to enter the “basic” lines, with which almost any bot starts:
import discord
from discord.ext import commands
config = {
‘token’: ‘your-token’,
‘prefix’: ‘//’,
}
bot = commands.Bot(command_prefix=config[‘prefix’])
In this part of the code, the most important libraries are imported, an auxiliary dictionary is created where the token and prefix will be stored, and then the prefix itself is created in the config.
Then you can create events or commands, below we will tell you how to add commands to the bot in Discord. Consider the example of a user exception, you will need the following code:
@bot.command()
async def kick(ctx, user : discord.User(), *arg, reason=’Reason not specified’):
await bot.kick(user)
await ctx.send(‘User {user.name} was kicked for reason ‘{reason}”)
bot.run(config[‘token’])
At the beginning, we set the command processing decorator, after which we set the conditions and messages that the bot will issue after the user kicks. It is also worth considering that some commands will require additional libraries to be added. For example, for the randomizer command:
import random
import discord
from discord.ext import commands
config = {
‘token’: ‘your-token’,
‘prefix’: ‘//’,
}
bot = commands.Bot(command_prefix=config[‘prefix’])
@bot.command()
async def rand(ctx, *arg):
await ctx.reply(random.randint(0, 100))
bot.run(config[‘token’])
In addition to the previously discussed imports, there is randomizer importwhich then, after the line with the command processing decorator, is responsible for issuing a message with a number from 0 to 100.
How to make a bot that responds to messages
In this section, we will look at creating a bot that will respond to a message, but it is completely meaningless. It will simply repeat after the user, it will take more effort to create something more complex. The code looks like this:
import discord
from discord.ext import commands
config = {
‘token’: ‘your-token’,
‘prefix’: ‘//’,
}
bot = commands.Bot(command_prefix=config[‘prefix’])
@bot.event
async def on_message(ctx):
if ctx.author != bot.user:
await ctx.reply(ctx.content)
bot.run(config[‘token’])
At the very beginning, the same import is performed, then the config, prefix is configured. Now, instead of the command decorator, we need to introduce an event handler decorator. You can read more about discord decorators on the official website. Next, a check is made that is responsible for ensuring that the bot does not talk to itself, after which the line for the bot’s response.
How to add a bot to a discord server
Before adding the created bot to the discord, you need to get a link. On the development site where the token was taken from, go to the tab “OAuth2”, then to the subsection “URL Generator“. In the plate “Scopes» check the box next to «bot“. In the second table that appears, set the permissions at your discretion. A link to the bot will appear at the very bottom.
Follow the link, the bot authorization window will open. Specify the server to which you are going to add the bot, then click “Continue“. Discord will ask if you allow the bot to get the following rights: click “Authorize».
If everything went well, a notification will appear in the chat about joining the bot to the server.
How to enable a bot in Discord
At this stage, the bot will be offline. Before you can make a bot online in Discord, you need to put the entire software shell on any hostwhere the bot will function and execute requests.
The easiest option is to host a bot on one’s own. To do this, run the .py file, which contains the code for the bot. As soon as the program starts, the bot will immediately enter the network and will be ready to respond. Remember that closing the app will disable the bot.
There is another option – host the bot 24/7, this is more suitable for users who want to distribute their bot to other servers. Often such services are paid, because their owners need to pay for the work of machines. You will also need to create a repository and the ability to track logs. Of the free hosting you can consider Herokuhowever, it will not be possible to use it from a Russian address.
We hope our guide helped you figure out how to make a Discord bot, how to add commands to it and make it online. If you have any questions – ask commentswe will definitely help.