Files
.gitignore
index.js
package.json
README.md
Code
4 176
index.js
require('dotenv').config();
const {
Client,
GatewayIntentBits,
Partials,
} = require('discord.js');
const token = process.env.DISCORD_TOKEN;
const queueChannelId = process.env.QUEUE_CHANNEL_ID;
const alertChannelId = process.env.ALERT_CHANNEL_ID || queueChannelId;
const alertUserId = process.env.ALERT_USER_ID;
const queueBotId = process.env.QUEUE_BOT_ID;
if (!token) {
throw new Error('DISCORD_TOKEN is missing from .env');
}
if (!queueChannelId) {
throw new Error('QUEUE_CHANNEL_ID is missing from .env');
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Channel, Partials.Message],
});
// Stores the last known open/closed state so edited queue messages only alert once.
const queueStates = new Map();
function messageText(message) {
const embedText = message.embeds
.flatMap((embed) => [embed.title, embed.description])
.filter(Boolean)
.join('\n');
return `${message.content}\n${embedText}`;
}
function hasJoinQueueButton(message) {
return message.components
.flatMap((row) => row.components)
.some((component) => component.label?.trim().toLowerCase() === 'join queue');
}
function isOpenQueueMessage(message) {
if (message.channelId !== queueChannelId) return false;
if (queueBotId && message.author?.id !== queueBotId) return false;
const text = messageText(message);
return /tester\(s\)\s+available/i.test(text) && hasJoinQueueButton(message);
}
async function sendQueueAlert(message) {
const alertChannel = await client.channels.fetch(alertChannelId);
if (!alertChannel?.isTextBased() || typeof alertChannel.send !== 'function') {
throw new Error(`Alert channel ${alertChannelId} is not a text channel`);
}
const mention = alertUserId ? `<@${alertUserId}> ` : '';
await alertChannel.send({
content: `${mention}Tier tester queue is open: ${message.url}`,
allowedMentions: alertUserId ? { users: [alertUserId] } : { users: [] },
});
}
async function inspectQueueMessage(message) {
if (message.partial) {
try {
message = await message.fetch();
} catch (error) {
console.error('Could not fetch a partial queue message:', error.message);
return;
}
}
const open = isOpenQueueMessage(message);
const wasOpen = queueStates.get(message.id) ?? false;
queueStates.set(message.id, open);
if (open && !wasOpen) {
try {
await sendQueueAlert(message);
console.log(`Queue opened in message ${message.id}`);
} catch (error) {
console.error('Could not send queue alert:', error.message);
}
}
}
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
console.log(`Watching queue channel ${queueChannelId}`);
});
client.on('messageCreate', (message) => {
inspectQueueMessage(message).catch((error) => {
console.error('Queue inspection failed:', error);
});
});
client.on('messageUpdate', (_oldMessage, newMessage) => {
inspectQueueMessage(newMessage).catch((error) => {
console.error('Queue inspection failed:', error);
});
});
client.on('error', (error) => {
console.error('Discord client error:', error);
});
process.on('unhandledRejection', (error) => {
console.error('Unhandled promise rejection:', error);
});
client.login(token).catch((error) => {
console.error('Discord login failed:', error.message);
process.exitCode = 1;
});