From 0ec88c47c692d5759025d63b3db8ed1f77c3d624 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 7 Jul 2020 17:58:33 -0400 Subject: [PATCH] Send message on guild join/leave --- .env.example | 1 + Xiao.js | 40 ++++++++++++++++++++++++++++------ commands/util-public/report.js | 5 ++--- package.json | 2 +- structures/Client.js | 21 +++++++++++++++++- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 3c803d4e..e3bcd5ba 100644 --- a/.env.example +++ b/.env.example @@ -11,6 +11,7 @@ POSTER_ID= POSTER_TOKEN= POSTER_TIME= REPORT_CHANNEL_ID= +JOIN_LEAVE_CHANNEL_ID= # Emoji IDs GOLD_FISH_EMOJI_ID= diff --git a/Xiao.js b/Xiao.js index 6726171b..db9f1ada 100644 --- a/Xiao.js +++ b/Xiao.js @@ -1,7 +1,7 @@ require('dotenv').config(); const { XIAO_TOKEN, OWNERS, XIAO_PREFIX, INVITE } = process.env; const path = require('path'); -const { Intents } = require('discord.js'); +const { Intents, MessageEmbed } = require('discord.js'); const Client = require('./structures/Client'); const client = new Client({ commandPrefix: XIAO_PREFIX, @@ -115,12 +115,38 @@ client.on('message', async msg => { }); client.on('guildCreate', async guild => { - if (!guild.systemChannel || !guild.systemChannel.permissionsFor(client.user).has('SEND_MESSAGES')) return; - try { - const usage = client.registry.commands.get('help').usage(); - await guild.systemChannel.send(`Hi! I'm Xiao, use ${usage} to see my commands, yes?`); - } catch { - return; // eslint-disable-line no-useless-return + if (guild.systemChannel && guild.systemChannel.permissionsFor(client.user).has('SEND_MESSAGES')) { + try { + const usage = client.registry.commands.get('help').usage(); + await guild.systemChannel.send(`Hi! I'm Xiao, use ${usage} to see my commands, yes?`); + } catch { + // Nothing! + } + } + const joinLeaveChannel = await client.fetchJoinLeaveChannel(); + if (joinLeaveChannel) { + const embed = new MessageEmbed() + .setColor(0x7CFC00) + .setThumbnail(guild.iconURL({ format: 'png' })) + .setTitle(`Joined ${guild.name}!`) + .setFooter(`ID: ${guild.id}`) + .setTimestamp() + .addField('❯ Members', guild.memberCount); + await joinLeaveChannel.send({ embed }); + } +}); + +client.on('guildDelete', async guild => { + const joinLeaveChannel = await client.fetchJoinLeaveChannel(); + if (joinLeaveChannel) { + const embed = new MessageEmbed() + .setColor(0xFF0000) + .setThumbnail(guild.iconURL({ format: 'png' })) + .setTitle(`Left ${guild.name}...`) + .setFooter(`ID: ${guild.id}`) + .setTimestamp() + .addField('❯ Members', guild.memberCount); + await joinLeaveChannel.send({ embed }); } }); diff --git a/commands/util-public/report.js b/commands/util-public/report.js index f418b92e..8e545bc5 100644 --- a/commands/util-public/report.js +++ b/commands/util-public/report.js @@ -4,7 +4,6 @@ const { list } = require('../../util/Util'); const reasons = ['bug', 'feedback', 'suggestion']; const reasonColors = ['RED', 'GREEN', 'YELLOW']; const displayReasons = ['🐛 Bug Report', '📬 Feedback', '❓ Suggestion']; -const { REPORT_CHANNEL_ID } = process.env; module.exports = class ReportCommand extends Command { constructor(client) { @@ -40,9 +39,9 @@ module.exports = class ReportCommand extends Command { .setFooter(`ID: ${msg.author.id}`) .setTimestamp() .setColor(reasonColors[reason]); - if (REPORT_CHANNEL_ID) { + const channel = await this.client.fetchReportChannel(); + if (channel) { try { - const channel = await this.client.channels.fetch(REPORT_CHANNEL_ID); await channel.send({ embed }); } catch { await this.sendOwnerDM(embed); diff --git a/package.json b/package.json index fe14911d..564b5cf9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.12.4", + "version": "119.12.5", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/Client.js b/structures/Client.js index 6c7517ad..70db67d5 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -9,7 +9,16 @@ const MemePosterClient = require('./MemePoster'); const activities = require('../assets/json/activity'); const leaveMsgs = require('../assets/json/leave-messages'); const subreddits = require('../assets/json/meme'); -const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env; +const { null, null } = require('mathjs'); +const { + XIAO_WEBHOOK_ID, + XIAO_WEBHOOK_TOKEN, + POSTER_ID, + POSTER_TOKEN, + POSTER_TIME, + REPORT_CHANNEL_ID, + JOIN_LEAVE_CHANNEL_ID +} = process.env; module.exports = class XiaoClient extends CommandoClient { constructor(options) { @@ -69,4 +78,14 @@ module.exports = class XiaoClient extends CommandoClient { }); return buf; } + + fetchReportChannel() { + if (!REPORT_CHANNEL_ID) return null; + return this.channels.fetch(REPORT_CHANNEL_ID); + } + + fetchJoinLeaveChannel() { + if (!JOIN_LEAVE_CHANNEL_ID) return null; + return this.channels.fetch(JOIN_LEAVE_CHANNEL_ID); + } };