2021-05-30 15:36:20 +02:00
|
|
|
import { Client, TextChannel } from 'discord.js';
|
|
|
|
import dedent from 'ts-dedent';
|
2021-05-23 16:07:33 +02:00
|
|
|
import { ImplementableApi } from './implementableApi';
|
|
|
|
|
|
|
|
namespace DiscordParser {
|
|
|
|
export type Config = ImplementableApi.Config & {
|
|
|
|
token: string;
|
2021-05-30 15:36:20 +02:00
|
|
|
channelsId: {
|
|
|
|
idChannelYtb: string;
|
|
|
|
idChannelPeerTube: string;
|
|
|
|
};
|
|
|
|
keyWord: string;
|
2021-05-23 16:07:33 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-30 15:36:20 +02:00
|
|
|
///Penser a split mes event peertube Ytb en 2 channel differents
|
2021-05-23 16:07:33 +02:00
|
|
|
class DiscordParser extends ImplementableApi {
|
|
|
|
readonly token: string;
|
2021-05-30 15:36:20 +02:00
|
|
|
readonly keyWord: string;
|
|
|
|
readonly channels: {
|
|
|
|
readonly ChannelYtb: TextChannel;
|
|
|
|
readonly ChannelPeerTube: TextChannel;
|
|
|
|
};
|
|
|
|
readonly client: Client;
|
2021-05-23 16:07:33 +02:00
|
|
|
|
|
|
|
constructor(readonly config: DiscordParser.Config) {
|
|
|
|
super(config);
|
|
|
|
this.token = config.token;
|
2021-05-30 15:36:20 +02:00
|
|
|
this.keyWord = config.keyWord;
|
|
|
|
this.settingEvents();
|
|
|
|
this.client = this.instantiateClient();
|
|
|
|
this.channels = this.setChannel(config.channelsId);
|
2021-05-23 16:07:33 +02:00
|
|
|
}
|
|
|
|
|
2021-05-30 15:36:20 +02:00
|
|
|
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;
|
2021-05-23 16:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public receivedMessage(message: ImplementableApi.Message) {
|
|
|
|
switch (message.type) {
|
|
|
|
case 'newEntriesNotify':
|
2021-05-30 15:36:20 +02:00
|
|
|
this.sendMsgYtb(
|
|
|
|
`New YouTubes entries received :\n${JSON.parse(
|
|
|
|
message.rawContent
|
|
|
|
).items.map((item: any) => `Author : ${item.author}`)}`
|
|
|
|
);
|
2021-05-23 16:07:33 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 15:36:20 +02:00
|
|
|
private sendMsgYtb(message: string) {
|
|
|
|
this.channels.ChannelYtb.send(message);
|
|
|
|
}
|
|
|
|
private sendMsgPeerTube(message: string) {
|
|
|
|
this.channels.ChannelPeerTube.send(message);
|
2021-05-23 16:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private settingEvents(): void {
|
2021-05-30 15:36:20 +02:00
|
|
|
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':
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-05-23 16:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { DiscordParser };
|