-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Add self-destruct messages and cleanup 2. Auto-dedupe messages (delete duplicate messages from users) 3. add botask and botdoublemsg reactions 4. auto-deploy commands and emoji on startup
- Loading branch information
1 parent
a50a0d8
commit 6709cdf
Showing
20 changed files
with
245 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type * as TDiscord from "discord.js"; | ||
import { cleanupGuildOnInterval, getSelfDestructTime } from "./utils"; | ||
|
||
async function cleanup(guild: TDiscord.Guild) { | ||
const channels = Array.from(guild.channels.cache.values()).filter((ch) => | ||
ch.isText() | ||
) as Array<TDiscord.TextBasedChannel>; | ||
if (!guild.client.user) return; | ||
|
||
const botId = guild.client.user.id; | ||
const promises = []; | ||
|
||
for (const channel of channels) { | ||
for (const message of Array.from(channel.messages.cache.values())) { | ||
if (message.author.id === botId) { | ||
const timeToSelfDestruct = getSelfDestructTime(message.content); | ||
if ( | ||
typeof timeToSelfDestruct === "number" && | ||
message.createdAt.getTime() + timeToSelfDestruct < Date.now() | ||
) { | ||
promises.push(message.delete()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Promise.all(promises); | ||
} | ||
|
||
async function setup(client: TDiscord.Client) { | ||
// prime the message cache for all channels | ||
// this is important for situations when the bot gets restarted after | ||
// it had just sent a self-destruct chat | ||
await Promise.all( | ||
Array.from(client.guilds.cache.values()).map(async (guild) => { | ||
const channels = Array.from(guild.channels.cache.values()).filter((ch) => | ||
ch.isText() | ||
) as Array<TDiscord.TextBasedChannel>; | ||
return Promise.all( | ||
Array.from(channels.values()).map((channel) => { | ||
return channel.messages.fetch({ limit: 30 }); | ||
}) | ||
); | ||
}) | ||
); | ||
|
||
cleanupGuildOnInterval(client, (guild) => cleanup(guild), 5000); | ||
} | ||
|
||
export { setup }; |
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,76 @@ | ||
import type * as TDiscord from "discord.js"; | ||
import { getMessageLink, sendBotMessageReply } from "./utils"; | ||
|
||
const sixHours = 1000 * 60 * 60 * 6; | ||
|
||
function isLink(text: string) { | ||
try { | ||
// eslint-disable-next-line no-new | ||
new URL(text.trim()); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async function dedupeMessages(message: TDiscord.Message) { | ||
const { guild } = message; | ||
if (!guild) return; | ||
|
||
if (message.author.id === message.client.user?.id) return; // ignore the bot | ||
if (!message.channel.isText()) return; // ignore non-text channels | ||
if (message.content.length < 50) return; // ignore short messages | ||
if (isLink(message.content)) return; // ignore links, gifs/blog posts/etc. | ||
|
||
const channels = Array.from( | ||
guild.channels.cache.filter((ch) => ch.isText()).values() | ||
) as Array<TDiscord.TextBasedChannel>; | ||
|
||
function msgFilter(msg: TDiscord.Message) { | ||
return ( | ||
msg.id !== message.id && // not the EXACT same message | ||
msg.author.id !== msg.client.user?.id && // not from the bot | ||
msg.author.id === message.author.id && // from the same user | ||
new Date().getTime() - msg.createdAt.getTime() < sixHours && // within the last six hours | ||
msg.content.length > 50 && // longer than 50 characters | ||
msg.content.toLowerCase() === message.content.toLowerCase() // same content | ||
); | ||
} | ||
|
||
let duplicateMessage; | ||
for (const channel of channels) { | ||
duplicateMessage = Array.from(channel.messages.cache.values()).find( | ||
msgFilter | ||
); | ||
if (duplicateMessage) break; | ||
} | ||
|
||
if (duplicateMessage) { | ||
await message.delete(); | ||
const duplicateMessageLink = getMessageLink(duplicateMessage); | ||
|
||
sendBotMessageReply( | ||
message, | ||
` | ||
Hi ${message.author}, I deleted a message you just posted because it's a duplicate of this one: <${duplicateMessageLink}>. Please give it time for users to respond to your first post. | ||
If you think your message is better suited in another channel please delete the first one then repost. Thank you. | ||
`.trim() | ||
); | ||
} | ||
} | ||
|
||
function setup(client: TDiscord.Client) { | ||
// prime the message cache for relevant channels | ||
const guild = client.guilds.cache.find(({ name }) => name === "KCD"); | ||
if (!guild) return; | ||
const channels = Array.from( | ||
guild.channels.cache.filter((ch) => ch.isText()).values() | ||
) as Array<TDiscord.TextBasedChannel>; | ||
for (const channel of channels) { | ||
// ignore the returned promise. Fire and forget. | ||
void channel.messages.fetch({ limit: 30 }); | ||
} | ||
} | ||
|
||
export { dedupeMessages as handleNewMessage, setup }; |
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,11 @@ | ||
import type * as TDiscord from "discord.js"; | ||
import * as dedupeMessages from "./dedupe"; | ||
import * as selfDestruct from "./cleanup-self-destruct-messages"; | ||
|
||
function setup(client: TDiscord.Client) { | ||
client.on("message", dedupeMessages.handleNewMessage); | ||
dedupeMessages.setup(client); | ||
selfDestruct.setup(client); | ||
} | ||
|
||
export { setup }; |
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 @@ | ||
export * from "../utils"; |
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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
|
@@ -7,4 +7,6 @@ | |
|
||
set -ex | ||
npx prisma migrate deploy | ||
node ./scripts/deploy-commands.js | ||
node ./scripts/deploy-emoji.js | ||
npm run start |