basic-implementation #1
3798
package-lock.json
generated
|
@ -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;
|
||||
florent
commented
`youtubeChannel` / `peertubeChannel` comme nom
|
||||
}>;
|
||||
};
|
||||
readonly client: Client;
|
||||
|
||||
constructor(readonly config: DiscordParser.Config) {
|
||||
constructor(readonly config: DiscordParser.ConstructorPattern) {
|
||||
florent
commented
```ts
type Channels = {
channelYtb: TextChannel;
channelPeerTube: TextChannel;
};
```
```ts
static async instanciate(readonly config: DiscordParser.Config) {
const client = new Client();
client.login(this.token);
await eventsOnce(client, 'ready');
const channels = {
channelYtb: this.client.channels.resolve("..."),
channelPeerTube: ...
};
return new Discord({
client,
channels,
keyword
});
```
amaury.joly
commented
I made a thing like this
The problem with this is for the usage inside the router.
So I have again an async function inside my constructor, and to solved this I need to create the same pattern inside the router class. We could try maybe an events who's called at the the end of the constructor. (I took this idea from DiscordJS)
With this, the restriction is to call the event 'ready' at the end of the constrctor's tasks. It's looking more friendly than the Dependency Injection for a beginner who would to make his own ImplementableAPI. I made a thing like this
```ts
namespace DiscordParser {
/*...*/
export type ConstructorPattern = ImplementableApi.Config & {
keyWord: string;
channels: {
ChannelYtb: TextChannel;
ChannelPeerTube: TextChannel;
};
client: Client;
};
}
/*...*/
class DiscordParser extends ImplementableApi {
readonly keyWord: string;
readonly channels: {
[key: string]: TextChannel;
ChannelYtb: TextChannel;
ChannelPeerTube: TextChannel;
};
readonly client: Client;
constructor(readonly config: DiscordParser.ConstructorPattern) {
super(config);
this.keyWord = config.keyWord;
this.channels = config.channels;
this.client = config.client;
this.settingEvents();
}
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
),
};
return {
name: config.name,
channels: channels,
client: client,
keyWord: config.keyWord,
};
}
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;
}
/*...*/
};
```
The problem with this is for the usage inside the router.
I need to make a thing like this :
```ts
class Router {
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(
await DiscordParser.instanciate(config.discord)
);
this.api_array[config.peertubeRequester.name] = new PeerTubeRequester(
config.peertubeRequester
);
this.api_array[config.logWriter.name] = new LogWriter(config.logWriter);
}
/*...*/
}
```
So I have again an async function inside my constructor, and to solved this I need to create the same pattern inside the router class.
And by extension i need to generalize this for my ImplementableAPI class. It's included to force all the users of the ImplementableAPI to make an Dependence Injection Pattern for their API. And it's look a little bit restrictive to me.
We could try maybe an events who's called at the the end of the constructor. (I took this idea from DiscordJS)
Like that we could imagine a thing like this :
```ts
class MyClass extends ImplementableAPI {
constructor(...) {
super(...);
const promise = startAnAsynchronousTask(...);
doSomeSynchronousTask(...);
promise.then(() = > {
this.emit('ready');
});
}
private startAnAsynchronousTask(...) : Promise<void> {
await something(..);
await somethingElse();
await events.once(anObject, 'ready');
}
/*...*/
}
```
With this, the restriction is to call the event 'ready' at the end of the constrctor's tasks. It's looking more friendly than the Dependency Injection for a beginner who would to make his own ImplementableAPI.
amaury.joly
commented
Ok, my bad. I think I missunderstanded the Dependency Injection. Sleep on it. Ok, my bad. I think I missunderstanded the Dependency Injection. Sleep on it.
I'm going to try another thing.
I keep my DiscordParser Class with the instantiate method. And I 'm going to insert the created object inside an method in Router who could be named 'inject' or something in this idea. And who could take an ImplementableAPI instance in params.
|
||||
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;
|
||||
amaury.joly marked this conversation as resolved
Outdated
florent
commented
```ts
if (!resp.ChannelPeerTube || !resp.ChannelYtb) {
throw new Error('Theres an issue concerned the channel check');
}
return {...};
```
|
||||
}
|
||||
|
||||
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)) {
|
||||
florent
commented
Je comprends pas la partie de ce code, il répond à quel cas d'utilisation ? Sinon :
Je comprends pas la partie de ce code, il répond à quel cas d'utilisation ?
Sinon :
```ts
if ( Object.values(this.channels).some(channel => channel.id === 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,9 +1,9 @@
|
|||
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
|
||||
|
@ -12,9 +12,9 @@ namespace ImplementableApi {
|
|||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Toujours utile ?