Basic message collection is working again
This commit is contained in:
@@ -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) {
|
||||
|
||||
34
src/utilties/discord/messages.ts
Normal file
34
src/utilties/discord/messages.ts
Normal 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
|
||||
}
|
||||
}
|
||||
33
src/utilties/discord/users.ts
Normal file
33
src/utilties/discord/users.ts
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user