Skip to content
Extraits de code Groupes Projets
Valider c27f9ad7 rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

channel.id is unique enough

parent 597bebdd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -138,29 +138,27 @@ export class DiscordBot { ...@@ -138,29 +138,27 @@ export class DiscordBot {
return this.provisioner; return this.provisioner;
} }
public lockChannel(channel: Discord.GuildChannel) { public lockChannel(channel: Discord.Channel) {
const key = `${channel.guild.id}|${channel.id}`; if (this.channelLocks[channel.id]) {
if (this.channelLocks[key]) {
return; return;
} }
let i: NodeJS.Timeout; let i: NodeJS.Timeout;
const p = new Promise((resolve) => { const p = new Promise((resolve) => {
i = setInterval(resolve, this.config.limits.discordSendDelay); i = setInterval(resolve, this.config.limits.discordSendDelay);
this.channelLocks[key] = {i, p}; this.channelLocks[channel.id] = {i, p};
}); });
} }
public unlockChannel(channel: Discord.GuildChannel) { public unlockChannel(channel: Discord.Channel) {
const key = `${channel.guild.id}|${channel.id}`; const lock = this.channelLocks[channel.id];
const lock = this.channelLocks[key];
if (lock) { if (lock) {
clearTimeout(lock.i); clearTimeout(lock.i);
} }
delete this.channelLocks[key]; delete this.channelLocks[channel.id];
} }
public async waitUnlock(channel: Discord.GuildChannel) { public async waitUnlock(channel: Discord.Channel) {
const lock = this.channelLocks[`${channel.guild.id}|${channel.id}`]; const lock = this.channelLocks[channel.id];
if (lock) { if (lock) {
await lock.p; await lock.p;
} }
...@@ -229,7 +227,7 @@ export class DiscordBot { ...@@ -229,7 +227,7 @@ export class DiscordBot {
client.on("messageDelete", async (msg: Discord.Message) => { client.on("messageDelete", async (msg: Discord.Message) => {
try { try {
await this.waitUnlock(msg.channel as Discord.TextChannel); await this.waitUnlock(msg.channel);
this.discordMessageQueue[msg.channel.id] = (async () => { this.discordMessageQueue[msg.channel.id] = (async () => {
await (this.discordMessageQueue[msg.channel.id] || Promise.resolve()); await (this.discordMessageQueue[msg.channel.id] || Promise.resolve());
try { try {
...@@ -244,7 +242,7 @@ export class DiscordBot { ...@@ -244,7 +242,7 @@ export class DiscordBot {
}); });
client.on("messageUpdate", async (oldMessage: Discord.Message, newMessage: Discord.Message) => { client.on("messageUpdate", async (oldMessage: Discord.Message, newMessage: Discord.Message) => {
try { try {
await this.waitUnlock(newMessage.channel as Discord.TextChannel); await this.waitUnlock(newMessage.channel);
this.discordMessageQueue[newMessage.channel.id] = (async () => { this.discordMessageQueue[newMessage.channel.id] = (async () => {
await (this.discordMessageQueue[newMessage.channel.id] || Promise.resolve()); await (this.discordMessageQueue[newMessage.channel.id] || Promise.resolve());
try { try {
...@@ -259,7 +257,7 @@ export class DiscordBot { ...@@ -259,7 +257,7 @@ export class DiscordBot {
}); });
client.on("message", async (msg: Discord.Message) => { client.on("message", async (msg: Discord.Message) => {
try { try {
await this.waitUnlock(msg.channel as Discord.TextChannel); await this.waitUnlock(msg.channel);
this.discordMessageQueue[msg.channel.id] = (async () => { this.discordMessageQueue[msg.channel.id] = (async () => {
await (this.discordMessageQueue[msg.channel.id] || Promise.resolve()); await (this.discordMessageQueue[msg.channel.id] || Promise.resolve());
try { try {
...@@ -461,9 +459,9 @@ export class DiscordBot { ...@@ -461,9 +459,9 @@ export class DiscordBot {
const msg = await chan.fetchMessage(storeEvent.DiscordId); const msg = await chan.fetchMessage(storeEvent.DiscordId);
try { try {
this.lockChannel(msg.channel as Discord.TextChannel); this.lockChannel(msg.channel);
await msg.delete(); await msg.delete();
this.unlockChannel(msg.channel as Discord.TextChannel); this.unlockChannel(msg.channel);
log.info(`Deleted message`); log.info(`Deleted message`);
} catch (ex) { } catch (ex) {
log.warn(`Failed to delete message`, ex); log.warn(`Failed to delete message`, ex);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter