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

Add name history plugin

This commit is contained in:
Dragory 2019-01-06 15:27:51 +02:00
parent 50c6233190
commit 681517341e
7 changed files with 245 additions and 6 deletions

View file

@ -0,0 +1,64 @@
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository } from "typeorm";
import { NameHistoryEntry } from "./entities/NameHistoryEntry";
const MAX_ENTRIES_PER_USER = 10;
export class GuildNameHistory extends BaseRepository {
private nameHistory: Repository<NameHistoryEntry>;
constructor(guildId) {
super(guildId);
this.nameHistory = getRepository(NameHistoryEntry);
}
async getByUserId(userId): Promise<NameHistoryEntry[]> {
return this.nameHistory.find({
where: {
guild_id: this.guildId,
user_id: userId
},
order: {
id: "DESC"
},
take: MAX_ENTRIES_PER_USER
});
}
getLastEntryByType(userId, type): Promise<NameHistoryEntry> {
return this.nameHistory.findOne({
where: {
guild_id: this.guildId,
user_id: userId,
type
},
order: {
id: "DESC"
}
});
}
async addEntry(userId, type, value) {
await this.nameHistory.insert({
guild_id: this.guildId,
user_id: userId,
type,
value
});
// Cleanup (leave only the last MAX_ENTRIES_PER_USER entries)
const lastEntries = await this.getByUserId(userId);
if (lastEntries.length > MAX_ENTRIES_PER_USER) {
const earliestEntry = lastEntries[lastEntries.length - 1];
if (!earliestEntry) return;
this.nameHistory
.createQueryBuilder()
.where("guild_id = :guildId", { guildId: this.guildId })
.andWhere("user_id = :userId", { userId })
.andWhere("id < :id", { id: earliestEntry.id })
.delete()
.execute();
}
}
}

View file

@ -0,0 +1,4 @@
export enum NameHistoryEntryTypes {
Username = 1,
Nickname
}

View file

@ -0,0 +1,18 @@
import { Entity, Column, PrimaryColumn } from "typeorm";
@Entity("name_history")
export class NameHistoryEntry {
@Column()
@PrimaryColumn()
id: string;
@Column() guild_id: string;
@Column() user_id: string;
@Column() type: number;
@Column() value: string;
@Column() timestamp: string;
}