namespaceDiscordParser{/*...*/exporttypeConstructorPattern=ImplementableApi.Config&{keyWord: string;channels:{ChannelYtb: TextChannel;ChannelPeerTube: TextChannel;};client: Client;};}/*...*/classDiscordParserextendsImplementableApi{readonlykeyWord: string;readonlychannels:{[key: string]:TextChannel;ChannelYtb: TextChannel;ChannelPeerTube: TextChannel;};readonlyclient: Client;constructor(readonlyconfig: DiscordParser.ConstructorPattern){super(config);this.keyWord=config.keyWord;this.channels=config.channels;this.client=config.client;this.settingEvents();}staticasyncinstanciate(config: DiscordParser.Config):Promise<DiscordParser.ConstructorPattern>{constclient=newClient();client.login(config.token);awaiteventsOnce(client,'ready');constchannels: 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,};}privatestaticgetTextChannel(client: Client,id: string):TextChannel{constchannel=client.channels.resolve(id);if(!channel||!(channelinstanceofTextChannel))throw'Bad token or bad channel id. Be careful, the channel must be a TextChannel';returnchannel;}/*...*/};
The problem with this is for the usage inside the router.
I need to make a thing like this :
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 :
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.
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.
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.
namespaceDiscordParser{/*...*/exporttypeConstructorPattern=ImplementableApi.Config&{keyWord: string;channels:{ChannelYtb: TextChannel;ChannelPeerTube: TextChannel;};client: Client;};}/*...*/classDiscordParserextendsImplementableApi{readonlykeyWord: string;readonlychannels:{[key: string]:TextChannel;ChannelYtb: TextChannel;ChannelPeerTube: TextChannel;};readonlyclient: Client;constructor(readonlyconfig: DiscordParser.ConstructorPattern){super(config);this.keyWord=config.keyWord;this.channels=config.channels;this.client=config.client;this.settingEvents();}staticasyncinstanciate(config: DiscordParser.Config):Promise<DiscordParser.ConstructorPattern>{constclient=newClient();client.login(config.token);awaiteventsOnce(client,'ready');constchannels: 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,};}privatestaticgetTextChannel(client: Client,id: string):TextChannel{constchannel=client.channels.resolve(id);if(!channel||!(channelinstanceofTextChannel))throw'Bad token or bad channel id. Be careful, the channel must be a TextChannel';returnchannel;}/*...*/};
for the discordParser.ts file
And i custom the Router Class by adding the injectDepency(..) (Router.ts:34) method.
Now, we're needing to use the router like this :
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) ) {
```
Un commentaire ici, c'est compenser à mon sens un problème sémantique dans le code.
J'aurais tendance à faire deux fonctions privées this.authenticate() et this.upload()
Un commentaire ici, c'est compenser à mon sens un problème sémantique dans le code.
J'aurais tendance à faire deux fonctions privées `this.authenticate()` et `this.upload()`
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
@@ -0,0 +38,4 @@this.settingEvents();});}I made a thing like this
The problem with this is for the usage inside the router.
I need to make a thing like this :
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 :
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.
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.
@@ -0,0 +75,4 @@return {ChannelPeerTube: resp.ChannelPeerTube,ChannelYtb: resp.ChannelYtb,};So i keep a thing like this :
for the discordParser.ts file
And i custom the Router Class by adding the injectDepency(..) (Router.ts:34) method.
Now, we're needing to use the router like this :
Encore quelques changements et go pour merger !
@@ -0,0 +32,4 @@class DiscordParser extends ImplementableApi {readonly botPrefix: string;readonly channels: {[key: string]: TextChannel;Toujours utile ?
@@ -0,0 +34,4 @@readonly channels: {[key: string]: TextChannel;ChannelYtb: TextChannel;ChannelPeerTube: TextChannel;youtubeChannel/peertubeChannelcomme nom@@ -0,0 +109,4 @@id_arr.push(resolvedChannel[key].id);if (this.channels)if (id_arr.includes(message.channel.id)) {Je comprends pas la partie de ce code, il répond à quel cas d'utilisation ?
Sinon :
@@ -0,0 +108,4 @@}const { access_token } = await response.json();// UploadUn commentaire ici, c'est compenser à mon sens un problème sémantique dans le code.
J'aurais tendance à faire deux fonctions privées
this.authenticate()etthis.upload()View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.