diff --git a/src/bot.ts b/src/bot.ts index 3174cca..7333315 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,6 +1,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import * as cron from 'node-cron'; +import { WebSocket } from 'ws'; +import { Actions } from './actions'; import { Config } from './config'; +import { bots, configs, db, wss } from './main'; import { PluginBase } from './plugin'; +import * as plugins from './plugins/index'; import { BroadcastMessage, Conversation, @@ -29,11 +34,6 @@ import { t, toBase64, } from './utils'; -import { WebSocket } from 'ws'; -import * as plugins from './plugins/index'; -import * as cron from 'node-cron'; -import { Actions } from './actions'; -import { bots, db, wss } from './main'; export class Bot { platform: string; @@ -453,15 +453,19 @@ export class Bot { async sendBroadcast(json: WSBroadcast): Promise { const broadcast: WSBroadcast = json; const conversation = broadcast.message.conversation; + let config = this.config; + if (!Array.isArray(broadcast.target)) { + config = configs[broadcast.target]; + } if (conversation.id === 'alerts') { - conversation.id = this.config.alertsConversationId; - conversation.title = await getFullName(this, this.config.alertsConversationId); + conversation.id = config.alertsConversationId; + conversation.title = await getFullName(this, config.alertsConversationId); } else if (conversation.id === 'admin') { - conversation.id = this.config.adminConversationId; - conversation.title = await getFullName(this, this.config.adminConversationId); + conversation.id = config.adminConversationId; + conversation.title = await getFullName(this, config.adminConversationId); } else if (conversation.id === 'owner') { - conversation.id = this.config.owner; - conversation.title = await getFullName(this, this.config.owner); + conversation.id = config.owner; + conversation.title = await getFullName(this, config.owner); } else { conversation.title = await getFullName(this, conversation.id); } diff --git a/src/main.ts b/src/main.ts index 22a07aa..f0b61b4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,8 @@ -import { WebSocketServer, WebSocket } from 'ws'; -import { BotSocket, MongoDatabases, WSInit, WSMessage, WSPong } from './types'; -import { catchException, logger } from './utils'; -import { Bot } from './bot'; import { MongoClient } from 'mongodb'; +import { WebSocket, WebSocketServer } from 'ws'; +import { Bot } from './bot'; +import { BotConfig, BotSocket, MongoDatabases, WSInit, WSMessage, WSPong } from './types'; +import { catchException, logger } from './utils'; let mongo: MongoClient; export const wss: WebSocketServer = new WebSocketServer({ port: 8080 }); @@ -31,6 +31,7 @@ process.on('exit', () => { }); export const bots: BotSocket = {}; +export const configs: BotConfig = {}; export const db: MongoDatabases = {}; const start = () => { @@ -64,6 +65,7 @@ const start = () => { } bot.initPlugins(); bots[bot.user.id] = ws; + configs[bot.user.id] = init.config; await bot.initTranslations(); logger.info( `✅ Connected as ${bot.config.icon} ${bot.user.firstName} (@${bot.user.username}) [${bot.user.id}] on platform '${init.platform}'`, diff --git a/src/plugins/world-of-warcraft.ts b/src/plugins/world-of-warcraft.ts index bcbdf47..904ea82 100644 --- a/src/plugins/world-of-warcraft.ts +++ b/src/plugins/world-of-warcraft.ts @@ -1,7 +1,9 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import FormData from 'form-data'; import format from 'string-format'; +import { Bot } from '../bot'; import { PluginBase } from '../plugin'; +import { Message } from '../types'; import { btoa, capitalize, @@ -15,8 +17,6 @@ import { sendRequest, setTag, } from '../utils'; -import { Bot } from '../bot'; -import { Message } from '../types'; export class WorldOfWarcraftPlugin extends PluginBase { accessToken: string; @@ -134,7 +134,7 @@ export class WorldOfWarcraftPlugin extends PluginBase { this.getCharacterStatistics(region, realm, characterName), this.getRaiderIO(region, realm, characterName), ]); - if (!character || !media || !raids || !pvp || !professions) { + if (!character || !raids || !pvp || !professions) { this.accessToken = await this.retrievingAccessToken(); return this.bot.replyMessage(msg, this.bot.errors.connectionError); } @@ -228,14 +228,17 @@ export class WorldOfWarcraftPlugin extends PluginBase { if (mythicScore.length == 0) { mythicScore = null; } - let asset = media.assets.find((asset) => asset.key === 'main'); - if (!asset) { - asset = media.assets.find((asset) => asset.key === 'inset'); - } - if (!asset) { - asset = media.assets.find((asset) => asset.key === 'avatar'); + let photo = null; + if (media) { + let asset = media.assets.find((asset) => asset.key === 'main'); + if (!asset) { + asset = media.assets.find((asset) => asset.key === 'inset'); + } + if (!asset) { + asset = media.assets.find((asset) => asset.key === 'avatar'); + } + photo = `${asset.value}?update=${Math.trunc(now() / 3600)}`; } - const photo = `${asset.value}?update=${Math.trunc(now() / 3600)}`; text = `${title ? title + '\n\t' : ''}${name}\n${ guild ? guild + '\n\n' : '' }${characterClass}\n\t${race}\n\n${info}\n\n${stats}\n\n${professionLevels ? professionLevels + '\n\n' : ''}${ @@ -243,6 +246,8 @@ export class WorldOfWarcraftPlugin extends PluginBase { }${raidProgression ? raidProgression + '\n\n' : ''}`; if (photo) { return this.bot.replyMessage(msg, photo, 'photo', null, { caption: text }); + } else { + return this.bot.replyMessage(msg, text, 'text', null); } } else if (isCommand(this, 2, msg.content)) { if (!input) { diff --git a/src/types.ts b/src/types.ts index e61fbe8..9ce576f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { WebSocket } from 'ws'; import { Db } from 'mongodb'; +import { WebSocket } from 'ws'; import { Config } from './config'; export class ErrorMessages { @@ -180,6 +180,10 @@ export interface BotSocket { [id: string]: WebSocket; } +export interface BotConfig { + [id: string]: Config; +} + export interface MongoDatabases { [id: string]: Db; }