From 66d7d481278b96febbf7c88b51f3e1bba43ebdf8 Mon Sep 17 00:00:00 2001 From: Max Worrall <96847801+Tsunami014@users.noreply.github.com> Date: Wed, 6 Dec 2023 20:40:24 +1100 Subject: [PATCH] Bing Chat joins the debugging fun --- .github/scripts/notifyDiscord.js | 57 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/.github/scripts/notifyDiscord.js b/.github/scripts/notifyDiscord.js index 5238b8b..87d50af 100644 --- a/.github/scripts/notifyDiscord.js +++ b/.github/scripts/notifyDiscord.js @@ -1,54 +1,57 @@ // Require the necessary discord.js classes -const { Client, Events, GatewayIntentBits } = require('discord.js'); +const { Client, Intents, Events } = require('discord.js'); const { token } = process.env.DISCORD_TOKEN; // Create a new client instance -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent] }); +const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); function processMessage(message, user) { - if (message.includes(`@${user}`)) { + if (message.mentions.users.has(user.id)) { return 'very'; } else { - return `hard.\n@${user} is working`; + return `hard.\n@${user.tag} is working`; } } -let previousMessage = null; +client.once(Events.Ready, async () => { + console.log(`Ready! Logged in as ${client.user.tag}`); -client.once(Events.ClientReady, async (readyClient) => { - console.log(`Ready! Logged in as ${readyClient.user.tag}`); - - const channel = readyClient.channels.cache.get('1181875066280091688'); // Replace with your channel ID + const channel = client.channels.cache.get('1181875066280091688'); // Replace with your channel ID if (channel) { console.log(`We got a channel :)`); try { - const messages = await channel.messages.fetch({ limit: 20 }); // Fetch up to 10 previous messages + const messages = await channel.messages.fetch({ limit: 10 }); // Fetch up to 10 previous messages // Iterate through the fetched messages - for (const [, message] of messages) { - if (message.content.includes('@')) { - // Check if the message contains an '@' mention + messages.forEach(message => { + if (message.mentions.users.size > 0) { + // Check if the message contains any user mentions const githubActor = process.env.GITHUB_ACTOR; console.log(`GitHub actor: ${githubActor}`); - const newMsg = processMessage(message.content, githubActor); - console.log(`Message: ${newMsg}`); - channel.send(newMsg) - .then(() => { - console.log(`Sent message: ${newMsg}`); - process.exit(); // Exit the process after sending the message - }) - .catch(error => { - console.error(`Error sending message: ${error}`); - process.exit(); // Exit the process after sending the message - }); + // Find the first mentioned user whose username matches the github actor + const mentionedUser = message.mentions.users.find(user => user.username === githubActor); + if (mentionedUser) { + // If a matching user is found, process the message + const newMsg = processMessage(message, mentionedUser); + console.log(`Message: ${newMsg}`); + channel.send(newMsg) + .then(() => { + console.log(`Sent message: ${newMsg}`); + process.exit(); // Exit the process after sending the message + }) + .catch(error => { + console.error(`Error sending message: ${error}`); + process.exit(); // Exit the process after sending the message + }); + } } - } + }); - // If no message with an '@' mention is found - console.log('No message with an "@" mention found.'); + // If no message with a user mention is found + console.log('No message with a user mention found.'); process.exit(); // Exit the process } catch (error) {