120 lines
3.0 KiB
TypeScript
120 lines
3.0 KiB
TypeScript
import {
|
|
Plugin,
|
|
Configurable,
|
|
EventListener,
|
|
DependencyLoad,
|
|
DependencyUnload
|
|
} from '@squeebot/core/lib/plugin';
|
|
|
|
import { IMessage, MessageResolver } from '@squeebot/core/lib/types';
|
|
import { fullIDMatcher } from '@squeebot/core/lib/common';
|
|
|
|
interface Monitor {
|
|
rooms: string[];
|
|
protocol: string;
|
|
}
|
|
|
|
/*
|
|
Monitor:
|
|
{
|
|
rooms: ['irc/icynet/#diamond'],
|
|
protocol: 'syncplay'
|
|
}
|
|
*/
|
|
|
|
@Configurable({
|
|
monitors: []
|
|
})
|
|
class SPSPlugin extends Plugin {
|
|
public syncPlugin: Plugin | null = null;
|
|
|
|
@EventListener('pluginUnload')
|
|
public unloadEventHandler(plugin: string | Plugin): void {
|
|
if (plugin === this.name || plugin === this) {
|
|
this.config.save().then(() =>
|
|
this.emit('pluginUnloaded', this));
|
|
}
|
|
}
|
|
|
|
@DependencyLoad('simplecommands')
|
|
addCommands(cmd: any): void {
|
|
const rooms: string[] = [];
|
|
this.config.get('monitors', []).forEach(
|
|
(monitor: Monitor) => rooms.push(...monitor.rooms));
|
|
|
|
if (!rooms.length) {
|
|
return;
|
|
}
|
|
|
|
cmd.registerCommand({
|
|
name: 'syncplay',
|
|
plugin: this.name,
|
|
execute: async (msg: IMessage, msr: MessageResolver, spec: any, prefix: string, ...simplified: any[]): Promise<boolean> => {
|
|
if (!msg.fullRoomID || !this.syncPlugin) {
|
|
return true;
|
|
}
|
|
let monitor: Monitor | undefined;
|
|
this.config.get('monitors', []).forEach((mon: Monitor) => {
|
|
let matched = false;
|
|
for (const r of mon.rooms) {
|
|
if (fullIDMatcher(msg.fullRoomID as string, r)) {
|
|
matched = true;
|
|
break;
|
|
}
|
|
}
|
|
if (matched) {
|
|
monitor = mon;
|
|
}
|
|
});
|
|
if (!monitor) {
|
|
return true;
|
|
}
|
|
|
|
const syncServ = this.syncPlugin.service?.getProtocolByName(monitor.protocol) as any;
|
|
if (!syncServ) {
|
|
return true;
|
|
}
|
|
|
|
const keys = [];
|
|
|
|
keys.push(['field', 'Syncplay', { color: 'orange', type: 'title' }]);
|
|
|
|
const realViews = Object.keys(syncServ.users).length - 1;
|
|
const cfg = syncServ.config.syncplay;
|
|
|
|
if (simplified[0] && simplified[0].toLowerCase() === 'users') {
|
|
const w = Object.keys(syncServ.users).filter((x) => x !== cfg.name);
|
|
|
|
keys.push(['field', realViews > 0 ?
|
|
w.join(', ') : 'No users', { label: 'Users online' }]);
|
|
|
|
msg.resolve(keys);
|
|
return true;
|
|
}
|
|
|
|
keys.push(['field', cfg.room, { label: 'Room', type: 'description' }]);
|
|
keys.push(['field', `${cfg.host}:${cfg.port}`, { label: 'Address' }]);
|
|
keys.push(['field', realViews, { label: 'Users' }]);
|
|
|
|
msg.resolve(keys);
|
|
return true;
|
|
},
|
|
usage: '[users]',
|
|
description: 'Show Syncplay status',
|
|
source: rooms,
|
|
});
|
|
}
|
|
|
|
@DependencyLoad('syncplay')
|
|
syncplayReady(plugin: any): void {
|
|
this.syncPlugin = plugin;
|
|
}
|
|
|
|
@DependencyUnload('syncplay')
|
|
syncplayUnready(): void {
|
|
this.syncPlugin = null;
|
|
}
|
|
}
|
|
|
|
module.exports = SPSPlugin;
|