2022-01-04 04:21:13 +01:00
var Client = require ( './lib/Client.js' ) ;
2022-01-05 01:01:27 +01:00
var WebSocketMessageCollector = require ( "./lib/datacollector" ) ;
2022-01-06 03:12:13 +01:00
var DiscordMessageSender = require ( "./lib/DiscordMessageSender" ) ;
2018-07-14 01:01:37 +02:00
2022-01-04 04:04:16 +01:00
global . createMPPbridge = async function createMPPbridge ( { room , channel , uri } ) {
channel = dClient . channels . resolve ( channel ) ;
2022-01-06 03:12:13 +01:00
var d = new DiscordMessageSender ( channel ) ;
2018-07-14 01:01:37 +02:00
2022-01-04 04:04:16 +01:00
const gClient = new Client ( uri ) ;
if ( uri == "wss://mppclone.com:8443" ) gClient . token = config . mpc _token ; //todo hmm
gClient . setChannel ( room , { visible : false } ) ;
2018-09-04 07:54:35 +02:00
gClient . start ( ) ;
2018-07-14 01:01:37 +02:00
2018-09-04 08:36:06 +02:00
// maintain the client's presence in the channel
2018-09-03 23:01:13 +02:00
gClient . channelCorrectorInterval = setInterval ( ( ) => {
2018-09-04 07:54:35 +02:00
// if client is connected and not in a channel (meaning setChannel failed due to ratelimit because another client joined a channel with the same user within the last second) OR client is in a channel but it is not the right channel…
if ( ( gClient . isConnected ( ) && ! gClient . channel ) || ( gClient . channel && gClient . channel . _id != room ) )
// …set the channel!
2019-01-26 00:45:11 +01:00
gClient . setChannel ( room , { visible : false } ) ;
2018-09-03 23:01:13 +02:00
} , 1000 ) ;
2018-11-29 21:37:29 +01:00
let lastError ;
2018-11-29 00:13:10 +01:00
gClient . on ( "error" , error => {
2022-02-25 00:03:50 +01:00
//handleError(error, `[${uri}][${room}]`);
2018-11-29 21:37:29 +01:00
error = error . toString ( ) ;
if ( lastError != error ) {
2022-01-06 03:12:13 +01:00
d . send ( ` ** ${ error . toString ( ) } ** ` ) ;
2018-11-29 21:37:29 +01:00
lastError = error ;
}
2018-11-29 00:13:10 +01:00
} ) ;
2018-09-04 08:36:06 +02:00
var isConnected = false ; // TODO use gClient.isConnected() ?
2018-07-14 01:01:37 +02:00
gClient . on ( 'connect' , ( ) => {
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ][ ${ room } ] Connected to server ` ) ;
2022-01-06 03:12:13 +01:00
d . send ( ` **Connected to server; joining channel…** ` ) ;
2018-07-14 01:01:37 +02:00
isConnected = true ;
2018-11-29 22:16:39 +01:00
lastError = undefined ;
2018-07-14 01:01:37 +02:00
} ) ;
2018-07-14 07:04:28 +02:00
gClient . on ( 'hi' , ( ) => {
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ][ ${ room } ] Received greeting ` ) ;
2018-11-30 23:50:08 +01:00
if ( ! testmode ) {
2022-01-04 04:04:16 +01:00
gClient . sendArray ( [ { m : "userset" , set : { name : config . mppname } } ] ) ;
2018-11-30 23:50:08 +01:00
}
2018-12-01 08:40:20 +01:00
gClient . sendArray ( [ { m : 'm' , x : Math . floor ( Math . random ( ) * 100 ) , y : Math . floor ( Math . random ( ) * 100 ) } ] )
2018-07-14 07:04:28 +02:00
} ) ;
2018-07-14 01:01:37 +02:00
gClient . on ( 'disconnect' , ( ) => {
if ( isConnected ) {
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ][ ${ room } ] Disconnected from server ` ) ;
2022-01-06 03:12:13 +01:00
d . send ( ` **Disconnected from server** ` ) ;
2018-07-14 01:01:37 +02:00
isConnected = false ;
}
} ) ;
/ * g C l i e n t . o n ( ' s t a t u s ' , s t a t u s = > {
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ] [ ${ room } ] ${ status } ` ) ;
2018-07-14 01:01:37 +02:00
} ) ; * /
2018-09-04 08:36:06 +02:00
// on channel change
{
let lastCh ;
gClient . on ( 'ch' , async msg => {
// announce channel join
if ( ! lastCh ) {
2022-01-06 03:12:13 +01:00
d . send ( ` **Joined channel \` ${ msg . ch . _id } \` ** ` ) ;
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ][ ${ room } ] Joined channel ${ msg . ch . _id } ` ) ;
2018-07-14 01:01:37 +02:00
}
2018-09-04 08:36:06 +02:00
// announce channel change
else if ( msg . ch . _id !== lastCh ) {
2022-01-06 03:12:13 +01:00
d . send ( ` **Channel changed from \` ${ lastCh } \` to \` ${ msg . ch . _id } \` ** ` ) ;
2022-01-04 04:04:16 +01:00
console . log ( ` [ ${ uri } ][ ${ room } ] Channel changed from ${ lastCh } to ${ msg . ch . _id } ` ) ;
2018-07-14 01:01:37 +02:00
}
2018-09-04 08:36:06 +02:00
lastCh = msg . ch . _id ;
} ) ;
2018-11-26 23:50:17 +01:00
gClient . on ( "disconnect" , ( ) => lastCh = undefined ) ;
2018-09-04 08:36:06 +02:00
}
2018-07-14 01:01:37 +02:00
// MPP to Discord
2018-09-04 08:36:06 +02:00
gClient . on ( 'a' , async msg => {
2022-01-04 07:57:17 +01:00
if ( msg . p . _id == gClient . getOwnParticipant ( ) . _id ) return ;
2018-07-14 01:01:37 +02:00
var id = msg . p . _id . substr ( 0 , 6 ) ;
2018-09-19 08:28:54 +02:00
var name = sanitizeName ( msg . p . name ) ;
2022-01-04 06:44:30 +01:00
var content = msg . a ;
2018-11-09 22:10:20 +01:00
var str = ` \` ${ id } \` ** ${ name } :** ${ content } ` ;
2022-01-06 03:12:13 +01:00
d . send ( str ) ;
2018-07-14 01:01:37 +02:00
} ) ;
// Discord to MPP
2018-09-04 08:23:19 +02:00
{
2022-01-04 07:57:17 +01:00
let msgQueue = [ ] ;
2018-09-04 08:36:06 +02:00
dClient . on ( 'message' , async message => {
2022-01-04 04:04:16 +01:00
if ( message . channel . id !== channel . id || message . author . id == dClient . user . id || ! message . member /*|| message.content.startsWith('!')*/ ) return ;
2018-09-04 08:23:19 +02:00
var str = message . cleanContent ;
2022-01-04 06:45:48 +01:00
var aname = message . author . tag ;
2022-01-04 07:57:17 +01:00
if ( str . startsWith ( '/' ) || str . startsWith ( '\\' ) )
msgQueue . push ( ` ⤹ ${ aname } ` ) ;
else
2018-10-03 21:06:20 +02:00
str = ` ${ aname } : ${ str } ` ;
2022-01-04 07:57:17 +01:00
if ( str . startsWith ( '\\' ) ) str = str . slice ( 1 ) ;
2022-01-04 06:56:30 +01:00
if ( message . attachments . size > 0 ) str += ' ' + message . attachments . map ( a => a . url ) . join ( ' ' ) ;
2022-01-04 07:57:17 +01:00
if ( str . length > 512 ) {
str = str . substr ( 0 , 511 ) + '…' ;
message . react ( '⚠' ) ;
}
msgQueue . push ( str ) ;
2018-09-04 08:23:19 +02:00
} ) ;
2022-01-04 07:57:17 +01:00
setInterval ( ( ) => {
let message = msgQueue . shift ( ) ;
if ( message ) gClient . sendArray ( [ { m : 'a' , message } ] ) ;
//todo wait moment to see if message got through then react warning if didnt
} , 1600 ) ; // just about fastest without exceeding quota; I figured quota is 4 messages per 6 seconds in lobbies
2018-09-04 08:23:19 +02:00
}
2018-07-14 01:01:37 +02:00
2018-12-14 06:31:38 +01:00
// announce join/leave/rename
2018-12-14 07:07:35 +01:00
gClient . prependListener ( "p" , async participant => {
2023-10-11 01:14:42 +02:00
if ( gClient . ppl [ participant . id ] ) { // is update
let oldName = gClient . ppl [ participant . id ] . name , newName = participant . name ;
if ( newName != oldName )
d . send ( ` \` ${ participant . _id . substr ( 0 , 6 ) } \` ___** ${ sanitizeName ( oldName ) } ** changed their name to ** ${ sanitizeName ( newName ) } **___ ` ) ;
} else { // is join
d . send ( ` \` ${ participant . _id . substr ( 0 , 6 ) } \` ___** ${ sanitizeName ( participant . name ) } ** entered the room.___ ` ) ;
}
2018-07-14 01:01:37 +02:00
} ) ;
2018-12-14 07:07:35 +01:00
gClient . prependListener ( "bye" , async msg => {
2023-10-11 01:14:42 +02:00
var participant = gClient . ppl [ msg . p ] ;
2023-10-11 01:04:19 +02:00
if ( ! participant ) return ;
2023-10-11 01:14:42 +02:00
d . send ( ` \` ${ participant . _id . substr ( 0 , 6 ) } \` ___** ${ sanitizeName ( participant . name ) } ** left the room.___ ` ) ;
} ) ;
2018-07-14 01:01:37 +02:00
2018-09-04 08:36:06 +02:00
// on notifications
2018-07-14 01:01:37 +02:00
gClient . on ( 'notification' , async msg => {
// show notification
2022-01-06 03:12:13 +01:00
d . sendEmbed ( {
2018-07-14 01:01:37 +02:00
title : msg . title ,
description : msg . text || msg . html
2022-01-06 03:12:13 +01:00
} ) ;
2018-07-14 01:01:37 +02:00
// handle bans
2020-10-18 22:26:18 +02:00
if ( msg . text && ( msg . text . startsWith ( 'Banned from "' + room + '"' ) || msg . text . startsWith ( 'Currently banned from "' + room + '"' ) ) ) {
2023-10-11 01:14:42 +02:00
// Banned from "{room}" for {n} minutes.
// Currently banned from "{room}" for {n} minutes.
2018-07-14 01:01:37 +02:00
let arr = msg . text . split ( ' ' ) ;
arr . pop ( ) ;
let minutes = arr . pop ( ) ;
gClient . stop ( ) ;
setTimeout ( ( ) => {
gClient . setChannel ( room ) ;
gClient . start ( ) ;
} , minutes * 60 * 1000 + 3000 ) ;
2022-01-06 03:12:13 +01:00
d . send ( ` **Attempting to rejoin in ${ minutes } minutes.** ` ) ;
2018-07-14 01:01:37 +02:00
}
} ) ;
// make room invisible when nobody else is in it
2018-09-04 08:36:06 +02:00
gClient . on ( "ch" , async function ( msg ) {
2018-07-14 01:01:37 +02:00
if ( gClient . isOwner ( ) ) {
if ( gClient . countParticipants ( ) <= 1 ) {
gClient . sendArray ( [ { m : 'chset' , set : { visible : false } } ] )
} else {
gClient . sendArray ( [ { m : 'chset' , set : { visible : true } } ] )
}
}
} ) ;
2023-10-11 01:14:42 +02:00
2018-07-14 01:01:37 +02:00
// addons
2022-01-05 01:01:27 +01:00
var wsc = new WebSocketMessageCollector ( async function ( data , startDate , endDate ) {
var attachmentName = ` ${ uri } ${ room } raw data recording from ${ startDate . toISOString ( ) } to ${ endDate . toISOString ( ) } .txt.gz ` ;
2022-01-05 01:28:12 +01:00
await channel . send ( { files : [ {
attachment : data ,
name : attachmentName
} ] } ) ;
2022-01-05 01:01:27 +01:00
} ) ;
gClient . on ( "message" , wsc . collect . bind ( wsc ) ) ;
2018-07-14 01:01:37 +02:00
2022-01-04 04:04:16 +01:00
return gClient ;
2018-07-14 05:31:56 +02:00
} ;
2018-07-14 01:01:37 +02:00
// start
2022-01-04 08:00:36 +01:00
dClient . once ( "ready" , async function ( ) {
2022-01-04 04:04:16 +01:00
global . bridges = require ( "./bridges" ) ;
for ( let bridge of bridges ) {
try {
bridge . client = await createMPPbridge ( bridge ) ;
} catch ( e ) {
2022-01-04 05:40:43 +01:00
handleError ( error , JSON . stringify ( bridge ) ) ;
2022-01-04 04:04:16 +01:00
}
2018-07-14 01:01:37 +02:00
}
2023-10-11 01:04:19 +02:00
} ) ;