Vice City: Multiplayer

VC:MP Discussion => Support => Tutorials => Topic started by: habi on Jun 18, 2022, 07:07 PM

Title: [Guide][Squirrel][discord04relx]Creating a discord bot
Post by: habi on Jun 18, 2022, 07:07 PM
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 (https://forum.vc-mp.org/?topic=6560.0) 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
(https://i.imgur.com/E1rB3nU.png)
(https://i.imgur.com/dYTrCzP.png)

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.
(https://i.imgur.com/cIrrL6x.png)
2. Obtaining TOKEN
2. (a) Creating the bot
1. Head to Discord Developer Portal (https://discord.com/developers/applications)
Click on New Application
(https://i.imgur.com/dUSe1W7.png)
2. Enter a name for  Name of the bot and click Create
(https://i.imgur.com/HfJLCvv.png)
That's all there to do.
3. Click on the sidebar and select Bot.
(https://i.imgur.com/yFxDEyv.png)
Then click on Add Bot
(https://i.imgur.com/zUvI48b.png)
You will see the message
A wild bot has appeared!
Scroll down and copy Application ID
(https://i.imgur.com/RPTS6fF.png)
2. (b) Obtaining token
In the same page, scroll down and you will see Token section. Click on Reset Token. Then click on Copy.
(https://i.imgur.com/7tE3yG8.png)
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. "
(https://i.imgur.com/OQgNGBf.png)
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.
(https://i.imgur.com/Emoup6U.png)
Select your server and click on Continue.
The bot will be added to your server

Title: Re: [Guide][Squirrel][discord04relx]Creating a discord bot
Post by: SexAppeal on Jun 18, 2022, 11:12 PM
Useful
Title: Re: [Guide][Squirrel][discord04relx]Creating a discord bot
Post by: Malik GT on Jul 07, 2023, 05:37 PM
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
Title: Re: [Guide][Squirrel][discord04relx]Creating a discord bot
Post by: Ridwan Rz on Aug 06, 2023, 10:04 AM
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.
Title: Re: [Guide][Squirrel][discord04relx]Creating a discord bot
Post by: H.a.S.a.N on Dec 25, 2023, 10:09 AM
Bro just learn JavaScript and get socket-web for make echo bot