Basic message collection is working again

This commit is contained in:
2025-06-27 22:49:09 -04:00
parent a247bfb35e
commit ac2dd9983e
8 changed files with 134 additions and 12 deletions

View File

@@ -1,17 +1,26 @@
import { SQLCommon } from "../storage/interfaces";
import { GuildBasedChannel } from "discord.js";
import { DMChannel, GuildBasedChannel, PartialDMChannel } from "discord.js";
import { SQLResult } from "../storage/enumerations";
export async function doesChannelExist(db: SQLCommon, channel : GuildBasedChannel) : Promise<boolean> {
export async function doesChannelExistByID(db: SQLCommon, channelID: string) : Promise<boolean> {
const queryResult : Object[] = await db.getAllParameterized(
"SELECT * FROM channels WHERE server_snowflake = ? AND channel_snowflake = ?",
[channel.guild.id, channel.id]
"SELECT * FROM channels WHERE channel_snowflake = ?",
[channelID]
)
return queryResult.length != 0
}
export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel) : Promise<SQLResult> {
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 insertChannel(db: SQLCommon, channel: GuildBasedChannel | DMChannel | PartialDMChannel) : Promise<SQLResult> {
const alreadyExists: boolean = await doesChannelExist(db, channel)
if(alreadyExists) {
@@ -19,10 +28,18 @@ export async function insertChannel(db: SQLCommon, channel: GuildBasedChannel) :
}
try {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?)",
[channel.id, channel.guild.id, channel.name, channel.isThread()]
)
if (channel.isDMBased()) {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?)",
[channel.id, null, channel.recipient?.username, channel.isThread(), channel.isDMBased()]
)
} else {
await db.runParameterized(
"INSERT INTO channels VALUES (?, ?, ?, ?, ?)",
[channel.id, channel.guild.id, channel.name, channel.isThread(), channel.isDMBased()]
)
}
return SQLResult.CREATED
} catch (err) {

View File

@@ -0,0 +1,34 @@
import { Message, OmitPartialGroupDMChannel } from "discord.js";
import { SQLCommon } from "../storage/interfaces";
import { SQLResult } from "../storage/enumerations";
export async function doesMessageExist(db: SQLCommon, message: OmitPartialGroupDMChannel<Message<boolean>>) : Promise<boolean> {
const queryResult: Object[] = await db.getAllParameterized(
"SELECT * FROM messages WHERE message_snowflake = ?",
[message.id]
)
return queryResult.length != 0
}
export async function insertMessage(db: SQLCommon, message: OmitPartialGroupDMChannel<Message<boolean>>) : Promise<SQLResult> {
const alreadyExists: boolean = await doesMessageExist(db, message)
if(alreadyExists) {
return SQLResult.ALREADYEXISTS
}
try {
await db.runParameterized(
"INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)",
[message.id, message.channel.id, message.author.id,
message.content, message.createdTimestamp, 0]
)
return SQLResult.CREATED
} catch (err) {
//TODO Winston should handle this
console.log(err)
return SQLResult.FAILED
}
}

View File

@@ -0,0 +1,33 @@
import { User } from "discord.js";
import { SQLCommon } from "../storage/interfaces";
import { SQLResult } from "../storage/enumerations";
export async function doesUserExist(db: SQLCommon, user: User): Promise<boolean> {
const queryResult: Object[] = await db.getAllParameterized(
"SELECT * FROM users WHERE user_snowflake = ?",
[user.id]
)
return queryResult.length != 0
}
export async function insertUser(db: SQLCommon, user: User): Promise<SQLResult> {
const alreadyExists: boolean = await doesUserExist(db, user)
if(alreadyExists) {
return SQLResult.ALREADYEXISTS
}
try {
await db.runParameterized(
"INSERT INTO users VALUES (?, ?, ?)",
[user.id, user.username, user.displayName]
)
return SQLResult.CREATED
} catch (err) {
//TODO Winston should handle this
console.log(err)
return SQLResult.FAILED
}
}