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

Started by habi, Jun 18, 2022, 07:07 PM

Previous topic - Next topic

habi

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
//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
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
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 to
Quotehttps://discord.com/oauth2/authorize?scope=bot&permissions=8590003200&client_id=APPLICATION_ID_HERE
Put 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



Malik GT

sir its not work can you tell me on discord Malik GT #6646 or any social media because i want this on my server pls reply fast

Thank you

Ridwan Rz

Quote from: Malik GT on Jul 07, 2023, 05:37 PMsir its not work can you tell me on discord Malik GT #6646 or any social media because i want this on my server pls reply fast

Thank you

Open a different thread and post the problem. and also that discord plugin kinda outdated, so I don't know if it's gonna work again. I suggest you to check Aroli's ADiscord.

H.a.S.a.N

Bro just learn JavaScript and get socket-web for make echo bot