import { Plugin, Configurable, EventListener, DependencyLoad } from '@squeebot/core/lib/plugin'; import { IMessage } from '@squeebot/core/lib/types'; import { query } from 'gamedig'; interface IMinecraftType { vanilla: { raw: { description: { extra: [ { text: string; } ]; }, players: { max: number, online: number; }, version: { name: string; } } }; } @Configurable({ games: [] }) class GamePlugin extends Plugin { @EventListener('pluginUnload') public unloadEventHandler(plugin: string | Plugin): void { if (plugin === this.name || plugin === this) { this.emit('pluginUnloaded', this); } } @DependencyLoad('simplecommands') addCommands(cmd: any): void { for (const i in this.config.get('games', [])) { const game = this.config.get('games', [])[i]; if (game.game === 'minecraft' && game.host) { const port = game.port || 25565; const command: any = { plugin: this.name, name: 'minecraft', execute: async (msg: IMessage, spec: any, prefix: string, ...simplified: any[]): Promise => { const keys: any[] = []; let inclPlayers = false; if (simplified[0] === 'players' || simplified[0] === 'online') { inclPlayers = true; } keys.push(['field', 'Minecraft', { type: 'title' }]); let state; try { state = await query({ type: 'minecraftping', host: game.host, port, }); if (!state) { throw new Error(); } } catch (e) { msg.resolve([['field', 'Server is offline.', { type: 'title' }]]); return true; } if (inclPlayers) { const players = []; if (state.players.length > 0) { for (const j in state.players) { players.push(state.players[j].name); } } keys.push(['field', players.length ? players.join(', ') : 'No players', { label: 'Players online' }]); msg.resolve(keys); return true; } keys.push(['field', state.connect, { label: 'Address' }]); const raw = state.raw as IMinecraftType; if (raw && raw.vanilla && raw.vanilla.raw) { keys.push(['field', raw.vanilla.raw.description.extra[0].text .replace(/ยง./g, '').replace(/\n/g, ' '), { label: 'MOTD', type: 'description' }]); keys.push(['field', state.players.length + '/' + state.maxplayers, { label: 'Players' }]); keys.push(['field', raw.vanilla.raw.version.name, { label: 'Version' }]); } msg.resolve(keys); return true; }, description: 'Minecraft server', usage: '[players]', aliases: ['mc'] }; if (game.rooms) { command.source = game.rooms; } cmd.registerCommand(command); } } } } module.exports = GamePlugin;