import { Client, TextChannel } from 'discord.js'; import dedent from 'ts-dedent'; import { ImplementableApi } from './implementableApi'; namespace DiscordParser { export type Config = ImplementableApi.Config & { token: string; channelsId: { idChannelYtb: string; idChannelPeerTube: string; }; keyWord: string; }; } ///Penser a split mes event peertube Ytb en 2 channel differents class DiscordParser extends ImplementableApi { readonly token: string; readonly keyWord: string; readonly channels: { readonly ChannelYtb: TextChannel; readonly ChannelPeerTube: TextChannel; }; readonly client: Client; constructor(readonly config: DiscordParser.Config) { super(config); this.token = config.token; this.keyWord = config.keyWord; this.settingEvents(); this.client = this.instantiateClient(); this.channels = this.setChannel(config.channelsId); } 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 for (const key in ids) { if (ids[key]) { const tmpChannel = this.client.channels.resolve(ids[key]); if (tmpChannel) if (tmpChannel instanceof TextChannel) resp[key] = 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, }; throw new Error('Theres an issue concerned the channel check'); } private instantiateClient(): Client { const newClient = new Client(); newClient.login(this.token); return newClient; } public receivedMessage(message: ImplementableApi.Message) { switch (message.type) { case 'newEntriesNotify': this.sendMsgYtb( `New YouTubes entries received :\n${JSON.parse( message.rawContent ).items.map((item: any) => `Author : ${item.author}`)}` ); default: break; } } private sendMsgYtb(message: string) { this.channels.ChannelYtb.send(message); } private sendMsgPeerTube(message: string) { this.channels.ChannelPeerTube.send(message); } private settingEvents(): void { this.client.on('message', (message) => { const msg_splitted = message.content.split(' '); if (msg_splitted[0] === this.keyWord) { // if (!this.channel) { // this.channel = message.channel; // } switch (msg_splitted[1]) { case 'add': } } }); } } export { DiscordParser };