[Guide][Squirrel][discord04relx]Creating a discord bot

habi

  • Sr. Member
  • Posts: 389
[Guide][Squirrel][discord04relx] Creating a discord bot
«  »Last edited
Hello, in this tutorial we will learn how to set up a discord bot. This bot can be used to send messages to your discord channel as well as receive text messages from the channel.

We will be using Discord Plugin for squirrel created by Luckshaya

Step 1. Download the plugin and put in plugins/discord04rel64.dll
Step 2. (a). scripts/dcdbot.nut
Code: [Select]
//Copyright @ Luckshaya 2020
//modified by adding field targetchannelID, edited functions sendMessage and onMessage
sessions <- {};

class MyDiscord
{
eventFuncs = null;
session = null;
connID = null;
targetchannelID=null;

function constructor()
{
session = SqDiscord.CSession();
connID = session.ConnID;

sessions.rawset(connID, this);

eventFuncs = [
onReady,
onMessage,
onError,
onDisconnect,
onQuit
];

session.InternalCacheEnabled = true;
}

function Connect(token)
{
session.Connect(token);
}

function sendMessage(channelID, message)
{
session.Message(channelID, message);
}
function sendMessage(message)
{
if(targetchannelID)
session.Message(targetchannelID, message);
else
print("Error. Use sendMessage(channelID, message) because targetchannelID was not set");
}

function sendEmbed(channelID, embed)
{
session.MessageEmbed(channelID, "", embed);
}

function onReady() {
print("Discord bot connection established successfully.");
}

function onMessage(message) {
if(!targetchannelID || targetchannelID==message.ChannelID)
{
print("[Discord]"+message.Member.User.Username+": "+message.Content);
}

}

function onError(code, message) {
print(format("%d - %s", code, message));
}

function onDisconnect() {
print("Discord session has disconnected.");
}

function onQuit() {
print("Discord session has quit.")
}
}


function onDiscordUpdate(connID, eventType, ...) {
if(sessions.rawin(connID)) {
local session = sessions.rawget(connID);
vargv.insert(0, session); //env
session.eventFuncs[eventType].acall(vargv);
}
}
(b). scripts/main.nut
Code: [Select]
dofile("scripts/dcdbot.nut");
myDiscord <- MyDiscord();
myDiscord.Connect("------------YOUR TOKEN HERE----------------");
myDiscord.targetchannelID="---YOUR CHNNL ID HERE--"
Output



The bot will automatically print the messages received from discord. To print those messages inside server (to every players), edit the function onMessage

To send message to the channel, use
Code: [Select]
myDiscord.sendMessage("---your message here--");

Part 2 - How to obtain TOKEN and CHNNL ID
1. Obtaining Channel ID
Obtaining channel id is relatively simple. Open discord in your browser and go to your favourite channel.
If the url when your channel is opened is https://discord.com/channels/xxxx/yyyy, then the yyyy is the channel id.

2. Obtaining TOKEN
2. (a) Creating the bot
1. Head to Discord Developer Portal
Click on New Application

2. Enter a name for  Name of the bot and click Create

That's all there to do.
3. Click on the sidebar and select Bot.

Then click on Add Bot

You will see the message
A wild bot has appeared!
Scroll down and copy Application ID

2. (b) Obtaining token
In the same page, scroll down and you will see Token section. Click on Reset Token. Then click on Copy.

This is the ---YOUR TOKEN HERE----- mentioned in the scripts/main.nut. Note that it must be passed as a string.(enclosed in double quotes)
2. (c) Permission
Permission=8590003200
If you want to change:
This will do for sending and receiving messages in the channel. Or if you want to change this number, use the calculator.
"Scroll down and you will see Bot Permissions Calculator. "

Select
2. (d) Application ID
You obtained in step 1.3
2. (e) Adding bot to the server
1. Go toPut the Application ID in the place of APPLICATION_ID_HERE. If you have changed permission in 2.(c), change is required on proper place in the above url.

Select your server and click on Continue.
The bot will be added to your server


h

  • Newbie
  • Posts: 30

R3Volution Since 2006 | VC:MP Scripter | Savage Sea Developer