Dicord parser used an Depency Injection Model and Router was adapted to be compatible with this
This commit is contained in:
parent
7fc2e29f0e
commit
d1f1165136
3798
package-lock.json
generated
3798
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -13,80 +13,69 @@ namespace DiscordParser {
|
|||
};
|
||||
keyWord: string;
|
||||
};
|
||||
|
||||
export type ConstructorPattern = ImplementableApi.Config & {
|
||||
botPrefix: string;
|
||||
channels: {
|
||||
ChannelYtb: TextChannel;
|
||||
ChannelPeerTube: TextChannel;
|
||||
};
|
||||
client: Client;
|
||||
};
|
||||
}
|
||||
|
||||
type ChannelsType = {
|
||||
ChannelYtb: TextChannel;
|
||||
ChannelPeerTube: TextChannel;
|
||||
};
|
||||
|
||||
class DiscordParser extends ImplementableApi {
|
||||
readonly token: string;
|
||||
readonly keyWord: string;
|
||||
readonly channels: Promise<{
|
||||
readonly botPrefix: string;
|
||||
readonly channels: {
|
||||
[key: string]: TextChannel;
|
||||
ChannelYtb: TextChannel;
|
||||
ChannelPeerTube: TextChannel;
|
||||
}>;
|
||||
};
|
||||
readonly client: Client;
|
||||
|
||||
constructor(readonly config: DiscordParser.Config) {
|
||||
constructor(readonly config: DiscordParser.ConstructorPattern) {
|
||||
super(config);
|
||||
this.token = config.token;
|
||||
this.keyWord = config.keyWord;
|
||||
|
||||
this.client = this.instantiateClient();
|
||||
this.channels = eventsOnce(this.client, 'ready').then(() => {
|
||||
return this.setChannel(config.channelsId);
|
||||
});
|
||||
this.channels.then(() => {
|
||||
this.settingEvents();
|
||||
});
|
||||
this.botPrefix = config.botPrefix;
|
||||
this.channels = config.channels;
|
||||
this.client = config.client;
|
||||
this.settingEvents();
|
||||
}
|
||||
|
||||
private setChannel(ids: {
|
||||
[key: string]: string;
|
||||
idChannelYtb: string;
|
||||
idChannelPeerTube: string;
|
||||
}): {
|
||||
ChannelYtb: TextChannel;
|
||||
ChannelPeerTube: TextChannel;
|
||||
} {
|
||||
let resp: {
|
||||
[key: string]: TextChannel | undefined;
|
||||
ChannelYtb?: TextChannel;
|
||||
ChannelPeerTube?: TextChannel;
|
||||
} = {};
|
||||
// construct and check the channels
|
||||
static async instanciate(
|
||||
config: DiscordParser.Config
|
||||
): Promise<DiscordParser.ConstructorPattern> {
|
||||
const client = new Client();
|
||||
client.login(config.token);
|
||||
await eventsOnce(client, 'ready');
|
||||
const channels: ChannelsType = {
|
||||
ChannelPeerTube: this.getTextChannel(
|
||||
client,
|
||||
config.channelsId.idChannelPeerTube
|
||||
),
|
||||
ChannelYtb: this.getTextChannel(
|
||||
client,
|
||||
config.channelsId.idChannelYtb
|
||||
),
|
||||
};
|
||||
|
||||
for (const key in ids) {
|
||||
if (ids[key]) {
|
||||
// console.log(ids[key]);
|
||||
const tmpChannel = this.client.channels.resolve(ids[key]);
|
||||
if (tmpChannel)
|
||||
if (tmpChannel instanceof TextChannel)
|
||||
resp[key.slice(2)] = tmpChannel;
|
||||
else throw new Error('The channel must be a TextChannel');
|
||||
else
|
||||
throw new Error(
|
||||
dedent`The channel cannot be found by the bot.
|
||||
Check if you had the bot to your server or if the channel ID is correct`
|
||||
);
|
||||
}
|
||||
}
|
||||
// return the well formed object
|
||||
if (resp.ChannelPeerTube)
|
||||
if (resp.ChannelYtb)
|
||||
return {
|
||||
ChannelPeerTube: resp.ChannelPeerTube,
|
||||
ChannelYtb: resp.ChannelYtb,
|
||||
};
|
||||
|
||||
console.log(resp);
|
||||
// console.log(resp.ChannelYtb);
|
||||
|
||||
throw new Error('Theres an issue concerned the channel check');
|
||||
return {
|
||||
name: config.name,
|
||||
channels: channels,
|
||||
client: client,
|
||||
botPrefix: config.keyWord,
|
||||
};
|
||||
}
|
||||
|
||||
private instantiateClient(): Client {
|
||||
const newClient = new Client();
|
||||
newClient.login(this.token);
|
||||
return newClient;
|
||||
private static getTextChannel(client: Client, id: string): TextChannel {
|
||||
const channel = client.channels.resolve(id);
|
||||
if (!channel || !(channel instanceof TextChannel))
|
||||
throw 'Bad token or bad channel id. Be careful, the channel must be a TextChannel';
|
||||
return channel;
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
|
@ -113,8 +102,8 @@ class DiscordParser extends ImplementableApi {
|
|||
}
|
||||
|
||||
private settingEvents(): void {
|
||||
this.on('message', async (message: Message) => {
|
||||
const resolvedChannel = await this.channels;
|
||||
this.client.on('message', (message: Message) => {
|
||||
const resolvedChannel = this.channels;
|
||||
let id_arr: string[] = [];
|
||||
for (const key in this.channels)
|
||||
id_arr.push(resolvedChannel[key].id);
|
||||
|
@ -122,7 +111,7 @@ class DiscordParser extends ImplementableApi {
|
|||
if (this.channels)
|
||||
if (id_arr.includes(message.channel.id)) {
|
||||
const msg_splitted = message.content.split(' ');
|
||||
if (msg_splitted[0] === this.keyWord) {
|
||||
if (msg_splitted[0] === this.botPrefix) {
|
||||
switch (msg_splitted[1]) {
|
||||
case 'add':
|
||||
const send_message: ImplementableApi.Message = {
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
import EventEmitter from "events";
|
||||
import EventEmitter from 'events';
|
||||
|
||||
namespace ImplementableApi {
|
||||
export type Config = {
|
||||
name: string;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* [optional] idListener is the way to identificate the listener who's the src of the newEntries
|
||||
* the type field permit to know which type of message it is
|
||||
* rawContent field is the string content of the message
|
||||
* rawContent field is the string content of the message
|
||||
*/
|
||||
export type Message = {
|
||||
idListener?: number;
|
||||
type: "newEntriesNotify" | "newListener";
|
||||
type: 'newEntriesNotify' | 'newListener';
|
||||
rawContent: any;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
abstract class ImplementableApi extends EventEmitter {
|
||||
|
@ -28,4 +28,4 @@ abstract class ImplementableApi extends EventEmitter {
|
|||
public abstract receivedMessage(message: ImplementableApi.Message): void;
|
||||
}
|
||||
|
||||
export { ImplementableApi }
|
||||
export { ImplementableApi };
|
||||
|
|
|
@ -3,44 +3,53 @@ import { ImplementableApi } from './implementableApi';
|
|||
import { DiscordParser } from './discordParser';
|
||||
import { LogWriter } from './logWriter';
|
||||
import { PeerTubeRequester } from './peertubeRequester';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
namespace Router {
|
||||
export type Config = {
|
||||
events: {
|
||||
name: string;
|
||||
type: 'emit' | 'received';
|
||||
}[];
|
||||
export type EventsType = {
|
||||
name: string;
|
||||
type: 'emit' | 'received';
|
||||
};
|
||||
export type InternalConfig = {
|
||||
events: EventsType[];
|
||||
apis: {
|
||||
apiName: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
export type GlobalConfig = {
|
||||
router: Config;
|
||||
discord: DiscordParser.Config;
|
||||
peertubeRequester: PeerTubeRequester.Config;
|
||||
logWriter: LogWriter.Config;
|
||||
};
|
||||
// export type GlobalConfig = {
|
||||
// router: InternalConfig;
|
||||
// };
|
||||
}
|
||||
|
||||
class Router {
|
||||
class Router extends EventEmitter {
|
||||
api_array: { [key: string]: ImplementableApi } = {};
|
||||
readonly routes: Router.Config;
|
||||
routes: Router.InternalConfig;
|
||||
|
||||
constructor(readonly config: Router.GlobalConfig) {
|
||||
this.routes = config.router;
|
||||
constructor(readonly config: Router.EventsType[]) {
|
||||
super();
|
||||
this.routes = { events: config, apis: [] };
|
||||
}
|
||||
|
||||
this.api_array[config.discord.name] = new DiscordParser(config.discord);
|
||||
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester(
|
||||
config.peertubeRequester
|
||||
);
|
||||
this.api_array[config.logWriter.name] = new LogWriter(config.logWriter);
|
||||
public injectDependency(api: ImplementableApi): void {
|
||||
if (api.name in this.api_array)
|
||||
throw `The api name '${api.name}' is already take`;
|
||||
this.routes.apis.push({ apiName: api.name });
|
||||
this.api_array[api.name] = api;
|
||||
|
||||
this.setEvents(api);
|
||||
}
|
||||
|
||||
private setEvents(api: ImplementableApi) {
|
||||
for (const eventName in this.routes.events.map(
|
||||
(ev) => ev.type === 'received'
|
||||
))
|
||||
api.on(eventName, () => this.emit(eventName));
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
this.routes.apis.forEach((api) =>
|
||||
this.api_array[api.apiName].receivedMessage(message)
|
||||
);
|
||||
for (const apiName in this.routes.apis)
|
||||
this.api_array[apiName].receivedMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user