-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add loop messages feature #18
base: main
Are you sure you want to change the base?
Changes from 3 commits
6fdfc2d
a530413
9a193c9
97d91c3
344292b
f12e9db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* ------------------------------ | ||
* Loop Messages | ||
* ------------------------------ | ||
* The bot will keep a message as the last one in a channel, with an interval between each post, | ||
* to keep important information more visible to users | ||
* | ||
* How it works: | ||
* At startup and repeatedly by a given interval, for each loop message, the bot will: | ||
* 1. Get the target channel of the loop message | ||
* 2. Fetch the last 50 messages of the channel | ||
* 3. If the last message is already the message we want to loop, do nothing (we don't want to spam the channel) | ||
* 4. Delete all loop messages (so the channel isn't filled with bot messages) | ||
* 5. Send the loop message | ||
*/ | ||
|
||
const loopMessages = require("./messages.js"); | ||
|
||
// time between each check to send a new loop message | ||
const INTERVAL_TIME = 5 * 60 * 1000; // 5 minutes (ms) | ||
|
||
function getContent(loopMessage) { | ||
return loopMessage.trim(); | ||
} | ||
|
||
function isMessageFromBot(bot, message) { | ||
return message.author.id === bot.user.id; | ||
} | ||
|
||
function isLoopMessage(messageToCheck, loopMessage, bot) { | ||
return ( | ||
isMessageFromBot(bot, messageToCheck) && | ||
messageToCheck.content.trim() === getContent(loopMessage.content) | ||
); | ||
} | ||
|
||
async function sendLoopMessage(client, loopMessage) { | ||
const channel = client.channels.get(loopMessage.channelId); | ||
const channelMessages = await channel.fetchMessages({ limit: 50 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing if the loop message is past 50 messages, it probably doesn't matter if we delete it or not 😛 I'm not exactly sure how the reactiflux website is parsing the job posts, not sure if it would pick up the bots messages for the jobs channel specifically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah exactly.
I was gonna say that the bot only parses the messages that contain the tags ( |
||
|
||
// if the last message in the channel is from the bot, we don't need to send it again | ||
if (isLoopMessage(channelMessages.first(), loopMessage, client)) { | ||
return; | ||
} | ||
|
||
// search through all the last messages for the loop message, so we can delete it | ||
channelMessages.forEach(message => { | ||
if (isLoopMessage(message, loopMessage, client)) { | ||
message.delete(); | ||
} | ||
}); | ||
|
||
// now we can send the loop message in the channel | ||
channel.send(getContent(loopMessage.content)); | ||
} | ||
|
||
module.exports = { | ||
register: (client, logger) => { | ||
client.on("ready", () => { | ||
const sendLoopMessages = () => { | ||
loopMessages.forEach(message => { | ||
sendLoopMessage(client, message); | ||
}); | ||
}; | ||
|
||
setInterval(() => { | ||
sendLoopMessages(); | ||
}, INTERVAL_TIME); | ||
|
||
sendLoopMessages(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this to initially run it on startup? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, if the bot restarts for some reason then it already executes the function to update the channels |
||
|
||
logger.log("INI", "Registered Loop Messages"); | ||
}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am using a JS file instead of JSON because of the comments that are useful with |
||
{ | ||
channelId: "600037610005463050", // #another | ||
content: ` | ||
|
||
:arrows_clockwise: __PLEASE READ BEFORE POSTING__ :pushpin: | ||
|
||
Job post search: http://jobs.reactiflux.com/ | ||
|
||
Messages must start with [FORHIRE] or [HIRING]. | ||
Lead with the location of the position and include LOCAL, REMOTE, INTERN, VISA, etc. | ||
|
||
Please only post jobs once a week. | ||
|
||
Jobs are paid—unpaid and equity-only positions may not be posted here. | ||
|
||
` | ||
} | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 5 minutes an appropriate amount of time? Should it be longer? I'm not sure if we want this to be customization depending on the loop message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure, to be honest. The bot won't keep sending new messages if the last one is from it so a low interval time won't create spam, but the messages on #jobs aren't that active to hide the bot message if the interval time is too big. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial implementation had an interval field on each message, but I decided to remove it to avoid making it more complex than it is initially needed. We can add when we feel necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I increased the interval to 40 minutes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok now each message can have a different interval