eduardo-olivares1 / Narwhal

A discord bot designed to alert users of important cryptocurrency movements and events.
MIT License
2 stars 1 forks source link

Create service for DiscordClient called DiscordClientService #19

Closed fredtsun closed 3 years ago

fredtsun commented 3 years ago

Since $bind is now in place (woohoo 🙌 ), areas in the code now require importing cache and getting the bounded channel (and sometimes prefix) in order to send a message to the desired channel. I think this should get abstracted out into a service!

Ideally this probably wants to look like: (in services/discord_client.service.js)

// maybe there are imports here
class DiscordClientService{
   constructor(...) {
      this.cache = cache
      this.discordClient = discordClient
   }
  // maybe some other functions here....
  sendMessage(msg){
     const boundChannel = this.cache.get('BIND', 'value');
     this.discordClient.channels.cache.get(boundChannel).send(msg)
   }
  // and some other fns here...
}

That way, in commands/* and jobs/* we can simply use it by saying:

class SomeJobOrCommand{
   constructor(...) {
      this.discordService = discordClientService
   }
   // Stuff Here....
   //...
   sayHi() {
       this.discordService.sendMessage('Hi!');
   }
  // ...
}

It'll help abstract out the implementation details of how a message should be sent out (which is nice because maybe it's something that wants to be different later on!) And it'll help with redundency - when a new job/service wants to send a message to channel, this.discordService.sendMessage is all that is needed now.

fredtsun commented 3 years ago

addressed in #21