From 205a93dfb21f4bbe647d4af224caeb9da12ac441 Mon Sep 17 00:00:00 2001 From: janderedev Date: Wed, 5 Jan 2022 23:20:19 +0100 Subject: [PATCH] send welcome message when added to server --- src/bot/modules/event_handler.ts | 41 ++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/bot/modules/event_handler.ts b/src/bot/modules/event_handler.ts index 7b9ad72..d2460b4 100644 --- a/src/bot/modules/event_handler.ts +++ b/src/bot/modules/event_handler.ts @@ -2,14 +2,16 @@ import { ulid } from "ulid"; import { client } from "../.."; import Infraction from "../../struct/antispam/Infraction"; import InfractionType from "../../struct/antispam/InfractionType"; -import { storeInfraction } from "../util"; +import logger from "../logger"; +import { hasPermForChannel, storeInfraction } from "../util"; +import { DEFAULT_PREFIX } from "./command_handler"; // Listen to system messages client.on('message', async message => { if (typeof message.content != 'object') return; let sysMsg = message.asSystemMessage; - + switch(sysMsg.type) { case 'user_kicked': case 'user_banned': @@ -41,3 +43,38 @@ client.on('message', async message => { case 'user_left' : break; } }); + +// Send a message when added to a server +client.on('member/join', (member) => { + if (member._id.user != client.user?._id) return; + + let url = `https://rvembed.janderedev.xyz/embed` + + `?title=${encodeURIComponent('Hi there, thanks for adding me!')}` + + `&description=${encodeURIComponent(`My prefix is "${DEFAULT_PREFIX}", ` + + `but you can also @mention me instead.\nCheck out ${DEFAULT_PREFIX}help to get started!`)}` + + `&link=${encodeURIComponent(`/bot/${client.user._id}`)}` + + `&redir=${encodeURIComponent(`https://github.com/janderedev/revolt-automod`)}` + + `&color=${encodeURIComponent("#ff6e6d")}` + + `&image=${encodeURIComponent(client.user.generateAvatarURL({ size: 128 }))}` + + `&image_large=false`; + + if (!member.server) return; + + let channels = member.server.channels.filter( + c => c + && c.channel_type == 'TextChannel' + && hasPermForChannel(member, c, 'SendMessage') + && hasPermForChannel(member, c, 'EmbedLinks') + ); + + // Attempt to find an appropriate channel, otherwise use the first one available + let channel = channels.find(c => c?.name?.toLowerCase() == 'welcome') + || channels.find(c => c?.name?.toLowerCase() == 'general') + || channels.find(c => c?.name?.toLowerCase() == 'bots') + || channels.find(c => c?.name?.toLowerCase() == 'spam') + || channels[0]; + + if (!channel) return logger.debug('Cannot send hello message: No suitable channel found'); + channel.sendMessage(`[:wave:](${url} "Hi there!")`) + .catch(e => logger.debug('Cannot send hello message: ' + e)); +});