3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Add persist plugin

This commit is contained in:
Dragory 2018-07-30 23:35:44 +03:00
parent e7734c558c
commit ad6afdfac1
10 changed files with 199 additions and 4 deletions

View file

@ -0,0 +1,53 @@
import knex from "../knex";
import PersistedData from "../models/PersistedData";
export interface IPartialPersistData {
roles?: string[];
nickname?: string;
is_voice_muted?: boolean;
}
export class GuildPersistedData {
protected guildId: string;
constructor(guildId) {
this.guildId = guildId;
}
async find(userId: string) {
const result = await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.first();
return result ? new PersistedData(result) : null;
}
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) finalData.is_voice_muted = data.is_voice_muted ? 1 : 0;
const existing = await this.find(userId);
if (existing) {
await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.update(finalData);
} else {
await knex("persisted_data").insert({
...finalData,
guild_id: this.guildId,
user_id: userId
});
}
}
async clear(userId: string) {
await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.delete();
}
}