Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#11) Ban function #63

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"autoposter",
"bannable",
"botstats",
"crey",
"davidzwa",
Expand All @@ -11,7 +12,8 @@
"Ludoviko",
"mkevenaar",
"nicknelson",
"topgg"
"topgg",
"unban"
],
"jest.jestCommandLine": "yarn run test:cov"
}
3 changes: 2 additions & 1 deletion src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getEnvConfig } from './shared.js';
import { Client, Collection, Intents } from 'discord.js';
import mongoose from 'mongoose';
import { AutoPoster } from 'topgg-autoposter';
import { resolveChannel, convertTime } from './tools/tools.js';
import { resolveChannel, convertTime, swapPages } from './tools/tools.js';
import { Constants } from './constants.js';
import { readdirSync } from 'fs';
import { GuildService } from './database/guild.service.js';
Expand Down Expand Up @@ -34,6 +34,7 @@ export function createDiscordClient() {
client.tools = {
convertTime,
resolveChannel,
swapPages,
};

return client;
Expand Down
108 changes: 108 additions & 0 deletions src/commands/moderation/ban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, Permissions } from 'discord.js';
import { botPermissions } from '../../tools/botPermissions.js';
import { BotColors } from '../../constants.js';

export const permission = new botPermissions()
.setUserPerms(Permissions.FLAGS.BAN_MEMBERS)
.setUserMessage("You don't have permission to ban members!")
.setBotPerms(Permissions.FLAGS.BAN_MEMBERS)
.setBotMessage("It seems that I don't have permission ban members!");

export const data = new SlashCommandBuilder()
.setName('ban')
.setDescription('Bans a Member from a Guild')
.addUserOption((user) => {
return user.setName('user').setDescription('User that needs to be banned').setRequired(true);
})
.addNumberOption((days) => {
return days
.setName('days')
.setDescription(
'Number of days of messages to delete, must be between 0 and 7, inclusive; default == 2'
)
.setRequired(false);
})
.addStringOption((reason) => {
return reason.setName('reason').setDescription('The reason for the ban').setRequired(false);
});

export async function execute(interaction) {
let user = interaction.options.getUser('user');
let days = interaction.options.getNumber('days');
let reason = interaction.options.getString('reason');

let banMember = await interaction.guild.members.fetch(user.id);

if (isNaN(days)) {
days = 2;
}

if (Number(days) >= 7) days = 7;
if (Number(days) <= 0) days = 0;

if (!reason) {
reason = `NO REASON PROVIDED`;
}

const memberPosition = banMember.roles?.highest.rawPosition;
const moderationPosition = interaction.member.roles?.highest.rawPosition;

if (moderationPosition <= memberPosition) {
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle('I cannot ban someone, who is above/equal to you');
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
}

if (!banMember.bannable) {
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle('The Member is not bannable, sorry!');
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
}

try {
if (!banMember.user.bot) {
let banMessage = new MessageEmbed()
.setColor(BotColors.default)
.setTitle(`You got banned by ${interaction.user.tag} from ${interaction.guild.name}`)
.setDescription(`Reason:\n> ${reason}`);
await banMember.user.send({ embeds: [banMessage] }).catch((error) => {
console.error(error);
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle(`Could not DM the Reason to: ${banMember.user.tag}`)
.setDescription(`${banMember.user}`);
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
});
}
} catch (error) {
console.error(error);
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle(`Could not DM the Reason to: ${banMember.user.tag}`)
.setDescription(`${banMember.user}`);
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
}

// Ban the member!
try {
banMember.ban({ days: days, reason: reason }).then(() => {
let banResult = new MessageEmbed()
.setColor(BotColors.default)
.setTitle(`Banned ${banMember.user.tag} (${banMember.user.id})`)
.setDescription(`Reason:\n> ${reason}`);

return interaction.reply({ embeds: [banResult] }).catch((error) => {
console.error(error);
});
});
} catch (error) {
console.error(error);
await interaction.reply({
content: `An issue has occurred while running the command. If this error keeps occurring please contact our development team.`,
ephemeral: true,
});
}
}
27 changes: 27 additions & 0 deletions src/commands/moderation/listbans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { Permissions } from 'discord.js';
import { botPermissions } from '../../tools/botPermissions.js';

export const permission = new botPermissions()
.setUserPerms(Permissions.FLAGS.BAN_MEMBERS)
.setUserMessage("You don't have permission to ban members!")
.setBotPerms(Permissions.FLAGS.BAN_MEMBERS)
.setBotMessage("It seems that I don't have permission ban members!");

export const data = new SlashCommandBuilder()
.setName('listbans')
.setDescription('Shows all Bans of the Guild');

export async function execute(interaction, client) {
let allBans = await interaction.guild.bans
.fetch()
.then((bans) =>
bans.map(
(ban) =>
`**${ban.user.username}**#${ban.user.discriminator} (\`${
ban.user.id
}\`)\n**Reason**:\n> ${ban.reason ? ban.reason : 'No Reason'}\n`
)
);
client.tools.swapPages(client, interaction, allBans, `All Bans of **${interaction.guild.name}**`);
}
49 changes: 49 additions & 0 deletions src/commands/moderation/unban.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed, Permissions } from 'discord.js';
import { botPermissions } from '../../tools/botPermissions.js';
import { BotColors } from '../../constants.js';

export const permission = new botPermissions()
.setUserPerms(Permissions.FLAGS.BAN_MEMBERS)
.setUserMessage("You don't have permission to ban members!")
.setBotPerms(Permissions.FLAGS.BAN_MEMBERS)
.setBotMessage("It seems that I don't have permission ban members!");

export const data = new SlashCommandBuilder()
.setName('unban')
.setDescription('Removes a ban from a Member in this Guild')
.addUserOption((user) => {
return user.setName('user').setDescription('User that needs to be unbanned').setRequired(true);
});

export async function execute(interaction) {
let user = interaction.options.getUser('user');

let bans = await interaction.guild.bans.fetch().catch(() => {});
if (!bans.map((b) => b?.user.id).includes(user.id)) {
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle('The User with that Id is not banned in this Server!');
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
}

try {
let banUser = bans.map((b) => b?.user).find((u) => u.id == user.id);
interaction.guild.members.unban(banUser ? banUser.id : user.id);
return interaction.reply({
embeds: [
new MessageEmbed()
.setColor(BotColors.default)
.setTitle(`Successfully Unbanned ${banUser.username}#${banUser.discriminator}`)
.setDescription(`Use: /listbans to see all ${bans.size - 1} Bans!`),
],
});
} catch (error) {
console.error(error);
let errorMessage = new MessageEmbed()
.setColor(BotColors.failed)
.setTitle(`Could unban: ${banMember.user.tag}`)
.setDescription(`${banMember.user}`);
return interaction.reply({ embeds: [errorMessage], ephemeral: true });
}
}
Loading