Files
Breadbot/src/utilties/discord/channels.ts

37 lines
1.5 KiB
TypeScript

import { DMChannel, GuildBasedChannel, PartialDMChannel } from "discord.js";
import { Repository } from "typeorm";
import { DBChannel } from "../storage/entities/DBChannel";
export async function doesChannelExistByID(db: Repository<DBChannel>, channelID: string) : Promise<boolean> {
return (await db.findOne({"where": {channel_snowflake: channelID}})) != null
}
export async function doesChannelExist(db: Repository<DBChannel>, channel : GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<boolean> {
return await doesChannelExistByID(db, channel.id)
}
export async function insertChannel(db: Repository<DBChannel>, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<DBChannel | null> {
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
}
}