Skip to content

Commit

Permalink
Add last_updated for game options (#1917)
Browse files Browse the repository at this point in the history
* Add last_updated for game options

* deepscan
  • Loading branch information
Brainicism authored Mar 2, 2024
1 parent 32c3eeb commit 83e5781
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Kysely } from "kysely";
import { KmqDB } from "../typings/kmq_db";

export async function up(db: Kysely<KmqDB>): Promise<void> {
await db.schema
.alterTable("game_options")
.addColumn("last_updated", "datetime")
.execute();
}

export async function down(db: Kysely<KmqDB>): Promise<void> {
await db.schema
.alterTable("game_options")
.dropColumn("last_updated")
.execute();
}
13 changes: 5 additions & 8 deletions src/structures/guild_preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import GameOption from "../enums/game_option_name";
import Session from "./session";
import _ from "lodash";
import dbContext from "../database_context";
import type { GameOptions as GameOptionsSchema } from "../typings/kmq_db";
import type { GenderModeOptions } from "../enums/option_types/gender";
import type { Insertable } from "kysely";
import type { MutexInterface } from "async-mutex";
import type ArtistType from "../enums/option_types/artist_type";
import type GameOptions from "../interfaces/game_options";
Expand Down Expand Up @@ -932,21 +934,15 @@ export default class GuildPreference {
async updateGuildPreferences(
updatedOptionsObjects?: Array<{ name: string; value: GameOptionValue }>,
): Promise<void> {
interface GameOptionDatabaseEntry {
guild_id: string;
client_id: string;
option_name: string;
option_value: string;
}

let updatedOptions: GameOptionDatabaseEntry[] = [];
let updatedOptions: Insertable<GameOptionsSchema>[] = [];
if (updatedOptionsObjects) {
updatedOptions = Object.values(updatedOptionsObjects).map(
(option) => ({
guild_id: this.guildID,
client_id: process.env.BOT_CLIENT_ID as string,
option_name: option.name,
option_value: JSON.stringify(option.value),
last_updated: new Date(),
}),
);
} else {
Expand All @@ -958,6 +954,7 @@ export default class GuildPreference {
client_id: process.env.BOT_CLIENT_ID as string,
option_name: optionName,
option_value: JSON.stringify(optionValue),
last_updated: new Date(),
};
});
}
Expand Down
1 change: 1 addition & 0 deletions src/typings/kmq_db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface GameOptionPresetsJson {
export interface GameOptions {
client_id: Generated<string>;
guild_id: string;
last_updated: Generated<Date | null>;
option_name: string;
option_value: Generated<string | null>;
}
Expand Down

0 comments on commit 83e5781

Please sign in to comment.