Skip to content

Commit

Permalink
add redirect functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ender-null committed May 7, 2024
1 parent 2279648 commit a2f3898
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
63 changes: 33 additions & 30 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { bots, db, wss } from './main';
import { PluginBase } from './plugin';
import * as plugins from './plugins/index';
import {
Expand Down Expand Up @@ -455,7 +455,7 @@ export class Bot {
const conversation = broadcast.message.conversation;
let config = this.config;
if (!Array.isArray(broadcast.target)) {
config = configs[broadcast.target];
config = bots[broadcast.target].config;
}
if (conversation.id === 'alerts') {
conversation.id = config.alertsConversationId;
Expand Down Expand Up @@ -488,48 +488,51 @@ export class Bot {
this.broadcastHandler(broadcast.message);
broadcast.target.forEach((target) => {
if (bots[target]) {
bots[target].send(JSON.stringify(message));
if (json.type === 'broadcast') {
bots[target].websocket.send(JSON.stringify(message));
} else if (json.type === 'redirect') {
bots[target].onMessageReceive(message.message);
}
}
});
} else if (broadcast.target === '*' || broadcast.target === 'all') {
wss.clients.forEach((client) => {
this.broadcastHandler(broadcast.message);
client.send(JSON.stringify(message));
if (json.type === 'broadcast') {
client.send(JSON.stringify(message));
}
});
} else {
this.broadcastHandler(broadcast.message);
if (bots[broadcast.target]) {
bots[broadcast.target].send(JSON.stringify(message));
if (json.type === 'broadcast') {
bots[broadcast.target].websocket.send(JSON.stringify(message));
} else if (json.type === 'redirect') {
bots[broadcast.target].onMessageReceive(message.message);
}
}
}
}

sendAlert(text: string, language = 'javascript'): void {
if (
this.config.alertsConversationId &&
!(text.includes(this.config.alertsConversationId) || text.includes('Chat not found'))
) {
const message = new Message(
null,
new Conversation('alerts'),
this.user,
`${this.user.firstName} (@${this.user.username}) [${this.user.id}]\n<code class="language-${language}">${text}</code>`,
'text',
null,
null,
{ format: 'HTML', preview: false },
);
const broadcast: WSBroadcast = {
bot: this.config.name,
target: this.config.alertsTarget,
platform: this.config.alertsPlatform,
type: 'broadcast',
message,
};

//this.send(message);
this.sendBroadcast(broadcast).then();
}
const message = new Message(
null,
new Conversation('alerts'),
this.user,
`${this.user.firstName} (@${this.user.username}) [<pre>${this.user.id}</pre>]\n<code class="language-${language}">${text}</code>`,
'text',
null,
null,
{ format: 'HTML', preview: false },
);
const broadcast: WSBroadcast = {
bot: this.config.name,
target: this.config.alertsTarget,
platform: this.config.alertsPlatform,
type: 'broadcast',
message,
};
this.sendBroadcast(broadcast).then();
}

sendAdminAlert(text: string): void {
Expand Down
8 changes: 3 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MongoClient } from 'mongodb';
import { WebSocket, WebSocketServer } from 'ws';
import { Bot } from './bot';
import { BotConfig, BotSocket, MongoDatabases, WSInit, WSMessage, WSPong } from './types';
import { BotSet, MongoDatabases, WSInit, WSMessage, WSPong } from './types';
import { catchException, logger } from './utils';

let mongo: MongoClient;
Expand Down Expand Up @@ -30,8 +30,7 @@ process.on('exit', () => {
logger.info('❎ Exit process');
});

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

const start = () => {
Expand Down Expand Up @@ -64,8 +63,7 @@ const start = () => {
db[init.platform] = mongo.db(init.platform);
}
bot.initPlugins();
bots[bot.user.id] = ws;
configs[bot.user.id] = init.config;
bots[bot.user.id] = bot;
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
12 changes: 4 additions & 8 deletions 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 { Db } from 'mongodb';
import { WebSocket } from 'ws';
import { Bot } from './bot';
import { Config } from './config';

export class ErrorMessages {
Expand Down Expand Up @@ -176,12 +176,8 @@ export class Message {
}
}

export interface BotSocket {
[id: string]: WebSocket;
}

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

export interface MongoDatabases {
Expand Down Expand Up @@ -277,7 +273,7 @@ export interface WSPong extends WSData {
}

export interface WSBroadcast extends WSData {
type: 'broadcast';
type: 'broadcast' | 'redirect';
target: string | string[];
message: BroadcastMessage;
}
Expand Down

0 comments on commit a2f3898

Please sign in to comment.