29 lines
982 B
TypeScript
29 lines
982 B
TypeScript
import { Guild } from "discord.js";
|
|
import { Repository } from "typeorm";
|
|
import { DBServer } from "../storage/entities/DBServer";
|
|
|
|
export async function doesGuildExist(db: Repository<DBServer>, guild : Guild) : Promise<boolean> {
|
|
return (await db.findOne({"where": {server_snowflake: guild.id}})) != null
|
|
}
|
|
|
|
export async function insertGuild(db: Repository<DBServer>, guild: Guild) : Promise<DBServer | null> {
|
|
const alreadyExists: boolean = await doesGuildExist(db, guild)
|
|
|
|
if (alreadyExists) {
|
|
return await db.findOne({"where": {server_snowflake: guild.id}})
|
|
}
|
|
try {
|
|
const server: DBServer = await db.create({
|
|
server_snowflake: guild.id,
|
|
server_name: guild.name,
|
|
server_description: guild.description ?? ""
|
|
})
|
|
|
|
return await db.save(server)
|
|
} catch (err) {
|
|
console.log("Insert Failed")
|
|
//TODO Winston should handle this
|
|
console.log(err)
|
|
return null
|
|
}
|
|
} |