All files / src/modules/api/discord discord.service.ts

15.78% Statements 3/19
0% Branches 0/2
9.09% Functions 1/11
15.38% Lines 2/13

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                  1x 1x                                                                              
import { Services } from "@/types/Constants";
import { Inject, Injectable } from "@nestjs/common";
import { AxiosResponse } from "axios";
import { PartialChannelData } from "discord.js";
import { GuildDTO } from "../guild/guild.dto";
import { DiscordHttpService } from "./discord-http.service";
import { IDiscordService } from "./interfaces/IDiscordService.interface";
 
@Injectable()
export class DiscordService implements IDiscordService {
	public constructor(@Inject(Services.Discord_HTTP) private readonly DiscordHttp: DiscordHttpService) {}
 
	public async getBotGuilds(): Promise<GuildDTO[]> {
		return (await this.DiscordHttp.fetchBotGuilds()).data;
	}
 
	public async getUserGuilds(accessToken: string): Promise<GuildDTO[]> {
		return (await this.DiscordHttp.fetchUserGuilds(accessToken)).data;
	}
 
	public async getMutualGuilds(accessToken: string): Promise<{
		mutual: {
			admin: GuildDTO[];
			owner: GuildDTO[];
		};
	}> {
		const bGuilds = await this.getBotGuilds();
		const uGuilds = await this.getUserGuilds(accessToken);
 
		const isUserAdmin = uGuilds.filter(({ permissions }) => (Number.parseInt(permissions) & 0x8) === 0x8);
		const isUserOwner = uGuilds.filter(({ owner }) => owner);
 
		const mutualAdminGuilds = isUserAdmin.filter(
			(guild: GuildDTO) => bGuilds.some((g: GuildDTO) => g.id === guild.id) && !guild.owner,
		);
		const mutualOwnerGuilds = isUserOwner.filter((guild) => bGuilds.some((g) => g.id !== guild.id));
 
		return {
			mutual: {
				admin: mutualAdminGuilds,
				owner: mutualOwnerGuilds,
			},
		};
	}
 
	public async getGuildChannels(guildId: string): Promise<AxiosResponse<PartialChannelData, unknown>> {
		return this.DiscordHttp.fetchGuildChannels(guildId);
	}
}