Skip to content

Commit

Permalink
increase logging and translate more code over
Browse files Browse the repository at this point in the history
  • Loading branch information
nathen418 committed Sep 23, 2021
1 parent 8538076 commit 2e3b372
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
//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';
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({
Expand Down Expand Up @@ -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`)
}
}
});

Expand Down

0 comments on commit 2e3b372

Please sign in to comment.