Skip to content

Commit

Permalink
Merge pull request #362 from Project-Coda/dev
Browse files Browse the repository at this point in the history
Add Coda Strikes
  • Loading branch information
ikifar2012 authored Apr 14, 2024
2 parents f7a5ecd + b9868ab commit 0985a89
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/eslint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
- name: Install eslint
run: npm install -g eslint
- name: Run eslint
run: eslint --ext .js .
run: eslint .
61 changes: 61 additions & 0 deletions commands/cban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const embedcreator = require('../embed.js');
const { ban } = require('../utilities/ban.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('cban')
.setDescription('Bans a user from the server')
.addUserOption(option =>
option.setName('user')
.setDescription('User you want to ban')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Reason for the ban')
.setRequired(true)),

async execute(interaction) {
try {
// Limit command to Founders and Mods
if (!(interaction.member.roles.cache.has(env.discord.admin_role) || interaction.member.roles.cache.has(env.discord.mod_role))) {
global.client.channels.cache.get(env.discord.logs_channel).send({
embeds: [embedcreator.setembed(
{
title: 'Incident Detected',
description: `${interaction.member.user} tried to use the cban command but did not have the correct role.`,
color: 0xe74c3c,
},
)],
},
);
return interaction.reply({
embeds: [embedcreator.setembed(
{
title: 'Incident Reported',
description: 'You do not have permission to use this command. This incident has been reported.',
color: 0xe74c3c,
},
),
], ephemeral: true,
});
}
const user = interaction.options.getUser('user');
const reason = interaction.options.getString('reason');
const userID = user.id;
await ban(userID, reason);
const embed = await embedcreator.setembed(
{
title: 'User Banned',
description: `User ${user.username} has been banned for ${reason}`,
color: 0xe74c3c,
},
);
await interaction.reply({ embeds: [embed], ephemeral: true });
}
catch (err) {
console.log(err);
embedcreator.sendError(err);
}
},
};
4 changes: 2 additions & 2 deletions embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ var kickAlert = async function(userkicking, userkicked, reason){
sendError(err);
}
};
var mentionAlert = async function(message){
var mentionAlert = async function(message, strikenum){
try {
var embed = setembed({
title: '🚨 Mass Mention Alert 🚨',
description: `${message.author} has mass mentioned in ${message.channel}`,
description: `${message.author} has mass mentioned in ${message.channel}, strike ${strikenum}`,
thumbnail: {
url: `${message.member.displayAvatarURL({ dynamic: true })}`,
},
Expand Down
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const CustomVC = require('./utilities/custom-vc.js');
const autorole = require('./utilities/autorole.js');
const vctools = require('./utilities/vc-tools.js');
const { checkMention } = require('./utilities/message-filter.js');
const nodecron = require('node-cron');
global.client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.DirectMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildModeration],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
Expand Down Expand Up @@ -51,6 +52,9 @@ global.client.once('ready', async () => {
await db.query('CREATE TABLE IF NOT EXISTS custom_vc (user_id VARCHAR(255) PRIMARY KEY, channel_id VARCHAR(255))');
// create auto role table if it doesn't exist
await db.query('CREATE TABLE IF NOT EXISTS auto_role (role_id VARCHAR(255) PRIMARY KEY)');
// create coda strikes table if it doesn't exist
// await db.query('DROP TABLE IF EXISTS coda_strikes');
await db.query('CREATE TABLE IF NOT EXISTS coda_strikes (user_id VARCHAR(255) PRIMARY KEY, strikes INT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)');
db.end();
}
)();
Expand Down Expand Up @@ -304,6 +308,21 @@ global.client.on(Events.GuildAuditLogEntryCreate, async auditLog => {
}
});

// clear coda strkes older than an hour
nodecron.schedule('0 0 * * *', async () => {
try {
db = await mariadb.getConnection();
await db.query('DELETE FROM coda_strikes WHERE timestamp < DATE_SUB(NOW(), INTERVAL 1 HOUR)');
db.end();
embedcreator.log('Coda strikes older than an hour have been deleted.');
}
catch (error) {
console.error(error);
embedcreator.sendError(error);
}
},
);

process.on('unhandledRejection', error => {
console.error(error);
// send error to discord
Expand Down
37 changes: 35 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coda-utilities",
"version": "3.4.0",
"version": "3.5.0",
"description": "A general utilities bot for Coda",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,6 +30,7 @@
"figlet": "^1.7.0",
"lodash": "^4.17.21",
"mariadb": "^3.3.0",
"node-cron": "^3.0.3",
"node-fetch": "^2.7.0",
"openai": "^4.33.0"
}
Expand Down
42 changes: 42 additions & 0 deletions utilities/ban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const mariadb = require('../db.js');
const embedcreator = require('../embed.js');
const env = require('../env.js');
async function ban(userID, message) {
try {
// if the user has 3 strikes, delete them from the database and ban them
db = await mariadb.getConnection();
await db.query('DELETE FROM coda_strikes WHERE user_id = ?', [userID]);
db.end();
}
catch (err) {
console.log(err);
}
// ban the user
const user = await global.client.users.fetch(userID);
try {
embed = await embedcreator.setembed(
{
title: 'You have been banned from the server',
description: 'If you believe this is in error, fill out the fourm and we will review your case, within 24 hours.',
color: 0xe74c3c,
fields: [
{
name: 'Ban Appeal Form',
value: env.discord.ban_appeal_form,
},
],
},
);
await user.send({ embeds: [embed] });
}
catch (err) {
console.log(err);
embedcreator.sendError(err);
}
const guild = await global.client.guilds.fetch(env.discord.guild);
const member = await guild.members.fetch(userID);
await member.ban({ deleteMessageSeconds: 604800, reason: message });
}
module.exports = {
ban,
};
71 changes: 61 additions & 10 deletions utilities/message-filter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
const embedcreator = require('../embed.js');
const env = require('../env.js');
const { ban } = require('./ban.js');
const mariadb = require('../db.js');
async function checkMention(message) {
try {
if (message.member.roles.cache.has(env.discord.admin_role) || message.member.roles.cache.has(env.discord.mod_role)) return;
if (message.content.includes('@everyone') || message.content.includes('@here')) {
embed = embedcreator.setembed(
{
title: 'Unauthorized Mention Detected',
description: 'You have attempted to mention everyone or here that behavior is not allowed.',
color: 0xe74c3c,
},
),
await embedcreator.mentionAlert(message);
reply = await message.reply({ embeds: [embed], ephemeral: true });
message.delete();
// increment the user's strikes
const reason = 'Mention Spam';
const strikes = await incrementStrikes(message.author.id, reason);
await embedcreator.mentionAlert(message, strikes);
const embed = await embedcreator.setembed(
{
title: 'Unauthorized Mention Detected',
description: 'You have attempted to mention everyone or here that behavior is not allowed.',
color: 0xe74c3c,
fields: [{
name: 'Strike',
value: strikes,
},
],
},
),
reply = await message.reply({ embeds: [embed], ephemeral: true });
await message.delete();
// wait 5 seconds then delete the reply
setTimeout(() => {
reply.delete();
Expand All @@ -26,6 +36,47 @@ async function checkMention(message) {
embedcreator.sendError(err);
}
}
async function incrementStrikes(userID, reason) {
try {
try {
// get the user's current strikes
db = await mariadb.getConnection();
strikes = await db.query('SELECT strikes FROM coda_strikes WHERE user_id = ?', [userID]);
db.end();
}
catch (err) {
console.log(err);
embedcreator.sendError(err);
}
// if the user has no strikes, add them to the database
if (strikes[0] == undefined) {
newStrikes = 1;
db = await mariadb.getConnection();
await db.query('INSERT INTO coda_strikes (user_id, strikes) VALUES (?, ?)', [userID, newStrikes]);
db.end();
}
else {
// add one to coda strikes for user
newStrikes = strikes[0].strikes + 1;
// update the user's strikes
console.log(newStrikes);
db = await mariadb.getConnection();
await db.query('UPDATE coda_strikes SET strikes = ? WHERE user_id = ?', [newStrikes, userID]);
db.end();

}
if (newStrikes >= 3) {
await ban(userID, reason);
}
// return the new strike count
return newStrikes;
}
catch (err) {
console.log(err);
embedcreator.sendError(err);
}
}
module.exports = {
checkMention,
incrementStrikes,
};

0 comments on commit 0985a89

Please sign in to comment.