Currently the developers are putting their own money into JC2-MP to keep the servers online.

Please take a few seconds of your time and disable your AdBlock plugin for our website.

Ad revenue is not going to developers, it is used purely for covering our hosting costs.

 

You are also free to donate, which removes all ads from our website!

Patch 0.3 was just released! Full changelog here: https://t.co/4A50m6IKen

2 years ago

Advertisement
May 24, 2019, 02:57:51 pm

Author Topic: JC2MP Discord Bridge  (Read 7430 times)

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
JC2MP Discord Bridge
« on: March 07, 2016, 10:31:00 pm »
Discord Bridge



Discord is a relatively new text and voice chat client. It's also free and multi-platform, but still in development.

This here is a module that connects the JC2MP chat to a Discord text channel.

To implement this, I originally wrote a self-contained Discord wrapper for JC2MP using LuaSocket + LuaSec, but there were problems with this. HTTP requests were locking up the JC2MP server, regardless of whether they were made asynchronous. I also couldn't find a good WebSocket library. After realizing that these problems would be difficult to overcome, I thought that any communication with Discord would have to be done with an external library, but for a time, the only idea for communication that I could come up with was writing and reading to and from a text file, which would have been a fantastic hack, but not at all efficient.

A few individuals had written libraries for building Discord bots in various languages, but none were written in Lua, with which I'm most familiar. So, I put this bridge on hold and went and wrote Discordia, Discord's first Lua library, written for the Luvit framework. Along the way, I learned a few tricks about networking, and libuv, the library that powers both Node.js and Luvit.

Now that Discordia has hit a stable state, I've revisited this bridge module. Knowing what I know now, I managed to implement a UDP link between Luvit and JC2MP, which can be used to relay messages between the JC2MP chat and a Discord bot running on Discordia. (If you'd like to use a library other than Discordia, you can, but you'd need to implement your own UDP server.)

Please let me know if there are any issues. Everything should be mostly stable, but realize that Discordia is in active development.

NB: Due to the low-level nature of this module, any errors in Lua code may crash your JC2MP server. Do not adjust the scripts unless you know what you're doing.



Download From Github

Video Demonstration
« Last Edit: November 19, 2016, 02:09:01 am by SinisterRectus »

LordNoob

  • Sr. Member
  • ****
  • Posts: 349
    • View Profile
Re: JC2MP Discord Bridge
« Reply #1 on: March 08, 2016, 11:14:38 pm »
Very nice script! I'm sure it will be useful to many communities once you develop the reverse integration.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #2 on: March 08, 2016, 11:30:15 pm »
Speaking of that...

The proper way to detect Discord messages would be to open a websocket connection and listen for certain Discord events. I haven't yet figured out an easy way to do that, since support for SSL websocket connections in Lua is hard to find.

Never the less, I did implement another method. I added to the test script a PreTick subscription that checks the active channel for new messages every second, and if they are detected, they are sent to the JC2MP chat.

I also added a bugfix for unicode characters, and made some minor adjustments.

Please note that if you plan to implement this module on your server, the HTTPS requests are not asynchronous. A large amount of messages may lag your server. I might fix that in the future.

Check GitHub for the updates and have a new video: https://youtu.be/8_gv58t0sHQ

Jman100

  • Sr. Member
  • ****
  • Posts: 325
    • View Profile
Re: JC2MP Discord Bridge
« Reply #3 on: March 09, 2016, 01:06:50 am »
Awesome work, as always. I've set this up on my discord. Anyone interested: http://bit.ly/JmanDiscord

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #4 on: March 15, 2016, 09:44:43 pm »
I added an asynchronous version in the async branch using Copas, which is a pure Lua wrapper for LuaSocket and LuaSec. In this branch, JC2MP to Discord communication works, but the reverse is not yet implemented.
« Last Edit: March 15, 2016, 10:26:55 pm by SinisterRectus »

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #5 on: March 23, 2016, 01:26:52 pm »
I've completely re-written the test script into a proper DiscordBridge class for the master branch.

  • A queue is now used to buffer HTTP requests. Default dequeue delay is 1 second.
  • All login parameters are set in DiscordBridge:ModuleLoad().
  • Incoming and outgoing message handling has been split between two distinct functions.
  • Added some more exception handling and minor tweaks.

*** Note: There is currently a bug that prevents both message listeners from being used at the same time. I'm not sure what the cause is. Until it is fixed, you can only have one way traffic. Choose which one you want and uncomment the necessary function call.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #6 on: April 09, 2016, 01:47:55 am »
Still haven't been able to sort out the "send message" issue, but I think it has something to do with POST requests getting caught up in Luasocket.

Nevertheless, Discord's web API is now official. See https://blog.discordapp.com/official-api-roadmap-vroom-vroom/ for more info.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #7 on: April 28, 2016, 12:14:06 am »
Updated to use an external Discord library. Please read the original post, as it contains all new information.

Darwood37

  • Donator
  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Re: JC2MP Discord Bridge
« Reply #8 on: November 13, 2016, 02:10:43 am »
Hi Sinister,

Having  a few issues trying to get this working. I have installed lit, luvit and discordia, created a bot etc. But i am not sure on what the directory structure should be for discordia's reps directory. it is currently scripts/JC2MP-DiscordBridge/reps/. When i  "luvit discordtojcmp.lua" i get the following errors:

Code: [Select]
Uncaught exception:
/home/minecraft/jc2/scripts/discordbridge/discordtojcmp.lua:7: attempt to call method 'new' (a nil value)
stack traceback:
        /home/minecraft/jc2/scripts/discordbridge/discordtojcmp.lua:7: in function 'fn'
        [string "bundle:deps/require.lua"]:309: in function 'require'
        [string "bundle:main.lua"]:118: in function 'main'
        [string "bundle:init.lua"]:49: in function <[string "bundle:init.lua"]:47>
        [C]: in function 'xpcall'
        [string "bundle:init.lua"]:47: in function 'fn'
        [string "bundle:deps/require.lua"]:309: in function <[string "bundle:dep

i have luvit and lit in my system path, when i just type luvit i get "Welcome to the Luvit repl!". I am running the luvit command in the scripts/JC2MP-DiscordBridge directory. Also when i try to load the JC2MP module i get errors but it is probably because of the luvit error, you state the luvit process must be executed first.

I also added the name of our server, the channel (it exists) and the token from my bot to the discordtojcmp.lua. Thanks.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #9 on: November 13, 2016, 04:57:44 am »
I'm actually in the process of rewriting Discordia, my Discord library. You probably installed a newer version. There are breaking changes, and I have not yet updated this module to compensate. I will let you know when I do.

Darwood37

  • Donator
  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Re: JC2MP Discord Bridge
« Reply #10 on: November 13, 2016, 02:24:23 pm »
OK Cheers. Really looking forward to getting this up and running. I have a similar setup for our Garry's Mod DarkRP server.

Update: was wondering if it was possible to add when a player joins and leaves the server, a notification in discord?

I have the same version of luvit as Jman and i am getting the following errors:
Code: [Select]
[[email protected] discordbridge]$ luvit discordtojcmp.lua
Uncaught exception:
[string "bundle:deps/require.lua"]:278: No such module 'md5' in '/home/minecraft/deps/discordia/client.lua'
module 'md5' not found:
        no field package.preload['md5']
        no file './md5.lua'
        no file '/usr/local/share/luajit-2.1.0-beta2/md5.lua'
        no file '/usr/local/share/lua/5.1/md5.lua'
        no file '/usr/local/share/lua/5.1/md5/init.lua'
        no file './md5.so'
        no file '/usr/local/lib/lua/5.1/md5.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'error'
        [string "bundle:deps/require.lua"]:278: in function 'require'
        [string "/home/minecraft/deps/discordia/client.lua"]:1: in function 'fn'
        [string "bundle:deps/require.lua"]:300: in function 'require'
        [string "/home/minecraft/deps/discordia/init.lua"]:6: in function 'fn'
        [string "bundle:deps/require.lua"]:300: in function 'require'
        [string "/home/minecraft/jc2/scripts/discordbridge/dis..."]:5: in function 'fn'
        [string "bundle:deps/require.lua"]:300: in function 'require'
        [string "bundle:main.lua"]:118: in function 'main'
        [string "bundle:init.lua"]:49: in function <[string "bundle:init.lua"]:47>
        [C]: in function 'xpcall'
        [string "bundle:init.lua"]:47: in function 'fn'
        [string "bundle:deps/require.lua"]:300: in function <[string "bundle:deps/require.lua"]:265>

UPDATE: Got it working, now all i need is to add a notification to discord each time a player joins and leaves the server.

SECOND UPDATE I have filtered all chat messages that begin with / (thanks Jman) and I have added player join and leave notifications (thanks me), pretty neat.
« Last Edit: November 15, 2016, 11:19:02 pm by Darwood37 »

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #11 on: November 19, 2016, 01:18:10 am »
Just pushed an update for this. Requires Discordia 1.0 to run.

- Changed config names to IDs to guarantee object uniqueness.
- Removed the message queue since the library now properly handles ratelimiting.
- Switched from TCP to UDP. This simplifies most of the connection code and should prevent server crashes when reloading modules.
- Added support for Windows.
- General cleanup and optimizations.

Let me know if you have problems. :)

Darwood37

  • Donator
  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Re: JC2MP Discord Bridge
« Reply #12 on: November 20, 2016, 07:44:16 pm »
Cool, when i get a chance i will give it a try, thanks. BTW the previous has been running on my server flawlessly except for the fact when i try to access the DB through the function ModuleLoad in the jcmptodiscord.lua it always crashes the server. I was trying to pull some general info to send back to the discord server by checkng for commands in the PlayerChat function within.

SinisterRectus

  • JC2-MP Betatester
  • Sr. Member
  • *****
  • Posts: 451
    • View Profile
Re: JC2MP Discord Bridge
« Reply #13 on: November 20, 2016, 08:21:26 pm »
You description is too vague. I'd need to see the code. Try the new version, though.

Darwood37

  • Donator
  • Hero Member
  • *****
  • Posts: 690
    • View Profile
Re: JC2MP Discord Bridge
« Reply #14 on: November 20, 2016, 08:59:13 pm »
Vague yes, cause i do not really care it was just a side project, right now i do not care if it does not work but i thought i would mention it. I will PM you the code anyway.