Popular Post specahawk Posted December 1, 2016 Popular Post Share Posted December 1, 2016 I brought you bakaGaijin and Ash Now I bring Discord integration with MTA scripts. MTA already has this for IRC Shoutout to the people who made the Sockets module, you're awesome. Features: -Scripts can echo messages to any Discord text channel of their choosing. -Any Discord channel can be set up to echo all messages to an ingame script. (Including the name of the person who said it, and his role) -One MTA server can send/receive to multiple Discord guilds. Example use: To show how this is useful, I made a small program to echo all global chat to a "global" Discord channel, and all team chats to individual "team" Discord channels. These Discord channels ofc echo messages back when someone posts. As proof of concept for commands, if a Discord user types "!ban name" then his role is checked, and if it includes Founder, the chat outputs "name was banned!" This is the client side script I used for this example: local SECRET_KEY = "15-A-53CR3T-K3Y" --The script works even if your server IP changes. You are mapped to a unique key. local function onopen(self) self:send(SECRET_KEY) --Your MTA server logs in addEventHandler("onResourceStop", resourceRoot, function() self:close() --Break off from Discord end) addEventHandler("onPlayerChat", getRootElement(), function(message, type) --Example hook to capture player chats local name = getPlayerName(source) local channel = "global" --Send to global channel if global chat if type==2 then channel = getTeamName(getPlayerTeam(source)) end --Or team channel if teamchat --Format to send messages is ("discord", channelName, message) self:send("discord", channel, name..": "..message) end) end function onmessage(self, data) local channelName, name, role, message = data[3], data[4], data[5], data[6] local orginal_message = message --The message we got from discord message = "("..role..") "..name..": "..message --Make it pretty if channelName=="global" then --Output to global chat or team chat outputChatBox("(DISCORD) "..message) else local team = getTeamFromName(channelName) local members = getPlayersInTeam(team) or {} local r, g, b = getTeamColor(team) --Color the output for lulz for _, player in ipairs(members) do outputChatBox( "(DISCORD) (TEAM) "..message, player, r, g, b) end end local commandExample = string.match(orginal_message, "^!ban (.+)") --If message started with !ban... if role=="Founders" and commandExample then -- ...and the person who said it had the right Role outputChatBox(commandExample.." was banned!", getRootElement(), 255, 0, 0) end end local function onclose() outputChatBox("The Discord link was closed") end local discord = Discord.new(onopen, onmessage, onclose) That's 41 lines of code, now let's see it in effect. I would love to hear what you think about it. 10 Link to comment
Best-Killer Posted December 1, 2016 Share Posted December 1, 2016 Awesome Work ^^ good joob keep it , i like it Link to comment
Senpai Posted December 1, 2016 Share Posted December 1, 2016 Will you be releasing this? Link to comment
MTA Team Popular Post botder Posted December 5, 2016 MTA Team Popular Post Share Posted December 5, 2016 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. 5 Link to comment
specahawk Posted December 5, 2016 Author Share Posted December 5, 2016 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 botder Posted December 5, 2016 MTA Team Share Posted December 5, 2016 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
LoPollo Posted December 5, 2016 Share Posted December 5, 2016 @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! @specahawk you made a great work too! Much more than most people in the community can achieve (including me) i'm hoping in a relase! Link to comment
MTA Team botder Posted December 6, 2016 MTA Team Share Posted December 6, 2016 (edited) 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 Visit Applications @ discordapp.com Create a new application if not done yet or select an existing one Create a bot user for the application Reveal the bot user token (you have to click on 'click to reveal') 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? { "port": 8100, // You can use any port you want "passphrase": "super-secret-password", // You can use any passphrase you want "server": "228439442069651456", // Discord server/guild id from Mr.Green Discord guild "bots": [{ "channel": "race", // Name of the channel the bot communicates on (here: #race, do not include the hash #) "token": "bot.token" // Your secret bot user token (do not share this code with others) }] } Warning: JSON configuration files do not support comments. Remove these. Edited December 6, 2016 by Necktrox 1 Link to comment
LoPollo Posted December 6, 2016 Share Posted December 6, 2016 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
specahawk Posted December 6, 2016 Author Share Posted December 6, 2016 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) 1 Link to comment
LoPollo Posted December 7, 2016 Share Posted December 7, 2016 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 botder Posted December 7, 2016 MTA Team Share Posted December 7, 2016 @LoPollo Did you use this token? Spoiler Link to comment
LoPollo Posted December 7, 2016 Share Posted December 7, 2016 (edited) 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 Edited December 7, 2016 by LoPollo Link to comment
MTA Team botder Posted December 8, 2016 MTA Team Share Posted December 8, 2016 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. 1 Link to comment
LoPollo Posted December 8, 2016 Share Posted December 8, 2016 It's working Thank you! Link to comment
K1parik Posted January 7, 2017 Share Posted January 7, 2017 why is that? ERROR: discord/socket.lua:56: attempt to call global 'sockOpen' (a nil value) Link to comment
MTA Team botder Posted January 10, 2017 MTA Team Share Posted January 10, 2017 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. 1 Link to comment
LeonardoS'k Posted February 17, 2017 Share Posted February 17, 2017 Very good! Thanks you. Link to comment
Syntrax# Posted April 10, 2017 Share Posted April 10, 2017 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? Link to comment
Syntrax# Posted April 10, 2017 Share Posted April 10, 2017 (edited) 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 @Necktrox Edited April 10, 2017 by Syntrax# Link to comment
Tails Posted April 10, 2017 Share Posted April 10, 2017 @Necktrox Getting connected, authorized and everything. Everything's set up correctly but the bot's not joining my discord channel. Am I missing something? Link to comment
Syntrax# Posted April 10, 2017 Share Posted April 10, 2017 2 hours ago, Tails said: @Necktrox Getting connected, authorized and everything. Everything's set up correctly but the bot's not joining my discord channel. Am I missing something? Fixed my problem, Contact me on discord 1 Link to comment
MTA Team botder Posted April 21, 2017 MTA Team Share Posted April 21, 2017 2 hours ago, HooliGans said: Dowland link ? Learn to read. https://github.com/Necktrox/mta-discord-bot 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now