Vice City: Multiplayer

VC:MP Discussion => Support => Topic started by: Nihongo^ on Jun 18, 2022, 05:51 AM

Title: Adding discord in server
Post by: Nihongo^ on Jun 18, 2022, 05:51 AM
Hi friends hope you guys are fine

So i am scripting a server and now its time to put discord on it, I am completely new in the world of discord features. I only know about the IRC and it was simple now we have to move to discord :D

i did some research and found [MODULE] Discord module for SqMod (https://forum.vc-mp.org/?topic=6558.msg44160#msg44160)

I put the plugins and install OpenSSL, the plugin loaded successfully but it gave error at "Constructor"

I only put discord.nut and plugins

(https://i.postimg.cc/XJMfXD2y/discord.png)
Title: Re: Adding discord in server
Post by: habi on Jun 18, 2022, 12:30 PM
Add
print(session);just before line 32.
This is to know if variable session is null.
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 18, 2022, 01:07 PM
Quote from: habi on Jun 18, 2022, 12:30 PMAdd
print(session);just before line 32.
This is to know if variable session is null.


Thanks for you kind reply. It said 0x0000

(https://i.postimg.cc/QxmxTDsW-/Untitled.png)
Title: Re: Adding discord in server
Post by: habi on Jun 18, 2022, 01:57 PM
QuoteThanks for you kind reply. It said 0x0000
it's ok.
The variable therefore is not null. It is an instance of CSession class.
This instance is supposed to have member InternalCacheEnabled,  as the source of the discord module shows when i checked it.
Could you try the following code just before line 32 ( remove earlier print statement) and post the output. (To copy an area of console in windows,  first select it by mouse. When it is white colour,  press enter)
foreach(key,  val in session)
print(key+ " " + val +"\n");
Title: Re: Adding discord in server
Post by: habi on Jun 18, 2022, 03:20 PM
Ah,  i figured this out.  There are two discord plugins made by Luckshya.
Discord module for SqMod (https://forum.vc-mp.org/?topic=6558.msg44160#msg44160)

Discord module for Squirrel (https://forum.vc-mp.org/?topic=6560.msg44171#msg44171)

Both have their own wiki and example codes.

You probably want the second one,  since the code in the first post is for squirrel.
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 19, 2022, 11:05 AM
I did some research and found this
https://forum.vc-mp.org/?topic=8869.msg52280#msg52280

I am successfully recevnig the message on my console what ever i am writing on discord. But i am confused how do i send messages in server when i type on discord ? and same discord to server

Please guide me on it
thank you
Title: Re: Adding discord in server
Post by: habi on Jun 19, 2022, 12:54 PM
Hi
If you are using dcdbot.nut and main.nut in topic?8869 (https://forum.vc-mp.org/?topic=8869.0),  then
function onPlayerChat( player, message )
{
myDiscord.sendMessage(player.Name + ": "+ message);
}

Can you see messages send in discord in the server console?
If yes,  then this is the homework for you: Receive messages from discord and show it in server. Hint: Use Message function of squirrel and work on dcdbot.nut. Find onMessage function.
function onMessage(message) {
  if(!targetchannelID || targetchannelID==message.ChannelID)
  {
   print("[Discord]"+message.Member.User.Username+": "+message.Content);
  }
 }



[Discord]
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 19, 2022, 01:59 PM
Its worked Thank you so much habi
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 19, 2022, 03:04 PM
I forgot to ask about sending messages in the server from discord.
like !say<text>. with discord rank
Title: Re: Adding discord in server
Post by: habi on Jun 19, 2022, 06:09 PM
I guess you meant Discord Roles which are assigned to server members.

There does not exit any material on this.

Suppose there are 3 roles created in your discord server: owner, admin, moderator
There can be more roles. But you only want these three people to use command "!say".

Modify your dcdbot.nut
dcdbot.nut (part)
function onMessage(message) {
if(!targetchannelID || targetchannelID==message.ChannelID)
{
local array=GetCmdAndText(message.Content);
if(array)
{
local cmd=array[0]; local text=array[1];
if(cmd=="say" && text){
local member = message.Member;
local serverID = message.ServerID;

if(member != null && serverID != null) {
foreach(roleID in member.Roles) {
local role=session.GetRoleName(serverID, roleID);
if(role=="moderator"||role=="admin"||role=="owner"){
print("[Discord]"+member.User.Username+": "+text);
break;
}
}
}

}
}
}

}
Don't copy paste. Read it and understand. Make necessary changes like role names.

Here is the GetCmdAndText function used
function GetCmdAndText(message)
{

local cmd=null;local text=null;
if(message.len()>1 && message[0]=='!')
{
local idx=message.find(" ");
if(idx)
{
cmd=message.slice(1,idx);
if(message.len()>idx+1)
{
text=message.slice(idx+1);
}
}else cmd=message.slice(1);
return [cmd, text];
}
}

Results:
(https://i.imgur.com/q3wfPk5.png)
(https://i.imgur.com/4qWTaNU.png)
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 20, 2022, 04:57 AM
Thank you so much its works for me I modified the code and now I can see the role too

I have 2 more questions how do I send this message to the server? I mean its printing on the console only

and what role for the normal player?

I tried to make this but didn't work

if(role=="moderator"||role=="admin"||role=="Owner")
 {
  print("[Discord]" +role+ "" +member.User.Username+": "+text);
  }
  else
  {
  print("Memeber" +member.User.Username+": "+text);
  break;
 }
Title: Re: Adding discord in server
Post by: habi on Jun 20, 2022, 12:37 PM
By normal player, you have meant those discord users in the server on whom no roles are assigned.

In that case using else part of the if statement
if(role=="moderator"||role=="admin"||role=="Owner"){is not correct,  because normal player has no roles.

In that case member.Roles will be an empty array.

Therefore this loop will not be executed:
foreach(roleID in member.Roles) {
      local role=session.GetRoleName(serverID, roleID);
      if(role=="moderator"||role=="admin"||role=="owner"){
       print("[Discord]"+member.User.Username+": "+text);
       break;
      }
     }
So just after the loop (after the closing brace of loop) add this:
if(member.Roles.len()==0)
{
// here code for user with no roles
}

This code is not tested yet.

....

Quotehow do I send this message to the server? I mean its printing on the console only
QuoteHint: Use Message function of squirrel
Using print statement will print to console and using Message statement will output in game.
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 20, 2022, 01:59 PM
Hi i tried Message function of squirrel but its gives error here .

(https://i.postimg.cc/G3TXxNpn/Untitled.png)
Title: Re: Adding discord in server
Post by: habi on Jun 20, 2022, 02:46 PM
Put comma ( , ) after text
ClientMessageToAll(...+": "+text, 255, 0, 255);
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 20, 2022, 03:26 PM
Sorry still giving the same error after putting the comma
(https://i.postimg.cc/PxN0YsKv/Untitled.png)
Title: Re: Adding discord in server
Post by: habi on Jun 20, 2022, 04:07 PM
Put :: before ClientMessageToAll.
::ClientMessageToAll( );
Title: Re: Adding discord in server
Post by: Nihongo^ on Jun 20, 2022, 04:19 PM
Quote from: habi on Jun 20, 2022, 04:07 PMPut :: before ClientMessageToAll.
::ClientMessageToAll( );
Oh wow its works thank you so much for the help
God blase you
you just solve my problem :)