import { DMChannel, GuildBasedChannel, PartialDMChannel } from "discord.js"; import { Repository } from "typeorm"; import { DBChannel } from "../storage/entities/DBChannel"; export async function doesChannelExistByID(db: Repository, channelID: string) : Promise { return (await db.findOne({"where": {channel_snowflake: channelID}})) != null } export async function doesChannelExist(db: Repository, channel : GuildBasedChannel | DMChannel | PartialDMChannel) : Promise { return await doesChannelExistByID(db, channel.id) } export async function insertChannel(db: Repository, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise { const alreadyExists: boolean = await doesChannelExist(db, channel) if(alreadyExists) { return await db.findOne({"where": {channel_snowflake: channel.id}}) } try { const newChannel : DBChannel = await db.create({ channel_snowflake: channel.id, channel_name: channel.isDMBased() ? channel.recipient?.username : channel.name, is_dm: channel.isDMBased(), is_thread: channel.isThread(), is_voice: channel.isVoiceBased(), server: channel.isDMBased() ? null : {server_snowflake: channel.guild.id} }) return await db.save(newChannel) } catch (err) { //TODO Winston should handle this console.log("CHANNEL INSERT ERROR") console.log(err) return null } }