Jump to content

[SHOW] - MTA - Discord integration API


specahawk

Recommended Posts

10 hours ago, 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.

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
50 minutes ago, 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?

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.

56 minutes ago, 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.

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

Link to comment
  • MTA Team
5 hours ago, 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!

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
Spoiler

With cmd: node app


library\Bot.js:74
    loginWithToken(token, callback = () => {}) {
                                   ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Program Files (x86)\MTA San Andreas 1.5\mta-discor
d-bot-master\src\app.js:3:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

with pm2


SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function._load (C:\Users\merotto\AppData\Roaming\npm\node_modules\pm2\node_modules\pmx\lib\transaction.js:62:21)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Program Files (x86)\MTA San Andreas 1.5\mta-discord-bot-master\src\app.js:3:31)
    at Module._compile (module.js:409:26)

 

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

Link to comment
8 hours ago, LoPollo said:
  Hide contents

With cmd: node app



library\Bot.js:74
    loginWithToken(token, callback = () => {}) {
                                   ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Program Files (x86)\MTA San Andreas 1.5\mta-discor
d-bot-master\src\app.js:3:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

with pm2



 
SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function._load (C:\Users\merotto\AppData\Roaming\npm\node_modules\pm2\node_modules\pmx\lib\transaction.js:62:21)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Program Files (x86)\MTA San Andreas 1.5\mta-discord-bot-master\src\app.js:3:31)
    at Module._compile (module.js:409:26)

 

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

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
1 hour ago, 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

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 7.1.2017 at 2:47 PM, K1parik said:

why is that?

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

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...
1 hour ago, Syntrax# said:

getting the error message 
Error: Cannot find module 'discord.js'

when trying to run the app.js in my Debian 7 64 bit VPS.Anyone knows what is causing the problem?

Fixed already, but somehow the bot keeps reconnecting every second

ru2xhw.png

@Necktrox

Edited by Syntrax#
Link to comment
  • 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...