2019-05-03 23:31:38 +03:00
|
|
|
import { decorators as d, GlobalPlugin } from "knub";
|
|
|
|
import { UsernameHistory } from "../data/UsernameHistory";
|
2020-06-02 00:26:06 +03:00
|
|
|
import { Member, Message, User } from "eris";
|
2019-08-04 15:47:09 +03:00
|
|
|
import { GlobalZeppelinPlugin } from "./GlobalZeppelinPlugin";
|
2020-06-02 01:40:02 +03:00
|
|
|
import { Queue } from "../Queue";
|
2019-05-03 23:31:38 +03:00
|
|
|
|
2019-08-04 15:47:09 +03:00
|
|
|
export class UsernameSaver extends GlobalZeppelinPlugin {
|
2019-05-03 23:31:38 +03:00
|
|
|
public static pluginName = "username_saver";
|
|
|
|
|
|
|
|
protected usernameHistory: UsernameHistory;
|
2020-06-02 01:40:02 +03:00
|
|
|
protected updateQueue: Queue;
|
2019-05-03 23:31:38 +03:00
|
|
|
|
|
|
|
async onLoad() {
|
2019-05-25 21:25:34 +03:00
|
|
|
this.usernameHistory = new UsernameHistory();
|
2020-06-02 01:40:02 +03:00
|
|
|
this.updateQueue = new Queue();
|
2019-05-03 23:31:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected async updateUsername(user: User) {
|
2019-05-04 10:47:59 +03:00
|
|
|
if (!user) return;
|
2019-05-03 23:31:38 +03:00
|
|
|
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) {
|
2019-05-03 23:31:38 +03:00
|
|
|
await this.usernameHistory.addEntry(user.id, newUsername);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 01:40:02 +03:00
|
|
|
@d.event("messageCreate", null)
|
2020-06-02 00:26:06 +03:00
|
|
|
async onMessage(msg: Message) {
|
|
|
|
if (msg.author.bot) return;
|
2020-06-02 01:40:02 +03:00
|
|
|
this.updateQueue.add(() => this.updateUsername(msg.author));
|
2019-05-03 23:31:38 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 01:40:02 +03:00
|
|
|
@d.event("voiceChannelJoin", null)
|
2020-06-02 00:26:06 +03:00
|
|
|
async onVoiceChannelJoin(member: Member) {
|
|
|
|
if (member.user.bot) return;
|
2020-06-02 01:40:02 +03:00
|
|
|
this.updateQueue.add(() => this.updateUsername(member.user));
|
2019-05-03 23:31:38 +03:00
|
|
|
}
|
|
|
|
}
|