this was written when i was 13, terrible tutorial -2024
ok
Many people want to create a discord bot but they are not familiar with the languages that are commonly used to do so such as JavaScript, Python etc… The thing is that there is no reason for discord bots to be constrained in one language as what you are doing when you are programming your own discord bot is just communicating with your bot through Discord’s API. So in this tutorial, I will be showing you how to create your own discord bot with Lua, as most of the people in this forum are familiar with it.
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).
https://vamik64.ml/dump/Luvit.zip
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 ping!
:
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:
client:run("Bot "..token)
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:
https://gyazo.com/afc6a7676e460c6c5721dcd768ef43d3
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 luvit bot
.
https://gyazo.com/737d7c19c8fb2275c9a4f7cec1dbe298
Now our bot is ready! Lets test!
https://gyazo.com/9ec361226929be375746de824875d84e
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!
https://gyazo.com/8b05c2933c36ea53aa2d85b01cf3b725
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 discordia.log
file.
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…)