Skip to content

Commit

Permalink
Merge pull request #136 from Project-Coda/ban-alerts
Browse files Browse the repository at this point in the history
Add Kick and Ban Alerts
  • Loading branch information
ikifar2012 authored Mar 18, 2023
2 parents 14c24a8 + 3313d8e commit 4c9106d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
88 changes: 87 additions & 1 deletion embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,90 @@ var alert = function(message){
});
global.client.channels.cache.get(env.discord.logs_channel).send({ content: '🚨 Critical Alert 🚨' + '\n<@&' + env.discord.admin_role + '> <@&' + env.discord.mod_role + '>', embeds: [embed] });
};
module.exports = { setembed, sendError, log, alert };
var banAlert = async function(userbanning, userbanned, reason){
try {
var embed = setembed({
title: '🚨 Ban Alert 🚨',
description: `${userbanning} has banned ${userbanned} from the server.`,
thumbnail: {
url: `${userbanned.displayAvatarURL({ dynamic: true })}`,
},
fields: [
{
name: 'Reason',
value: `${reason}`,
},
{
name: 'User ID',
value: `${userbanned.id}`,
},
{
name: 'User Tag',
value: `${userbanned.tag}`,
},
{
name: 'Joined Discord',
value: `${userbanned.createdAt}`,
},
{
name: 'Mod ID',
value: `${userbanning.id}`,
},
{
name: 'Mod Tag',
value: `${userbanning.tag}`,
},
],
color: 0xe74c3c,
});
global.client.channels.cache.get(env.discord.logs_channel).send({ content: `Attention <@&${env.discord.mod_role}>, ${userbanning} has banned ${userbanned}`, embeds: [embed] });
}
catch (err) {
console.log(err);
sendError(err);
}
};
var kickAlert = async function(userkicking, userkicked, reason){
try {
var embed = setembed({
title: '🚨 Kick Alert 🚨',
description: `${userkicking} has kicked ${userkicked} from the server.`,
thumbnail: {
url: `${userkicked.displayAvatarURL({ dynamic: true })}`,
},
fields: [
{
name: 'Reason',
value: `${reason}`,
},
{
name: 'User ID',
value: `${userkicked.id}`,
},
{
name: 'User Tag',
value: `${userkicked.tag}`,
},
{
name: 'Joined Discord',
value: `${userkicked.createdAt}`,
},
{
name: 'Mod ID',
value: `${userkicking.id}`,
},
{
name: 'Mod Tag',
value: `${userkicking.tag}`,
},
],
color: 0xe74c3c,
});
global.client.channels.cache.get(env.discord.logs_channel).send({ content: `Attention <@&${env.discord.mod_role}>, ${userkicking} has kicked ${userkicked}`, embeds: [embed] });
}
catch (err) {
console.log(err);
sendError(err);
}
};
module.exports = { setembed, sendError, log, alert, banAlert, kickAlert };
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Client, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
const { Client, GatewayIntentBits, Partials, ActivityType, AuditLogEvent, Events } = require('discord.js');
const env = require('./env.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
Expand All @@ -13,7 +13,7 @@ const pkg = require('./package.json');
const CustomVC = require('./utilities/custom-vc.js');
const autorole = require('./utilities/autorole.js');
global.client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent],
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildModeration],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
global.client.login(env.discord.token);
Expand Down Expand Up @@ -269,6 +269,31 @@ global.client.on('interactionCreate', async interaction => {
}
});

global.client.on(Events.GuildAuditLogEntryCreate, async auditLog => {
// define audit log variables
const { action, executorId, targetId, reason } = auditLog;
// Check only for banned users.
if (action == AuditLogEvent.MemberBanAdd) {
// Ensure the executor is cached.
const user = await client.users.fetch(executorId);
// Ensure the banned guild member is cached.
const banedUser = await client.users.fetch(targetId);
const reasonformatted = reason || 'No reason provided';
// Now log the output!
await embedcreator.banAlert(user, banedUser, reasonformatted);
console.log(`${user.tag} banned ${banedUser.tag}! Reason: ${reasonformatted}`);
}
else if (action == AuditLogEvent.MemberKick) {
// Ensure the executor is cached.
const user = await client.users.fetch(executorId);
// Ensure the banned guild member is cached.
const kickedUser = await client.users.fetch(targetId);
const reasonformatted = reason || 'No reason provided';
// Now you can log the output!
await embedcreator.kickAlert(user, kickedUser, reasonformatted);
console.log(`${user.tag} kicked ${kickedUser.tag}! Reason: ${reasonformatted}`);
}
});

process.on('unhandledRejection', error => {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coda-utilities",
"version": "2.6.1",
"version": "2.7.0",
"description": "A general utilities bot for Coda",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4c9106d

Please sign in to comment.