3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-18 23:09:59 +00:00
zeppelin/backend/src/plugins/UsernameSaver.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

import { decorators as d, GlobalPlugin } from "knub";
import { UsernameHistory } from "../data/UsernameHistory";
import { Member, Message, User } from "eris";
import { GlobalZeppelinPlugin } from "./GlobalZeppelinPlugin";
import { Queue } from "../Queue";
export class UsernameSaver extends GlobalZeppelinPlugin {
public static pluginName = "username_saver";
protected usernameHistory: UsernameHistory;
protected updateQueue: Queue;
async onLoad() {
this.usernameHistory = new UsernameHistory();
this.updateQueue = new Queue();
}
protected async updateUsername(user: User) {
2019-05-04 10:47:59 +03:00
if (!user) return;
const newUsername = `${user.username}#${user.discriminator}`;
const latestEntry = await this.usernameHistory.getLastEntry(user.id);
2019-05-03 23:35:09 +03:00
if (!latestEntry || newUsername !== latestEntry.username) {
await this.usernameHistory.addEntry(user.id, newUsername);
}
}
@d.event("messageCreate", null)
async onMessage(msg: Message) {
if (msg.author.bot) return;
this.updateQueue.add(() => this.updateUsername(msg.author));
}
@d.event("voiceChannelJoin", null)
async onVoiceChannelJoin(member: Member) {
if (member.user.bot) return;
this.updateQueue.add(() => this.updateUsername(member.user));
}
}