mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
Fix username change logging and saving. Re-enable migrations.
This commit is contained in:
parent
1b1ae98714
commit
e231faf51f
12 changed files with 255 additions and 119 deletions
|
@ -16,7 +16,7 @@
|
|||
"MEMBER_ROLE_REMOVE": "🔑 {userMention(member)}: role(s) **{roles}** removed by {userMention(mod)}",
|
||||
"MEMBER_ROLE_CHANGES": "🔑 {userMention(member)}: roles changed: added **{addedRoles}**, removed **{removedRoles}** by {userMention(mod)}",
|
||||
"MEMBER_NICK_CHANGE": "✏ {userMention(member)}: nickname changed from **{oldNick}** to **{newNick}**",
|
||||
"MEMBER_USERNAME_CHANGE": "✏ {userMention(member)}: username changed from **{oldName}** to **{newName}**",
|
||||
"MEMBER_USERNAME_CHANGE": "✏ {userMention(user)}: username changed from **{oldName}** to **{newName}**",
|
||||
"MEMBER_RESTORE": "💿 Restored {restoredData} for {userMention(member)} on rejoin",
|
||||
|
||||
"CHANNEL_CREATE": "🖊 Channel {channelMention(channel)} was created",
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
68
src/data/GuildNicknameHistory.ts
Normal file
68
src/data/GuildNicknameHistory.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import { BaseRepository } from "./BaseRepository";
|
||||
import { getRepository, Repository } from "typeorm";
|
||||
import { NicknameHistoryEntry } from "./entities/NicknameHistoryEntry";
|
||||
import { sorter } from "../utils";
|
||||
|
||||
export const MAX_NICKNAME_ENTRIES_PER_USER = 10;
|
||||
|
||||
export class GuildNicknameHistory extends BaseRepository {
|
||||
private nicknameHistory: Repository<NicknameHistoryEntry>;
|
||||
|
||||
constructor(guildId) {
|
||||
super(guildId);
|
||||
this.nicknameHistory = getRepository(NicknameHistoryEntry);
|
||||
}
|
||||
|
||||
async getByUserId(userId): Promise<NicknameHistoryEntry[]> {
|
||||
return this.nicknameHistory.find({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
},
|
||||
order: {
|
||||
id: "DESC",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getLastEntry(userId): Promise<NicknameHistoryEntry> {
|
||||
return this.nicknameHistory.findOne({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
},
|
||||
order: {
|
||||
id: "DESC",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async addEntry(userId, nickname) {
|
||||
await this.nicknameHistory.insert({
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
nickname,
|
||||
});
|
||||
|
||||
// Cleanup (leave only the last MAX_NICKNAME_ENTRIES_PER_USER entries)
|
||||
const lastEntries = await this.getByUserId(userId);
|
||||
if (lastEntries.length > MAX_NICKNAME_ENTRIES_PER_USER) {
|
||||
const earliestEntry = lastEntries
|
||||
.sort(sorter("timestamp", "DESC"))
|
||||
.slice(0, 10)
|
||||
.reduce((earliest, entry) => {
|
||||
if (earliest == null) return entry;
|
||||
if (entry.id < earliest.id) return entry;
|
||||
return earliest;
|
||||
}, null);
|
||||
|
||||
this.nicknameHistory
|
||||
.createQueryBuilder()
|
||||
.where("guild_id = :guildId", { guildId: this.guildId })
|
||||
.andWhere("user_id = :userId", { userId })
|
||||
.andWhere("id < :id", { id: earliestEntry.id })
|
||||
.delete()
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
export enum NameHistoryEntryTypes {
|
||||
Username = 1,
|
||||
Nickname
|
||||
}
|
67
src/data/UsernameHistory.ts
Normal file
67
src/data/UsernameHistory.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { BaseRepository } from "./BaseRepository";
|
||||
import { getRepository, Repository } from "typeorm";
|
||||
import { UsernameHistoryEntry } from "./entities/UsernameHistoryEntry";
|
||||
import { sorter } from "../utils";
|
||||
|
||||
export const MAX_USERNAME_ENTRIES_PER_USER = 10;
|
||||
|
||||
export class UsernameHistory extends BaseRepository {
|
||||
private usernameHistory: Repository<UsernameHistoryEntry>;
|
||||
|
||||
constructor(guildId) {
|
||||
super(guildId);
|
||||
this.usernameHistory = getRepository(UsernameHistoryEntry);
|
||||
}
|
||||
|
||||
async getByUserId(userId): Promise<UsernameHistoryEntry[]> {
|
||||
return this.usernameHistory.find({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
},
|
||||
order: {
|
||||
id: "DESC",
|
||||
},
|
||||
take: MAX_USERNAME_ENTRIES_PER_USER,
|
||||
});
|
||||
}
|
||||
|
||||
getLastEntry(userId): Promise<UsernameHistoryEntry> {
|
||||
return this.usernameHistory.findOne({
|
||||
where: {
|
||||
guild_id: this.guildId,
|
||||
user_id: userId,
|
||||
},
|
||||
order: {
|
||||
id: "DESC",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async addEntry(userId, username) {
|
||||
await this.usernameHistory.insert({
|
||||
user_id: userId,
|
||||
username,
|
||||
});
|
||||
|
||||
// Cleanup (leave only the last MAX_USERNAME_ENTRIES_PER_USER entries)
|
||||
const lastEntries = await this.getByUserId(userId);
|
||||
if (lastEntries.length > MAX_USERNAME_ENTRIES_PER_USER) {
|
||||
const earliestEntry = lastEntries
|
||||
.sort(sorter("timestamp", "DESC"))
|
||||
.slice(0, 10)
|
||||
.reduce((earliest, entry) => {
|
||||
if (earliest == null) return entry;
|
||||
if (entry.id < earliest.id) return entry;
|
||||
return earliest;
|
||||
}, null);
|
||||
|
||||
this.usernameHistory
|
||||
.createQueryBuilder()
|
||||
.andWhere("user_id = :userId", { userId })
|
||||
.andWhere("id < :id", { id: earliestEntry.id })
|
||||
.delete()
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("name_history")
|
||||
export class NameHistoryEntry {
|
||||
@Entity("nickname_history")
|
||||
export class NicknameHistoryEntry {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
@ -10,9 +10,7 @@ export class NameHistoryEntry {
|
|||
|
||||
@Column() user_id: string;
|
||||
|
||||
@Column() type: number;
|
||||
|
||||
@Column() value: string;
|
||||
@Column() nickname: string;
|
||||
|
||||
@Column() timestamp: string;
|
||||
}
|
14
src/data/entities/UsernameHistoryEntry.ts
Normal file
14
src/data/entities/UsernameHistoryEntry.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import { Entity, Column, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity("username_history")
|
||||
export class UsernameHistoryEntry {
|
||||
@Column()
|
||||
@PrimaryColumn()
|
||||
id: string;
|
||||
|
||||
@Column() user_id: string;
|
||||
|
||||
@Column() username: string;
|
||||
|
||||
@Column() timestamp: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue