generated from Mirasaki/discord.js-bot-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from Mirasaki/delayed-kill-feed
feat: delayed kill-feed
- Loading branch information
Showing
6 changed files
with
107 additions
and
54 deletions.
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
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,46 @@ | ||
const { serverConfig } = require('./cftClient'); | ||
const { isValidWebhookConfigMessage } = require('./webhooks'); | ||
|
||
const checkIsDelayedKillFeedMsg = async (msg) => { | ||
const { | ||
channelId, | ||
webhookId, | ||
author, | ||
cleanContent | ||
} = msg; | ||
const isKillMsg = cleanContent.indexOf(' got killed by ') >= 0; | ||
if (!isKillMsg) return false; | ||
|
||
for await (const cfg of serverConfig) { | ||
const { | ||
USE_KILL_FEED, | ||
KILL_FEED_DELAY, | ||
KILL_FEED_CHANNEL_ID, | ||
CFTOOLS_WEBHOOK_CHANNEL_ID, | ||
CFTOOLS_WEBHOOK_USER_ID | ||
} = cfg; | ||
const isTargetChannel = channelId === CFTOOLS_WEBHOOK_CHANNEL_ID; | ||
const isTargetUser = CFTOOLS_WEBHOOK_USER_ID === ( | ||
process.env.NODE_ENV === 'production' | ||
? webhookId | ||
: author.id | ||
); | ||
|
||
// Validate target ids/is webhook message | ||
if (!USE_KILL_FEED || !isTargetChannel || !isTargetUser) continue; | ||
|
||
// Resolve target channel | ||
const webhookTargetChannel = isValidWebhookConfigMessage(msg, KILL_FEED_CHANNEL_ID); | ||
if (!webhookTargetChannel) continue; | ||
|
||
// Send the notification | ||
await new Promise((resolve) => { | ||
setTimeout(async () => { | ||
const feedMsg = await webhookTargetChannel.send(cleanContent); | ||
resolve(feedMsg); | ||
}, KILL_FEED_DELAY * 1000); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { checkIsDelayedKillFeedMsg }; |
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,41 @@ | ||
const { stripIndents } = require('common-tags'); | ||
const { ChannelType } = require('discord.js'); | ||
|
||
const isValidWebhookConfigMessage = ( | ||
msg, | ||
targetChannelId | ||
) => { | ||
// Destructure from message | ||
const { id: msgId, | ||
guild } = msg; | ||
|
||
// Return if target can't be resolved | ||
const targetChannel = guild.channels.cache.get(targetChannelId); | ||
if (!targetChannel) { | ||
console.error(stripIndents` | ||
[${ msgId }] Can't send watch-list message, targetChannelId channel from config can't be resolved: ${ targetChannelId } | ||
`); | ||
return false; | ||
} | ||
|
||
// Return if target is not text based | ||
else if (!targetChannel.isTextBased()) { | ||
console.error(stripIndents` | ||
[${ msgId }] Can't send watch-list message, targetChannelId channel is NOT a text based channel | ||
`); | ||
return false; | ||
} | ||
|
||
// Don't allow stage channels - which have linked text chat | ||
else if (targetChannel.type === ChannelType.GuildStageVoice) { | ||
console.error(stripIndents` | ||
[${ msgId }] Can't send watch-list message, targetChannelId channel is NOT a valid text based channel | ||
`); | ||
return false; | ||
} | ||
|
||
// Configuration is valid | ||
else return targetChannel; | ||
}; | ||
|
||
module.exports = { isValidWebhookConfigMessage }; |