All files / src/clients discord.ts

20.83% Statements 10/48
0% Branches 0/11
0% Functions 0/12
23.8% Lines 10/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98  1x 1x 1x 1x   1x 1x 1x 1x   1x   1x                                                                                                                                                                        
import fetch from "node-fetch";
import { config } from '../config';
import { Client, MessageAttachment, MessageEmbed, TextChannel } from "discord.js";
import { Routes } from "discord-api-types/v10";
import { REST } from "@discordjs/rest";
 
const discordCommands = []
let inited = false
const callbacks: Function[] = []
const interactionsListener:any[] = [];
let client: Client;
const channels: TextChannel[] = [];
 
export default class DiscordClient {
  
  getDiscordCommands() {
    return discordCommands
  }
  
  setup: boolean;
 
  getClient():Client {
    return client
  }
 
  getInteractionsListener() {
    return interactionsListener
  }
 
  init(callback:Function=undefined) {
    Iif (!process.env.DISCORD_TOKEN) return;
    Iif (!client) {
      console.log(`new discord client`)
      client = new Client({ intents: ['GUILD_MESSAGE_REACTIONS', 'GUILD_MEMBERS', 'MESSAGE_CONTENT'] });
      client.once('ready', async (c) => {
        
        console.log('logged in', c.user.username)
        const configurationChannels = config.discord_channels.split(',');
        for (let channel of configurationChannels) {
          //console.log(`fetching ${channel}`)
          channels.push(
            (await client.channels.fetch(channel)) as TextChannel,
          );
        }
        const rest = new REST().setToken(process.env.DISCORD_TOKEN);
      
        const guildIds = config.discord_guild_ids.split(',')
    
        Iif (callback) callback()
        Iif (callbacks.length) callbacks.forEach(c => c())
  
        client.on('interactionCreate', (interaction) => {
          for (const listener of interactionsListener) {
            listener(interaction)
          }
        })
        guildIds.forEach(async (guildId) => {
          await rest.put(
            Routes.applicationGuildCommands(config.discord_client_id, guildId),
            { body: discordCommands },
          );    
        })              
      });      
    }
    if (!inited) {
      inited = true
      client.login(process.env.DISCORD_TOKEN);
    } else if (client.isReady()) {
      Iif (callback !== undefined) callback()
    } else {
      Iif (callback !== undefined) callbacks.push(callback)
    }
    this.setup = true;
  }
 
 
  async sendEmbed(embed:MessageEmbed, image:string|Buffer, platform:string) {
    channels.forEach(async (channel) => {
      await channel.send({
        embeds: [embed],
        files: [
          { attachment: image, name: 'token.png' },
          { attachment: platform, name: 'platform.png' },
        ],
      });
    });
  }
 
  async send(text: string, images: string[]) {
    channels.forEach(async (channel) => {
      await channel.send({
        content: text,
        files: images,
      });
    });
  }
}