Skip to content

Commit

Permalink
Merge branch 'feature/websocket' of https://github.com/ender-null/pol…
Browse files Browse the repository at this point in the history
…aris into feature/websocket
  • Loading branch information
ender-null committed May 7, 2024
2 parents 7433632 + 5fc7388 commit 934535d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
26 changes: 15 additions & 11 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -453,15 +453,19 @@ export class Bot {
async sendBroadcast(json: WSBroadcast): Promise<void> {
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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down Expand Up @@ -31,6 +31,7 @@ process.on('exit', () => {
});

export const bots: BotSocket = {};
export const configs: BotConfig = {};
export const db: MongoDatabases = {};

const start = () => {
Expand Down Expand Up @@ -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}'`,
Expand Down
25 changes: 15 additions & 10 deletions src/plugins/world-of-warcraft.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,8 +17,6 @@ import {
sendRequest,
setTag,
} from '../utils';
import { Bot } from '../bot';
import { Message } from '../types';

export class WorldOfWarcraftPlugin extends PluginBase {
accessToken: string;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -228,21 +228,26 @@ 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' : ''}${
mythicScore ? mythicScore + '\n\n' : ''
}${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) {
Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -180,6 +180,10 @@ export interface BotSocket {
[id: string]: WebSocket;
}

export interface BotConfig {
[id: string]: Config;
}

export interface MongoDatabases {
[id: string]: Db;
}
Expand Down

0 comments on commit 934535d

Please sign in to comment.