Save names using a queue to avoid race conditions

This commit is contained in:
Dragory 2020-06-02 01:40:02 +03:00
parent e684bf7dac
commit 69feccbcab
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 13 additions and 6 deletions

View file

@ -8,6 +8,7 @@ import * as t from "io-ts";
import { NICKNAME_RETENTION_PERIOD } from "../data/cleanup/nicknames";
import moment from "moment-timezone";
import { USERNAME_RETENTION_PERIOD } from "../data/cleanup/usernames";
import { Queue } from "../Queue";
const ConfigSchema = t.type({
can_view: t.boolean,
@ -22,6 +23,8 @@ export class NameHistoryPlugin extends ZeppelinPlugin<TConfigSchema> {
protected nicknameHistory: GuildNicknameHistory;
protected usernameHistory: UsernameHistory;
protected updateQueue: Queue;
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
return {
config: {
@ -42,6 +45,7 @@ export class NameHistoryPlugin extends ZeppelinPlugin<TConfigSchema> {
onLoad() {
this.nicknameHistory = GuildNicknameHistory.getGuildInstance(this.guildId);
this.usernameHistory = new UsernameHistory();
this.updateQueue = new Queue();
}
@d.command("names", "<userId:userId>")
@ -91,11 +95,11 @@ export class NameHistoryPlugin extends ZeppelinPlugin<TConfigSchema> {
@d.event("messageCreate")
async onMessage(msg: Message) {
this.updateNickname(msg.member);
this.updateQueue.add(() => this.updateNickname(msg.member));
}
@d.event("voiceChannelJoin")
async onVoiceChannelJoin(member: Member) {
this.updateNickname(member);
this.updateQueue.add(() => this.updateNickname(member));
}
}

View file

@ -2,14 +2,17 @@ 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) {
@ -21,15 +24,15 @@ export class UsernameSaver extends GlobalZeppelinPlugin {
}
}
@d.event("messageCreate")
@d.event("messageCreate", null)
async onMessage(msg: Message) {
if (msg.author.bot) return;
this.updateUsername(msg.author);
this.updateQueue.add(() => this.updateUsername(msg.author));
}
@d.event("voiceChannelJoin")
@d.event("voiceChannelJoin", null)
async onVoiceChannelJoin(member: Member) {
if (member.user.bot) return;
this.updateUsername(member.user);
this.updateQueue.add(() => this.updateUsername(member.user));
}
}