basic-implementation #1

Open
florent wants to merge 8 commits from basic-implementation into master
4 changed files with 96 additions and 3884 deletions
Showing only changes of commit d1f1165136 - Show all commits

3798
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,80 +13,69 @@ namespace DiscordParser {
}; };
keyWord: string; 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 { class DiscordParser extends ImplementableApi {
readonly token: string; readonly botPrefix: string;
readonly keyWord: string; readonly channels: {
readonly channels: Promise<{
[key: string]: TextChannel; [key: string]: TextChannel;
ChannelYtb: TextChannel; ChannelYtb: TextChannel;
ChannelPeerTube: TextChannel; ChannelPeerTube: TextChannel;
}>; };
readonly client: Client; readonly client: Client;
constructor(readonly config: DiscordParser.Config) { constructor(readonly config: DiscordParser.ConstructorPattern) {
super(config); super(config);
this.token = config.token; this.botPrefix = config.botPrefix;
this.keyWord = config.keyWord; this.channels = config.channels;
this.client = config.client;
this.client = this.instantiateClient(); this.settingEvents();
this.channels = eventsOnce(this.client, 'ready').then(() => {
return this.setChannel(config.channelsId);
});
this.channels.then(() => {
this.settingEvents();
});
} }
private setChannel(ids: { static async instanciate(
[key: string]: string; config: DiscordParser.Config
idChannelYtb: string; ): Promise<DiscordParser.ConstructorPattern> {
idChannelPeerTube: string; const client = new Client();
}): { client.login(config.token);
ChannelYtb: TextChannel; await eventsOnce(client, 'ready');
ChannelPeerTube: TextChannel; const channels: ChannelsType = {
} { ChannelPeerTube: this.getTextChannel(
let resp: { client,
[key: string]: TextChannel | undefined; config.channelsId.idChannelPeerTube
ChannelYtb?: TextChannel; ),
ChannelPeerTube?: TextChannel; ChannelYtb: this.getTextChannel(
} = {}; client,
// construct and check the channels config.channelsId.idChannelYtb
),
};
for (const key in ids) { return {
if (ids[key]) { name: config.name,
// console.log(ids[key]); channels: channels,
const tmpChannel = this.client.channels.resolve(ids[key]); client: client,
if (tmpChannel) botPrefix: config.keyWord,
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');
} }
private instantiateClient(): Client { private static getTextChannel(client: Client, id: string): TextChannel {
const newClient = new Client(); const channel = client.channels.resolve(id);
newClient.login(this.token); if (!channel || !(channel instanceof TextChannel))
return newClient; throw 'Bad token or bad channel id. Be careful, the channel must be a TextChannel';
return channel;
} }
public receivedMessage(message: ImplementableApi.Message) { public receivedMessage(message: ImplementableApi.Message) {
@ -113,8 +102,8 @@ class DiscordParser extends ImplementableApi {
} }
private settingEvents(): void { private settingEvents(): void {
this.on('message', async (message: Message) => { this.client.on('message', (message: Message) => {
const resolvedChannel = await this.channels; const resolvedChannel = this.channels;
let id_arr: string[] = []; let id_arr: string[] = [];
for (const key in this.channels) for (const key in this.channels)
id_arr.push(resolvedChannel[key].id); id_arr.push(resolvedChannel[key].id);
@ -122,7 +111,7 @@ class DiscordParser extends ImplementableApi {
if (this.channels) if (this.channels)
if (id_arr.includes(message.channel.id)) { if (id_arr.includes(message.channel.id)) {
const msg_splitted = message.content.split(' '); const msg_splitted = message.content.split(' ');
if (msg_splitted[0] === this.keyWord) { if (msg_splitted[0] === this.botPrefix) {
switch (msg_splitted[1]) { switch (msg_splitted[1]) {
case 'add': case 'add':
const send_message: ImplementableApi.Message = { const send_message: ImplementableApi.Message = {

View File

@ -1,9 +1,9 @@
import EventEmitter from "events"; import EventEmitter from 'events';
namespace ImplementableApi { namespace ImplementableApi {
export type Config = { export type Config = {
name: string; name: string;
} };
/** /**
* [optional] idListener is the way to identificate the listener who's the src of the newEntries * [optional] idListener is the way to identificate the listener who's the src of the newEntries
@ -12,9 +12,9 @@ namespace ImplementableApi {
*/ */
export type Message = { export type Message = {
idListener?: number; idListener?: number;
type: "newEntriesNotify" | "newListener"; type: 'newEntriesNotify' | 'newListener';
rawContent: any; rawContent: any;
} };
} }
abstract class ImplementableApi extends EventEmitter { abstract class ImplementableApi extends EventEmitter {
@ -28,4 +28,4 @@ abstract class ImplementableApi extends EventEmitter {
public abstract receivedMessage(message: ImplementableApi.Message): void; public abstract receivedMessage(message: ImplementableApi.Message): void;
} }
export { ImplementableApi } export { ImplementableApi };

View File

@ -3,44 +3,53 @@ import { ImplementableApi } from './implementableApi';
import { DiscordParser } from './discordParser'; import { DiscordParser } from './discordParser';
import { LogWriter } from './logWriter'; import { LogWriter } from './logWriter';
import { PeerTubeRequester } from './peertubeRequester'; import { PeerTubeRequester } from './peertubeRequester';
import EventEmitter from 'events';
namespace Router { namespace Router {
export type Config = { export type EventsType = {
events: { name: string;
name: string; type: 'emit' | 'received';
type: 'emit' | 'received'; };
}[]; export type InternalConfig = {
events: EventsType[];
apis: { apis: {
apiName: string; apiName: string;
}[]; }[];
}; };
export type GlobalConfig = { // export type GlobalConfig = {
router: Config; // router: InternalConfig;
discord: DiscordParser.Config; // };
peertubeRequester: PeerTubeRequester.Config;
logWriter: LogWriter.Config;
};
} }
class Router { class Router extends EventEmitter {
api_array: { [key: string]: ImplementableApi } = {}; api_array: { [key: string]: ImplementableApi } = {};
readonly routes: Router.Config; routes: Router.InternalConfig;
constructor(readonly config: Router.GlobalConfig) { constructor(readonly config: Router.EventsType[]) {
this.routes = config.router; super();
this.routes = { events: config, apis: [] };
}
this.api_array[config.discord.name] = new DiscordParser(config.discord); public injectDependency(api: ImplementableApi): void {
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester( if (api.name in this.api_array)
config.peertubeRequester throw `The api name '${api.name}' is already take`;
); this.routes.apis.push({ apiName: api.name });
this.api_array[config.logWriter.name] = new LogWriter(config.logWriter); 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) { public receivedMessage(message: ImplementableApi.Message) {
this.routes.apis.forEach((api) => for (const apiName in this.routes.apis)
this.api_array[api.apiName].receivedMessage(message) this.api_array[apiName].receivedMessage(message);
);
} }
} }