Add test logWriter + more things inside my other tests
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
import { Client, TextChannel } from 'discord.js';
|
||||
import { Client, Message, TextChannel } from 'discord.js';
|
||||
import dedent from 'ts-dedent';
|
||||
import { ImplementableApi } from './implementableApi';
|
||||
|
||||
import { once as eventsOnce } from 'events';
|
||||
|
||||
namespace DiscordParser {
|
||||
export type Config = ImplementableApi.Config & {
|
||||
token: string;
|
||||
@ -13,23 +15,28 @@ namespace DiscordParser {
|
||||
};
|
||||
}
|
||||
|
||||
///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 channels: Promise<{
|
||||
[key: string]: TextChannel;
|
||||
ChannelYtb: TextChannel;
|
||||
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);
|
||||
this.channels = eventsOnce(this.client, 'ready').then(() => {
|
||||
return this.setChannel(config.channelsId);
|
||||
});
|
||||
this.channels.then(() => {
|
||||
this.settingEvents();
|
||||
});
|
||||
}
|
||||
|
||||
private setChannel(ids: {
|
||||
@ -46,12 +53,14 @@ class DiscordParser extends ImplementableApi {
|
||||
ChannelPeerTube?: TextChannel;
|
||||
} = {};
|
||||
// construct and check the channels
|
||||
|
||||
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] = tmpChannel;
|
||||
resp[key.slice(2)] = tmpChannel;
|
||||
else throw new Error('The channel must be a TextChannel');
|
||||
else
|
||||
throw new Error(
|
||||
@ -67,6 +76,10 @@ class DiscordParser extends ImplementableApi {
|
||||
ChannelPeerTube: resp.ChannelPeerTube,
|
||||
ChannelYtb: resp.ChannelYtb,
|
||||
};
|
||||
|
||||
console.log(resp);
|
||||
// console.log(resp.ChannelYtb);
|
||||
|
||||
throw new Error('Theres an issue concerned the channel check');
|
||||
}
|
||||
|
||||
@ -80,33 +93,51 @@ class DiscordParser extends ImplementableApi {
|
||||
switch (message.type) {
|
||||
case 'newEntriesNotify':
|
||||
this.sendMsgYtb(
|
||||
`New YouTubes entries received :\n${JSON.parse(
|
||||
message.rawContent
|
||||
).items.map((item: any) => `Author : ${item.author}`)}`
|
||||
`New YouTubes entries received :\n${message.rawContent.items.map(
|
||||
(item: any) =>
|
||||
`Author : ${item.author}\nTitle: ${item.title}\nlink: ${item.link}`
|
||||
)}`
|
||||
);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private sendMsgYtb(message: string) {
|
||||
this.channels.ChannelYtb.send(message);
|
||||
private async sendMsgYtb(message: string) {
|
||||
const resolvedChannel = await this.channels;
|
||||
resolvedChannel.ChannelYtb.send(message);
|
||||
}
|
||||
private sendMsgPeerTube(message: string) {
|
||||
this.channels.ChannelPeerTube.send(message);
|
||||
private async sendMsgPeerTube(message: string) {
|
||||
const resolvedChannel = await this.channels;
|
||||
resolvedChannel.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':
|
||||
this.on('message', async (message: Message) => {
|
||||
const resolvedChannel = await this.channels;
|
||||
let id_arr: string[] = [];
|
||||
for (const key in this.channels)
|
||||
id_arr.push(resolvedChannel[key].id);
|
||||
|
||||
if (this.channels)
|
||||
if (id_arr.includes(message.channel.id)) {
|
||||
const msg_splitted = message.content.split(' ');
|
||||
if (msg_splitted[0] === this.keyWord) {
|
||||
switch (msg_splitted[1]) {
|
||||
case 'add':
|
||||
const send_message: ImplementableApi.Message = {
|
||||
rawContent: {
|
||||
address: msg_splitted[2],
|
||||
user: message.author.toString(),
|
||||
date: message.createdAt.toUTCString(),
|
||||
},
|
||||
type: 'newListener',
|
||||
};
|
||||
message.channel.send('Ceci est un feedback');
|
||||
this.emit('addListener', send_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,9 @@ namespace LogWriter {
|
||||
path: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* check nodejs buffer et throttle
|
||||
*/
|
||||
class LogWriter extends ImplementableApi {
|
||||
readonly path: string;
|
||||
|
||||
@ -31,14 +33,15 @@ class LogWriter extends ImplementableApi {
|
||||
private writeMsg(message: ImplementableApi.Message): void;
|
||||
private writeMsg(message: ImplementableApi.Message | string) {
|
||||
if (typeof message !== 'string')
|
||||
message = `[${message.type} :: ${new Date().toLocaleString(
|
||||
'fr-FR'
|
||||
)}] ${message.rawContent} ${
|
||||
message.idListener ?? `( listener_id : ${message.idListener} )`
|
||||
message = `[${message.type} :: ${new Date().toISOString()}] ${
|
||||
message.rawContent
|
||||
} ${
|
||||
message.idListener ??
|
||||
`( listener_id : ${message.idListener} )\n`
|
||||
}`;
|
||||
|
||||
const fd = openSync(this.path, 'a');
|
||||
const str = `[${new Date().toLocaleString('fr-FR')}] ${message}`;
|
||||
const str = `[${new Date().toISOString()}] ${message}\n`;
|
||||
appendFileSync(fd, str);
|
||||
closeSync(fd);
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ namespace PeerTubeRequester {
|
||||
|
||||
type UploadInstruction = {
|
||||
[key: string]: string;
|
||||
channelID: string;
|
||||
channelId: string;
|
||||
targetUrl: string;
|
||||
};
|
||||
|
||||
@ -68,7 +68,7 @@ class PeerTubeRequester extends ImplementableApi {
|
||||
for (const item of items) {
|
||||
const media_group = item['media:group'];
|
||||
const args: UploadInstruction = {
|
||||
channelID: 'undefined', // to do binding avec les idDeChaines Skeptikom
|
||||
channelId: '848', // to do binding avec les idDeChaines Skeptikom
|
||||
targetUrl: item.link,
|
||||
};
|
||||
await this.apiRequest(args);
|
||||
|
@ -1,43 +1,47 @@
|
||||
import { ImplementableApi } from "./implementableApi";
|
||||
import { ImplementableApi } from './implementableApi';
|
||||
|
||||
import { DiscordParser } from './discordParser'
|
||||
import { LogWriter } from './logWriter'
|
||||
import { PeerTubeRequester } from './peertubeRequester'
|
||||
import { DiscordParser } from './discordParser';
|
||||
import { LogWriter } from './logWriter';
|
||||
import { PeerTubeRequester } from './peertubeRequester';
|
||||
|
||||
namespace Router {
|
||||
export type Config = {
|
||||
events: {
|
||||
name: string,
|
||||
type: 0 | 1
|
||||
name: string;
|
||||
type: 'emit' | 'received';
|
||||
}[];
|
||||
apis: {
|
||||
apiName: string
|
||||
}[]
|
||||
}
|
||||
apiName: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
export type GlobalConfig = {
|
||||
router: Config;
|
||||
discord: DiscordParser.Config;
|
||||
peertubeRequester: PeerTubeRequester.Config;
|
||||
logWriter: LogWriter.Config;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
class Router {
|
||||
api_array: {[key: string]: ImplementableApi} = {};
|
||||
api_array: { [key: string]: ImplementableApi } = {};
|
||||
readonly routes: Router.Config;
|
||||
|
||||
constructor(readonly config: Router.GlobalConfig) {
|
||||
this.routes = config.router;
|
||||
|
||||
this.api_array[config.discord.name] = new DiscordParser(config.discord);
|
||||
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester(config.peertubeRequester);
|
||||
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester(
|
||||
config.peertubeRequester
|
||||
);
|
||||
this.api_array[config.logWriter.name] = new LogWriter(config.logWriter);
|
||||
}
|
||||
|
||||
public receivedMessage(message: ImplementableApi.Message) {
|
||||
this.routes.apis.forEach((api) => this.api_array[api.apiName].receivedMessage(message))
|
||||
this.routes.apis.forEach((api) =>
|
||||
this.api_array[api.apiName].receivedMessage(message)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export { Router };
|
||||
export { Router };
|
||||
|
Reference in New Issue
Block a user