mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { Repository } from "typeorm";
|
|
import { BaseGuildRepository } from "./BaseGuildRepository";
|
|
import { dataSource } from "./dataSource";
|
|
import { PersistedData } from "./entities/PersistedData";
|
|
|
|
export class GuildPersistedData extends BaseGuildRepository {
|
|
private persistedData: Repository<PersistedData>;
|
|
|
|
constructor(guildId) {
|
|
super(guildId);
|
|
this.persistedData = dataSource.getRepository(PersistedData);
|
|
}
|
|
|
|
async find(userId: string) {
|
|
return this.persistedData.findOne({
|
|
where: {
|
|
guild_id: this.guildId,
|
|
user_id: userId,
|
|
},
|
|
});
|
|
}
|
|
|
|
async set(userId: string, data: Partial<PersistedData> = {}) {
|
|
const existing = await this.find(userId);
|
|
if (existing) {
|
|
await this.persistedData.update(
|
|
{
|
|
guild_id: this.guildId,
|
|
user_id: userId,
|
|
},
|
|
data,
|
|
);
|
|
} else {
|
|
await this.persistedData.insert({
|
|
...data,
|
|
guild_id: this.guildId,
|
|
user_id: userId,
|
|
});
|
|
}
|
|
}
|
|
|
|
async clear(userId: string) {
|
|
await this.persistedData.delete({
|
|
guild_id: this.guildId,
|
|
user_id: userId,
|
|
});
|
|
}
|
|
}
|