[OUTDATED TUTORIAL, POORLY WRITTEN, HIGHLY ADVISE AGAINST USING DISCORDIA OR LUVIT]
First of all to create a discord bot, we will need to install Luvit and Discordia. Here is a simple guide on how to do that: Installing Discordia · SinisterRectus/Discordia Wiki · GitHub.
EDIT: There has recently been a problem with Luvit installation, it errors when installing it, so I am also linking a
.ZIP file containing all of the files you need to have (containing discordia).
Once we are done with that, put everything installed in 1 directory. Your directory should look something like this:
So now we are pretty much done with the installation! Simple as that. Time to program the bot!
So to learn how to program the bot by yourself you need to read Discordia’s wiki which covers everything about it and everything you should know about it. For the sake of this tutorial I will be showing you how you should organize your discord bot and how to make a simple command.
First of all you must create a discord bot before programming it (obviously). So go to Discord Developer Portal and make a new application. After you are done with that go to the
Bot tab and get the token of the bot. Once you get it do not share it with anyone. Anyone who has access to your discord bot’s token can do anything to the bot with it! For now save that token somewhere, we will need it later.
Now create a new
.lua file. Call it whatever you want. I will call it
bot.lua. Once you are done with that open your
.lua file with a code editor. I personally use Brackets.
Now lets create a simple command! Lets make the bot respond with
pong every time we say
(prefix)ping. First of all lets require discordia and define the client:
local discordia = require('discordia') local client = discordia.Client()
Once we are done with that lets add a prefix variable and also lets add a variable for our bot’s token:
local discordia = require('discordia') local client = discordia.Client() local prefix = "!" local token = --your own bot's token here
Lets start writing our bot! So here is the code I wrote to make the bot respond with
pong when we say
client:on('messageCreate', function(message) if message.content == prefix..'ping' then message.channel:send('pong') end end)
If you are confused by this then I will explain what we are basically doing. So every time a message is being sent, we check if the message’s content is equal to
prefix..'ping'. If so we respond with
pong!. Simple as that. Now lets run our bot! Before we do that tho we must add this code to the bottom of our script:
Basically this just runs our bot. Now to actually run our bot and make it work we need to type
cmd at the file’s location directory. Like this:
That is the first step, now type there
luvit (Your Lua File Name Here). So my Lua file is called
bot.lua, so I will type
Now our bot is ready! Lets test!
It worked! I suggest you make a
.bat file which automatically runs the bot for you when testing because opening the
cmd program and typing
luvit bot every time is really time consuming. So lets do that. Make a new
.txt file and type:
luvit bot. Save it as
something.bat and make sure you save it as
All Files or else it won’t work. Lets test it out!
Worked! If you run the bot through a
.bat file, if an error occurs the program with shut down. Every error is logged in a the
Now lets take a look at our code and try to make it better:
local discordia = require('discordia') local client = discordia.Client() local prefix = "!" local token = --token here client:on('messageCreate', function(message) if message.content == prefix..'ping' then message.channel:send('pong') end end) client:run("Bot "..token)
So you are probably thinking the code looks good, there is nothing wrong with it, and you are right, but what if we have like 10 commands? Our functions will be spammed with code and if statements. To prevent that we can organize the code by making a dictionary which holds all of the commands. This is not useful for just organizing the code, but lets suppose you are going to make a help command. You would need to display a description for every command, and it would be better if we just looped on our command dictionary. That is just a suggestion, it is OK if you stick to the current method. So we are pretty much done programming our first bot! You can do different harder stuff with the bot, this was just an easy example for the sake of the tutorial, you can make the bot play audio (it is a bit complicated), you can make the bot kick, ban, mute people, you can make the bot do pretty much everything! Here is the wiki once more Home · SinisterRectus/Discordia Wiki · GitHub, you can learn everything about discordia there!
(P.S You will need to host your bot if you want it to always be active and run, you can self host it or you can use a VPS to host your bot such as DigitalOcean, Heroku, Glitch etc…)