From 2e3b3729a997ba29b330c8ac61ad08ed9a263c9e Mon Sep 17 00:00:00 2001 From: Nate Goldsborough Date: Wed, 22 Sep 2021 21:43:50 -0500 Subject: [PATCH] increase logging and translate more code over --- index.ts | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index 5facca71..f1f9fbde 100644 --- a/index.ts +++ b/index.ts @@ -3,7 +3,7 @@ //This project will morph overtime //built for discord.js V.13.1.0 //Project started on December 15, 2020 -import DiscordJs, { Intents } from 'discord.js'; +import DiscordJs, { Intents, MessageEmbed } from 'discord.js'; import WOKCommands from 'wokcommands'; import mongoose from "mongoose"; import path from 'path'; @@ -11,7 +11,14 @@ import dotenv from 'dotenv'; import chalk from 'chalk'; dotenv.config(); import gateModel from './models/gate' +import piiModel from './models/pii'; +import guildModel from './models/guild'; import onReady from './actions/onReady'; +import counting from './functions/counting'; +import messageLog from './actions/messageLog'; +import docCreate from './actions/docCreate'; +import piiCreate from './actions/piiCreate'; +import guildUpdate from './actions/guildUpdate'; const client = new DiscordJs.Client({ @@ -103,13 +110,103 @@ client.on('ready', async () => { console.log(chalk.green.bold("Startup complete. Listening for input...")); }); -client.on("messageCreate", (message) => { +client.on("messageCreate", async (message) => { + const gate = await gateModel.findOne({ NAME: 'GATE' }) + + if (message.guild && gate.IGNORED_GUILDS.includes(message.guild.id)) return; if (message.author.bot) return; if (message.channel.type === "DM") { console.log(`${chalk.blue.bold(`DM`)} ${chalk.yellow(`[`+message.author.username+`]`)} ${chalk.grey.bold(`--`)} ${chalk.cyan(`[`+message.content+`]`)}`) } if (message.channel.type === "GUILD_TEXT"){ console.log(`${chalk.magenta.bold(`MESSAGE`)} ${chalk.green(`[`+message.channel.guild.name+`]`)} ${chalk.blue(`[`+message.channel.name+`]`)} ${chalk.yellow(`[`+message.author.username+`]`)} ${chalk.grey.bold(`--`)} ${chalk.cyan(`[`+message.content+`]`)}`); + try { + counting.count(message, client); // logic + messageLog.log(message); // log number of messages sent in each guild + } catch (e) { + console.log("Error on guild lookup. Maybe from a message sent in a DM to the bot") + } + } +}); + + +client.on('messageDelete', async (message) => { + const gate = await gateModel.findOne({ NAME: 'GATE' }) + if (message.author?.bot) return; + if (message.member?.user.bot) return; + if (message.channel.type != "DM") { + if (gate.IGNORED_GUILDS.includes(message.guild?.id)) return; + console.log(`${chalk.red.bold(`MESSAGE DELETED`)} ${chalk.green(`[`+message.channel.guild.name+`]`)} ${chalk.blue(`[`+message.channel.name+`]`)} ${chalk.yellow(`[`+message.author?.username+`]`)} ${chalk.grey.bold(`--`)} ${chalk.cyan(`[`+message.content+`]`)}`); + } +}); + + + +//actions to run when the bot joins a server +client.on("guildCreate", async (guild) => { + docCreate.event(guild, client); + piiCreate.event(guild, client); +}) + +//actions to run when the bot leaves a server +client.on("guildDelete", async (guild) => { + var d = new Date(); + const Embed = new MessageEmbed() + .setColor('#ff3505') + .setTitle(`I Left a Server`) + .setThumbnail(String(guild.iconURL() || 'https://cdn.discordapp.com/embed/avatars/0.png')) + .addFields([ + { name: 'Guild Creation Date:', value: guild.createdAt.toString() }, + { name: 'Guild Leave Date:', value: d.toString() }, + { name: 'Guild Name:', value: guild.name }, + { name: 'Guild ID:', value: guild.id }, + { name: 'Owner ID:', value: guild.ownerId }, + { name: 'Guild Member Count:', value: guild.memberCount.toString() }, + ]) + .setFooter(`Delivered in: ${client.ws.ping}ms | Antares Bot | ${process.env.VERSION}`, 'https://playantares.com/resources/icon.png'); + try { + await guildModel.findOneAndUpdate({ GUILD_ID: guild.id }, { $set: { GUILD_LEAVE_DATE: d.toString() } }, { new: true }); + client.users.fetch(String(process.env.BOT_OWNER_ID)).then(user => { + user.send({embeds: [Embed]}); + }) + } catch (e) { + console.log(e); + } +}); + +client.on('guildMemberAdd', async (member) => { + console.log(`${chalk.green.bold(`MEMBER JOINED`)} ${chalk.green(`[`+member.guild.name+`]`)} ${chalk.blue(`[`+member.user.username+`]`)}`); + try { + await guildModel.findOneAndUpdate({ GUILD_ID: member.guild.id }, { $set: { GUILD_MEMBER_COUNT: member.guild.memberCount } }, { new: true }); + } catch (e) { + console.log(e); + } +}) + +client.on('guildMemberRemove', async (member) => { + console.log(`${chalk.red.bold(`MEMBER LEFT`)} ${chalk.green(`[`+member.guild.name+`]`)} ${chalk.blue(`[`+member.user?.username+`]`)}`); + try { + await guildModel.findOneAndUpdate({ GUILD_ID: member.guild.id }, { $set: { GUILD_MEMBER_COUNT: member.guild.memberCount } }, { new: true }); + } catch (e) { + console.log(e); + } +}) + +client.on('guildUpdate', async (oldGuild, newGuild) => { + guildUpdate.update(oldGuild, newGuild, client) +}) + +client.on('channelDelete', async (channel) => { + + // check if the deleted channel is the counting channel and remove that channel from the db + if (channel.type === 'GUILD_TEXT') { + console.log(`${chalk.red.bold(`CHANNEL DELETED`)} ${chalk.green(`[`+channel.guild.name+`]`)} ${chalk.blue(`[`+channel.name+`]`)}`); + const req = await piiModel.findOne({ GUILD_ID: channel.guild?.id }) + if (channel.id === req.GUILD_COUNTING_CHANNEL_ID) { + req.GUILD_COUNTING_CHANNEL_ID = null; + req.save(); + console.log(`Database updated`) + } } });