-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
48 lines (39 loc) · 1.62 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Logger } from '@log4js-node/log4js-api';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { BotProxy } from './bot-proxy.interface';
import { MessageContext } from './message-context.interface';
let mBot: BotProxy;
let logger: Logger;
let metadata: { [key: string]: string };
let infoText: string;
let periodicGuideText: string;
export const init = async (bot: BotProxy, options: { [key: string]: any }): Promise<void> => {
mBot = bot;
logger = options.logger || console;
metadata = await import(path.resolve(__dirname, 'package.json'));
try {
infoText = (await promisify(fs.readFile)(path.resolve(__dirname, 'text', 'info.txt'), {encoding: 'utf-8'})).trim();
periodicGuideText = (await promisify(fs.readFile)(path.resolve(__dirname, 'text', 'periodic-guide.txt'),
{encoding: 'utf-8'})).trim();
} catch (e) {
logger.warn('Could not load guide text.');
throw e;
}
logger.info(`${metadata.name} plugin v${metadata.version} has been initialized.`);
};
export const onStart = () => {
logger.debug('onStart()');
};
export const onStop = () => {
logger.debug('onStop()');
};
export const onMessage = async (message: string, context: MessageContext, data: { [key: string]: any }) => {
await mBot.sendTalk(context.channelId, infoText);
};
export const onPluginEvent = async (eventName: string, value?: any, fromId?: string) => {
if (eventName === 'scheduled:periodic-guide') {
await mBot.sendTalk(await mBot.getChannelId(process.env.REC0_ENV_PERIODIC_GUIDE_CHANNEL || 'random'), periodicGuideText);
}
};