Skip to content

Commit

Permalink
Made it check many previous messages in discord for the one with an @…
Browse files Browse the repository at this point in the history
… in it
  • Loading branch information
Tsunami014 committed Dec 6, 2023
1 parent b8f8580 commit 4907c9e
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions .github/scripts/notifyDiscord.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,46 @@ function processMessage(message, user) {

let previousMessage = null;

client.once(Events.ClientReady, (readyClient) => {
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

if (channel) {
console.log(`We gots a channel :)`);
channel.messages.fetch({ limit: 1 })
.then(messages => {
previousMessage = messages.last(); // Get the second-to-last message
console.log(`Previous message: ${previousMessage.content}`);
// Send a new message in the same channel
const oldMsg = previousMessage.content;
const githubActor = process.env.GITHUB_ACTOR;
console.log(`GitHub actor: ${githubActor}`);
const newMsg = processMessage(oldMsg, githubActor)
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
});
})
.catch(error => {
console.error(`Error fetching messages: ${error}`)
process.exit(); // Exit the process after sending the message
});
}
});

/*client.on('messageCreate', (message) => {
const commitAuthor = process.env.GITHUB_ACTOR;
if (message.content.includes(`${commitAuthor} is working`)) {
if (message.author.username === commitAuthor) {
// If the message contains the commit author and it's the same person, reply with 'really'
message.reply('really');
} else {
// If the message contains the commit author but it's a different person, reply with 'hard.\n{0} is working'
const newPerson = message.author.username;
message.reply(`hard.\n${newPerson} is working`);
console.log(`We got a channel :)`);

try {
const messages = await channel.messages.fetch({ limit: 20 }); // 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
const githubActor = process.env.GITHUB_ACTOR;
console.log(`GitHub actor: ${githubActor}`);
const newMsg = processMessage(message.content, githubActor);
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
});
break; // Exit the loop once a message with an '@' mention is found
}
}

// If no message with an '@' mention is found
console.log('No message with an "@" mention found.');
process.exit(); // Exit the process

} catch (error) {
console.error(`Error fetching messages: ${error}`);
process.exit(); // Exit the process after sending the message
}
}
});*/
});

client.login(token);

0 comments on commit 4907c9e

Please sign in to comment.