zappyzep/src/data/GuildPersistedData.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import { PersistedData } from "./entities/PersistedData";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository } from "typeorm";
2018-07-30 23:35:44 +03:00
export interface IPartialPersistData {
roles?: string[];
nickname?: string;
is_voice_muted?: boolean;
}
export class GuildPersistedData extends BaseRepository {
private persistedData: Repository<PersistedData>;
2018-07-30 23:35:44 +03:00
constructor(guildId) {
super(guildId);
this.persistedData = getRepository(PersistedData);
2018-07-30 23:35:44 +03:00
}
async find(userId: string) {
return this.persistedData.findOne({
where: {
guild_id: this.guildId,
user_id: userId
}
});
2018-07-30 23:35:44 +03:00
}
async set(userId: string, data: IPartialPersistData = {}) {
const finalData: any = {};
if (data.roles) finalData.roles = data.roles.join(",");
if (data.nickname) finalData.nickname = data.nickname;
if (data.is_voice_muted != null) finalData.is_voice_muted = data.is_voice_muted ? 1 : 0;
2018-07-30 23:35:44 +03:00
const existing = await this.find(userId);
if (existing) {
await this.persistedData.update(
{
guild_id: this.guildId,
user_id: userId
},
finalData
);
2018-07-30 23:35:44 +03:00
} else {
await this.persistedData.insert({
2018-07-30 23:35:44 +03:00
...finalData,
guild_id: this.guildId,
user_id: userId
});
}
}
async clear(userId: string) {
await this.persistedData.delete({
guild_id: this.guildId,
user_id: userId
});
2018-07-30 23:35:44 +03:00
}
}