-
Notifications
You must be signed in to change notification settings - Fork 40
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
Open
rafaelalmeidatk
wants to merge
6
commits into
reactiflux:main
Choose a base branch
from
rafaelalmeidatk:feature/msg-loop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fdfc2d
Add loop messages feature
rafaelalmeidatk a530413
Better description for the module
rafaelalmeidatk 9a193c9
Add #jobs initial text
rafaelalmeidatk 97d91c3
Ignore messages from bots
rafaelalmeidatk 344292b
Increase the interval to 40 minutes
rafaelalmeidatk f12e9db
Add individual intervals for each message
rafaelalmeidatk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
// if the interval is not specified in the msg object, then we will use this one | ||
const DEFAULT_INTERVAL_TIME = 60 * 60 * 1000; // 1 hour (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 // we don't need to worry about deleting old messages | ||
}); | ||
|
||
// 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", () => { | ||
loopMessages.forEach(message => { | ||
sendLoopMessage(client, message); // initial run on startup | ||
|
||
const interval = message.interval || DEFAULT_INTERVAL_TIME; | ||
|
||
setInterval(() => { | ||
sendLoopMessage(client, message); | ||
}, interval); | ||
}); | ||
|
||
logger.log("INI", "Registered Loop Messages"); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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 | ||
interval: 40 * 60 * 1000, // 40 minutes (ms) | ||
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. | ||
|
||
` | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this strictly 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.
Yes? What benefit would we get if we had bots messaging/triggering each other?
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.
This is necessary because the bot may post a message in the
#jobs
channel that does not contain these tags so it would receive a warning from itself