Jump to content

[SHOW] - MTA - Discord integration API


specahawk

Recommended Posts

  On 05/12/2016 at 03:51, Necktrox said:

I have released a self-hosted Discord relay some time ago, but didn't bother posting here or adding documentation. @specahawk, you can tell me to remove this post if you don't want to have this advertising in your thread. Anyway, the code is on GitHub (Necktrox/mta-discord-bot) and it's being used by Mr.Green, one unknown guy and my clan's server. It doesn't support any commands, but the implementation is up to you (the bot recognizes commands with dot (.) prefix). It's running perfectly stable for months.

Expand  

I love the code, it's very nice. Much better than the hack I put together.
A few things I want to ask:
The split function in util splits according to the separator, but do you escape the separator (like URL encode it maybe) anywhere? If the separator can be a part of the actual message then I have some snippet I made for this project to safely escape/unescape strings which I can send a pull request for.
Also, I don't understand why you have separate resources for the events and the actual API. This is a design decision ofc, but is it worth running two separate Lua VMs?

Link to comment
  • MTA Team
  On 05/12/2016 at 15:42, specahawk said:

Also, I don't understand why you have separate resources for the events and the actual API. This is a design decision ofc, but is it worth running two separate Lua VMs?

Expand  

One the one hand you are forced to keep OOP disabled in any resource, which uses the socket module's functions; and I like to use the OOP methods, because the code looks cleaner and easier to read when you try to understand it. On the other hand, you can restart the event handler resource without interrupting the connection with the Discord relay - I have to admit here, that I didn't have any specific reason not to use one resource, it was personal preference.

  On 05/12/2016 at 15:42, specahawk said:

The split function in util splits according to the separator, but do you escape the separator (like URL encode it maybe) anywhere? If the separator can be a part of the actual message then I have some snippet I made for this project to safely escape/unescape strings which I can send a pull request for.

Expand  

I actually encode the JSON string with base64 to avoid this issue, see here.

Link to comment
  • MTA Team
  On 05/12/2016 at 18:37, LoPollo said:

@Necktrox can you tell me how to use the bot? I know it's node.js (right?), npm to install, but then when i have to configure i have no idea what to do. BTW nice job!

Expand  

There are 3 parts, the relay server and 2 MTA resources. You can install PM2 (Unitech/pm2) to launch the relay node.js server. You only have to duplicate example.config.json as config.json, make your changes to the configuration and run the relay.

Below is a copy of my PM - how to configure the relay.

Token

  1. Visit Applications @ discordapp.com
  2. Create a new application if not done yet or select an existing one
  3. Create a bot user for the application
  4. Reveal the bot user token (you have to click on 'click to reveal')
  5. Copy the entire bot token and paste it into your config (see example below)

Note: Do not share this bot token with anybody, because they will have access to the bot user.

Passphrase

This is a random text passphrase you can set to anything you want. Make sure the MTA resource uses the same passphrase in the configuration, otherwise the MTA resource won't be able to authenticate with the node.js relay. The passphrase is like a login password just to make sure nobody can connect to the node.js relay and send messages/spam without your permission to do so.

Example Configuration?

  1. {
  2.     "port": 8100, // You can use any port you want
  3.     "passphrase": "super-secret-password", // You can use any passphrase you want
  4.     "server": "228439442069651456", // Discord server/guild id from Mr.Green Discord guild
  5.     "bots": [{
  6.         "channel": "race", // Name of the channel the bot communicates on (here: #race, do not include the hash #)
  7.         "token": "bot.token" // Your secret bot user token (do not share this code with others)
  8.     }]
  9. }

Warning: JSON configuration files do not support comments. Remove these.

Edited by Necktrox
  • Like 1
Link to comment
  Reveal hidden contents

I don't understand this error. Isn't the syntax correct??? PS: i'm running Win7, just so you know

Link to comment
  On 06/12/2016 at 13:14, LoPollo said:
  Reveal hidden contents

I don't understand this error. Isn't the syntax correct??? PS: i'm running Win7, just so you know

Expand  

You may have an older (LTS) version of note. Upgrade to 7+ and try
OR
Just remove the "callback =" part.
(If it is a function definition, then in the first line of the function add "if (callback==nil) { callback = () =:> {} }", it's a default value for the parameter)

  • Like 1
Link to comment

After updating to node.js 7.2.1 this error disappeared.

The bot connects to MTA server, which shows: 

[2016-12-07 16:00:49] INFO: [Discord] Connected to localhost on port 8100
[2016-12-07 16:00:49] INFO: [Discord] Authentication successful
[2016-12-07 16:00:49] INFO: [Discord] Bot isn't ready

that bot isn't ready is due to this error that shows in CMD (launched the bot with node app.js), i guess:

Bot false error: Error: Authentication failed
Error: Authentication failed
    at WebSocket.websocket.onclose (C:\Program Files (x86)\MTA San Andreas 1.5\m
ta-discord-bot-master\src\node_modules\discord.js\lib\Client\InternalClient.js:9
4:1122)
    at WebSocket.onClose (C:\Program Files (x86)\MTA San Andreas 1.5\mta-discord
-bot-master\src\node_modules\discord.js\node_modules\ws\lib\WebSocket.js:446:14)

    at emitTwo (events.js:106:13)
    at WebSocket.emit (events.js:191:7)
    at WebSocket.cleanupWebsocketResources (C:\Program Files (x86)\MTA San Andre
as 1.5\mta-discord-bot-master\src\node_modules\discord.js\node_modules\ws\lib\We
bSocket.js:950:8)
    at emitNone (events.js:91:20)
    at TLSSocket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Do i have configured something in the discord part wrong? 

Since the connection to MTA is done i think that i'm doing something wrong with the part of discord, but the Discord server id is correct (copy-paste from the app), also the channel name is correct. The token is correct for sure since it's copied-pasted too. No extra spaces inserted (actually none at all).

The port is NOT forwarded since i thought (and i still guess) that's the port to connect to MTA server, since it's in localhost it doesn't need to be forwarded.

 

Googling - discord.js auth failed - brings me nothing intresting

Link to comment
  • MTA Team
  On 07/12/2016 at 23:03, LoPollo said:

Sure, i copied pasted it

EDIT: nope i had in the clipboard the token of another app i tried to use for this, SO THE TOKEN WAS WRONG. But even after copying the correct token the problem is still here

Expand  

https://github.com/Necktrox/mta-discord-bot/commit/da6cd936a57ef106b2c612fd7dec0bc893de216c

Fixed, I didn't test this part of the code, because I modified my config to include the "Bot " prefix and never tested it without it. Sorry for any inconvenience.

  • Like 1
Link to comment
  • 5 weeks later...
  • MTA Team
  On 07/01/2017 at 13:47, K1parik said:

why is that?

ERROR: discord/socket.lua:56: attempt to call global 'sockOpen' (a nil value)

Expand  

You are missing the socket module for your MTA server. There is a check in place to prevent starting the resource without it, which you probably circumvented, because the resource stops immediately if it's not available.

  • Like 1
Link to comment
  • 1 month later...
  • 1 month later...
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...