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:
parent
e7734c558c
commit
ad6afdfac1
10 changed files with 199 additions and 4 deletions
53
src/data/GuildPersistedData.ts
Normal file
53
src/data/GuildPersistedData.ts
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue