Large swathes of re-engineering
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,29 @@
|
||||
import { Guild } from "discord.js";
|
||||
import { SQLCommon } from "../storage/interfaces";
|
||||
import { SQLResult } from "../storage/enumerations";
|
||||
import { Repository } from "typeorm";
|
||||
import { DBServer } from "../storage/entities/DBServer";
|
||||
|
||||
export async function doesGuildExist(db: SQLCommon, guild : Guild) : Promise<boolean> {
|
||||
const queryResult : Object[] = await db.getAllParameterized(
|
||||
"SELECT * FROM servers WHERE server_snowflake = ?",
|
||||
[guild.id]
|
||||
)
|
||||
|
||||
return queryResult.length != 0
|
||||
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: SQLCommon, guild: Guild) : Promise<SQLResult> {
|
||||
export async function insertGuild(db: Repository<DBServer>, guild: Guild) : Promise<DBServer | null> {
|
||||
const alreadyExists: boolean = await doesGuildExist(db, guild)
|
||||
|
||||
if (alreadyExists) {
|
||||
return SQLResult.ALREADYEXISTS
|
||||
return await db.findOne({"where": {server_snowflake: guild.id}})
|
||||
}
|
||||
try {
|
||||
await db.runParameterized(
|
||||
"INSERT INTO servers VALUES (?, ?, ?)",
|
||||
[guild.id, guild.name, guild.description]
|
||||
)
|
||||
const server: DBServer = await db.create({
|
||||
server_snowflake: guild.id,
|
||||
server_name: guild.name,
|
||||
server_description: guild.description ?? ""
|
||||
})
|
||||
|
||||
return SQLResult.CREATED
|
||||
return await db.save(server)
|
||||
} catch (err) {
|
||||
console.log("Insert Failed")
|
||||
//TODO Winston should handle this
|
||||
console.log(err)
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,79 @@
|
||||
import { Role } from "discord.js";
|
||||
import { SQLCommon } from "../storage/interfaces";
|
||||
import { SQLResult } from "../storage/enumerations";
|
||||
import { Repository } from "typeorm";
|
||||
import { DBRole } from "../storage/entities/DBRole";
|
||||
|
||||
export async function doesRoleExist(db: SQLCommon, role : Role) : Promise<boolean> {
|
||||
const queryResult : Object[] = await db.getAllParameterized(
|
||||
"SELECT * FROM roles WHERE role_snowflake = ?",
|
||||
[role.id]
|
||||
)
|
||||
|
||||
return queryResult.length != 0
|
||||
export async function doesRoleExist(db: Repository<DBRole>, role : Role) : Promise<boolean> {
|
||||
return (await db.findOne({"where": {role_snowflake: role.id}})) != null
|
||||
}
|
||||
|
||||
export async function insertRole(db: SQLCommon, role: Role) : Promise<SQLResult> {
|
||||
export async function insertRole(db: Repository<DBRole>, role: Role) : Promise<DBRole | null> {
|
||||
const alreadyExists: boolean = await doesRoleExist(db, role)
|
||||
|
||||
if(alreadyExists) {
|
||||
return SQLResult.ALREADYEXISTS
|
||||
return await db.findOne({"where": {role_snowflake: role.id}})
|
||||
}
|
||||
|
||||
try {
|
||||
await db.runParameterized(
|
||||
"INSERT INTO roles VALUES (?, ?, ?, 0)",
|
||||
[role.id, role.guild.id, role.name]
|
||||
)
|
||||
const newRole : DBRole = await db.create({
|
||||
role_snowflake: role.id,
|
||||
server: {server_snowflake: role.guild.id},
|
||||
role_name: role.name,
|
||||
is_deleted: false
|
||||
})
|
||||
|
||||
return SQLResult.CREATED
|
||||
return await db.save(newRole)
|
||||
} catch (err) {
|
||||
console.log("ROLE INSERT ERROR")
|
||||
console.log(err)
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateRole(db: SQLCommon, role: Role): Promise<SQLResult> {
|
||||
export async function updateRole(db: Repository<DBRole>, role: Role): Promise<DBRole | null> {
|
||||
const roleExists: boolean = await doesRoleExist(db, role)
|
||||
|
||||
if(!roleExists) {
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
await db.runParameterized(
|
||||
"UPDATE roles SET role_name = ? WHERE role_snowflake = ?",
|
||||
[role.name, role.id]
|
||||
)
|
||||
const toUpdate: DBRole | null = await db.findOne({"where": {role_snowflake: role.id}})
|
||||
|
||||
return SQLResult.UPDATED
|
||||
if(toUpdate != null) {
|
||||
toUpdate.role_name = role.name
|
||||
|
||||
return await db.save(toUpdate)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.log("ROLE UPDATE FAILED")
|
||||
console.log(err)
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function markRoleDeleted(db: SQLCommon, role: Role) : Promise<SQLResult> {
|
||||
export async function markRoleDeleted(db: Repository<DBRole>, role: Role) : Promise<DBRole | null> {
|
||||
const roleExists: boolean = await doesRoleExist(db, role)
|
||||
|
||||
if(!roleExists) {
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
await db.runParameterized(
|
||||
"UPDATE roles SET is_deleted = 1 WHERE role_snowflake = ?",
|
||||
[role.id]
|
||||
)
|
||||
const toUpdate: DBRole | null = await db.findOne({"where": {role_snowflake: role.id}})
|
||||
|
||||
return SQLResult.UPDATED
|
||||
if(toUpdate != null) {
|
||||
toUpdate.is_deleted = true
|
||||
|
||||
return await db.save(toUpdate)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("ROLE DELETE FAILED")
|
||||
console.log(err)
|
||||
return SQLResult.FAILED
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Guild, VoiceBasedChannel } from "discord.js";
|
||||
import { SQLCommon } from "../storage/interfaces";
|
||||
|
||||
export async function breadbotInCall(db: SQLCommon, channel: VoiceBasedChannel) : Promise<boolean> {
|
||||
const queryResult: Object[] = await db.getAllParameterized(
|
||||
"SELECT * FROM calls WHERE channel_snowflake = ? AND call_end_time IS NULL",
|
||||
[channel.id]
|
||||
)
|
||||
|
||||
return queryResult.length != 0
|
||||
}
|
||||
|
||||
export async function getExistingCallID(db: SQLCommon, channel: VoiceBasedChannel) : Promise<Number> {
|
||||
const queryResult: any[] = await db.getAllParameterized(
|
||||
"SELECT * FROM calls WHERE channel_snowflake = ? AND call_end_time IS NULL",
|
||||
[channel.id]
|
||||
)
|
||||
|
||||
return queryResult.length != 0 ? queryResult[0].call_id : -1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user