DB Storage Re-engineering #1

Open
Bradley wants to merge 4 commits from typescript_db_rework into typescript_refactor
6 changed files with 131 additions and 75 deletions
Showing only changes of commit 68d8415a77 - Show all commits

View File

@ -1,5 +1,5 @@
import "reflect-metadata"
import { Client, Events, GatewayIntentBits, Guild, GuildBasedChannel, Role } from "discord.js"
import { ChatInputCommandInteraction, Client, Events, GatewayIntentBits, Guild, GuildBasedChannel, Interaction, Role } from "discord.js"
import { config } from "./config"
import { DataSource } from "typeorm"
import { DBServer } from "./utilties/storage/entities/DBServer"
@ -15,6 +15,11 @@ import { DBMessage } from "./utilties/storage/entities/DBMessage"
import { DBMessageContentChanges } from "./utilties/storage/entities/DBMessageContentChanges"
import { DBMessageAttachments } from "./utilties/storage/entities/DBMessageAttachment"
import { setupMessageCapture } from "./utilties/events/messages"
import { utilities } from "./utilties"
import { commands } from "./commands"
import { DBCall } from "./utilties/storage/entities/DBCall"
import { DBCallTranscriptions } from "./utilties/storage/entities/DBCallTranscriptions"
import { DBCallUsers } from "./utilties/storage/entities/DBCallUsers"
console.log(__dirname + path.sep + "utilities" + path.sep + "storage" + path.sep + "entities" + path.sep + "*.ts")
@ -28,7 +33,10 @@ export const dataSource = new DataSource({
DBUser,
DBMessage,
DBMessageContentChanges,
DBMessageAttachments
DBMessageAttachments,
DBCall,
DBCallTranscriptions,
DBCallUsers
],
synchronize: true,
logging: true
@ -69,81 +77,41 @@ client.once(Events.ClientReady, async () => {
}
})
client.on(Events.GuildCreate, async (guild : Guild) => {
await utilities.commands.deployCommands(guild.id)
await utilities.guilds.insertGuild(serverRepo, guild)
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
await utilities.channels.insertChannel(channelRepo, channel)
})
guild.roles.cache.forEach(async (role: Role) => {
await insertRole(roleRepo, role)
})
})
client.on(Events.ChannelCreate, async (channel) => {
await utilities.channels.insertChannel(channelRepo, channel)
})
client.on(Events.ThreadCreate, async (channel) => {
await utilities.channels.insertChannel(channelRepo, channel)
})
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (!interaction.isCommand()) {
return
}
if (commands[interaction.commandName as keyof typeof commands]) {
commands[interaction.commandName as keyof typeof commands].execute(interaction as ChatInputCommandInteraction)
}
})
setupRoleCapture(client, serverRepo, roleRepo)
setupMessageCapture(client, channelRepo, userRepo, messageRepo, mccRepo, maRepo)
console.log("Breadbot is Ready")
})
client.login(config.DISCORD_TOKEN)
/*
export let db: SQLCommon
if (config.DB_MODE == "sqlite") {
db = new utilities.sqlite.SqliteDB("breadbot_test.db")
db.run("PRAGMA foreign_keys = ON")
utilities.tables.makeTables(db)
//TODO I really don't want this to be here.
utilities.events.messages.setupMessageCapture(client, db)
utilities.events.roles.setupRoleCapture(client, db)
}
client.once(Events.ClientReady, () => {
// TODO Winston should handle this
console.log("Breadbot is ready")
client.guilds.cache.forEach(async (guild: Guild) => {
await utilities.commands.deployCommands(guild.id)
// TODO handle failures?
await utilities.guilds.insertGuild(db, guild)
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
await utilities.channels.insertChannel(db, channel)
})
guild.roles.cache.forEach(async (role: Role) => {
await utilities.roles.insertRole(db, role)
})
})
})
client.on(Events.GuildCreate, async (guild : Guild) => {
await utilities.commands.deployCommands(guild.id)
await utilities.guilds.insertGuild(db, guild)
guild.channels.cache.forEach(async (channel: GuildBasedChannel) => {
await utilities.channels.insertChannel(db, channel)
})
})
client.on(Events.ChannelCreate, async (channel) => {
console.log("CHANNEL CREATE CALLED")
await utilities.channels.insertChannel(db, channel)
})
client.on(Events.ThreadCreate, async (channel) => {
console.log("THREAD CREATE CALLED")
console.log(channel.toString())
await utilities.channels.insertChannel(db, channel)
})
client.on(Events.InteractionCreate, async (interaction: Interaction) => {
if (!interaction.isCommand()) {
return
}
if (commands[interaction.commandName as keyof typeof commands]) {
commands[interaction.commandName as keyof typeof commands].execute(interaction as ChatInputCommandInteraction)
}
})
setInterval(async () => {
await utilities.breadthread.breadthreadProcessLocks(db, client)
}, 5000)
client.login(config.DISCORD_TOKEN)*/
client.login(config.DISCORD_TOKEN)

View File

@ -0,0 +1,34 @@
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { DBChannel } from "./DBChannel";
import { DBCallTranscriptions } from "./DBCallTranscriptions";
import { DBCallUsers } from "./DBCallUsers";
@Entity()
export class DBCall {
@PrimaryGeneratedColumn()
call_id: number
@ManyToOne(() => DBChannel, (channel: DBChannel) => channel.calls)
channel: DBChannel
@Column({type: "datetime"})
call_start_time: Date
@Column({type: "datetime", nullable: true, default: null})
call_end_time: Date | null
@Column({default: false})
call_consolidated: boolean
@Column({default: false})
call_transcribed: boolean
@Column({default: false})
call_data_cleaned_up: boolean
@OneToMany(() => DBCallTranscriptions, (transcription: DBCallTranscriptions) => transcription.call, {nullable: true})
transcriptions: DBCallTranscriptions[] | null
@OneToMany(() => DBCallUsers, (callUser: DBCallUsers) => callUser.call, {nullable: true})
participants: DBCallUsers | null
}

View File

@ -0,0 +1,21 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { DBCall } from "./DBCall";
import { DBUser } from "./DBUser";
@Entity()
export class DBCallTranscriptions {
@PrimaryGeneratedColumn()
transcription_id: number
@ManyToOne(() => DBCall, (call: DBCall) => call.transcriptions)
call: DBCall
@ManyToOne(() => DBUser, (user: DBUser) => user.transcriptions)
user: DBUser
@Column({type: "datetime"})
speaking_start_time: Date
@Column()
text: string
}

View File

@ -0,0 +1,21 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { DBCall } from "./DBCall";
import { DBUser } from "./DBUser";
@Entity()
export class DBCallUsers {
@PrimaryGeneratedColumn()
call_users_id: number
@ManyToOne(() => DBCall, (call: DBCall) => call.participants)
call: DBCall
@ManyToOne(() => DBUser, (user: DBUser) => user.call_history)
user: DBUser
@Column({type: "datetime"})
call_join_time: Date
@Column({type: "datetime", nullable: true, default: null})
call_leave_time: Date | null
}

View File

@ -1,6 +1,7 @@
import { Column, Entity, ManyToOne, OneToMany, PrimaryColumn } from "typeorm";
import { DBServer } from "./DBServer";
import { DBMessage } from "./DBMessage";
import { DBCall } from "./DBCall";
@Entity()
export class DBChannel {
@ -23,5 +24,8 @@ export class DBChannel {
is_voice: boolean
@OneToMany(() => DBMessage, (message: DBMessage) => message.channel)
messages: DBMessage[]
messages: DBMessage[] | null
@OneToMany(() => DBCall, (call: DBCall) => call.channel)
calls: DBCall[] | null
}

View File

@ -1,5 +1,7 @@
import { Column, Entity, OneToMany, PrimaryColumn } from "typeorm";
import { DBMessage } from "./DBMessage";
import { DBCallTranscriptions } from "./DBCallTranscriptions";
import { DBCallUsers } from "./DBCallUsers";
@Entity()
export class DBUser {
@ -14,4 +16,10 @@ export class DBUser {
@OneToMany(() => DBMessage, (message: DBMessage) => message.user)
messages: DBMessage[]
@OneToMany(() => DBCallTranscriptions, (transcription: DBCallTranscriptions) => transcription.user, {nullable: true})
transcriptions: DBCallTranscriptions[] | null
@OneToMany(() => DBCallUsers, (call_user: DBCallUsers) => call_user.user)
call_history: DBCallUsers[] | null
}