diff --git a/src/index.ts b/src/index.ts index fe5a3bd..9737021 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,10 +21,23 @@ discord.on("ready", async event => { console.log(`Connected to Discord as ${event.user.tag} (${event.user.id})`); }); +const channelsRunning: DiscordApi.Collection = new DiscordApi.Collection(); + discord.on("messageCreate", async message => { if (message.author.bot) return; if (!message.mentions.has(message.client.user)) return; + const messagesForChannel = channelsRunning.ensure(message.channelId, () => {return [] as DiscordApi.Message[];} ); + const shouldStart = messagesForChannel.length == 0; + messagesForChannel.push(message); + if (shouldStart) + onMessage(message.channelId); +}); + +async function onMessage(channel: string) { + const channelQueue = channelsRunning.get(channel) as DiscordApi.Message[]; + const message = channelQueue.at(0) as DiscordApi.Message; + try { let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false }); @@ -73,6 +86,12 @@ discord.on("messageCreate", async message => { } }); } -}); + + channelQueue.shift(); + if (channelQueue.length == 0) + channelsRunning.delete(channel); + else + onMessage(channel); +} discord.login(config.tokens.Discord);