Large swathes of re-engineering

This commit is contained in:
2025-11-12 20:41:13 -05:00
parent 6f868dff1e
commit 34f57b96dc
20 changed files with 1567 additions and 196 deletions

View File

@@ -1,50 +1,37 @@
import { SQLCommon } from "../storage/interfaces";
import { DMChannel, GuildBasedChannel, PartialDMChannel } from "discord.js";
import { SQLResult } from "../storage/enumerations";
import { Repository } from "typeorm";
import { DBChannel } from "../storage/entities/DBChannel";
export async function doesChannelExistByID(db: SQLCommon, channelID: string) : Promise<boolean> {
const queryResult : Object[] = await db.getAllParameterized(
"SELECT * FROM channels WHERE channel_snowflake = ?",
[channelID]
)
return queryResult.length != 0
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: SQLCommon, channel : GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<boolean> {
const queryResult : Object[] = await db.getAllParameterized(
"SELECT * FROM channels WHERE channel_snowflake = ?",
[channel.id]
)
return queryResult.length != 0
export async function doesChannelExist(db: Repository<DBChannel>, channel : GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<boolean> {
return await doesChannelExistByID(db, channel.id)
}
export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<SQLResult> {
export async function insertChannel(db: Repository<DBChannel>, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<DBChannel | null> {
const alreadyExists: boolean = await doesChannelExist(db, channel)
if(alreadyExists) {
return SQLResult.ALREADYEXISTS
return await db.findOne({"where": {channel_snowflake: channel.id}})
}
try {
if (channel.isDMBased()) {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?, ?)",
[channel.id, null, channel.recipient?.username, channel.isThread(), channel.isDMBased(), channel.isVoiceBased()]
)
} else {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?, ?)",
[channel.id, channel.guild.id, channel.name, channel.isThread(), channel.isDMBased(), channel.isVoiceBased()]
)
}
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 SQLResult.CREATED
return await db.save(newChannel)
} catch (err) {
//TODO Winston should handle this
console.log("CHANNEL INSERT ERROR")
console.log(err)
return SQLResult.FAILED
return null
}
}