Skip to content

Commit

Permalink
added error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
vKxni committed Feb 15, 2022
1 parent f7ffbb8 commit d93b0f6
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 102 deletions.
14 changes: 10 additions & 4 deletions src/commands/Admin/Guild/blacklistuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports.run = async (interaction, utils) =>
{
try
{
const user = interaction.options.getUser("id");
const user = interaction.options.getUser("target");

const isBlacklisted = await User.findOne({ userID: user.id })
if(isBlacklisted) return interaction.reply({ content: `${emojis.error} | <@${user.id}> is already blacklisted.`, ephemeral: true })
Expand All @@ -41,9 +41,15 @@ module.exports.run = async (interaction, utils) =>
.setColor("RED")
.setTimestamp()

await user.send({ embeds: [embed] })
try {
await user.send({ embeds: [embed] });
} catch(error) {
interaction.reply({ content: `${emojis.success} | Successfully blacklisted ${user}\n\nError: Could not send message.`, ephemeral: true });
console.log(error)
return;
}

interaction.reply({ content: `${emojis.success} | Successfully blacklisted ${user}`, ephemeral: true })
interaction.followUp({ content: `${emojis.success} | Successfully blacklisted ${user}`, ephemeral: true })
}
catch (err)
{
Expand All @@ -59,4 +65,4 @@ module.exports.permissions = {
module.exports.data = new SlashCommandBuilder()
.setName("blacklistuser")
.setDescription("Blacklist a User from using the Bot")
.addUserOption((option) => option.setName("id").setDescription("Who should get blacklisted?").setRequired(true));
.addUserOption((option) => option.setName("target").setDescription("Who should get blacklisted?").setRequired(true));
11 changes: 9 additions & 2 deletions src/commands/Admin/Guild/whitelistuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ module.exports.run = async (interaction, utils) =>
.setColor("GREEN")
.setTimestamp()

await user.send({ embeds: [embed] });
interaction.reply({ content: `${emojis.success} | Successfully whitelisted ${user}`, ephemeral: true });
try {
await user.send({ embeds: [embed] });
} catch(error) {
interaction.reply({ content: `${emojis.success} | Successfully whitelisted ${user}\n\nWarning: Could not send message.`, ephemeral: true });
console.log(error)
return;
}

interaction.followUp({ content: `${emojis.success} | Successfully whitelisted ${user}`, ephemeral: true });
}
catch (err)
{
Expand Down
32 changes: 22 additions & 10 deletions src/commands/Moderation/addrole.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { CommandInteraction, Permissions, MessageEmbed, WebhookClient } = require("discord.js");

// Database queries
const Guild = require("../../models/Logging/logs");

// Configs
const emojis = require("../../../Controller/emojis/emojis");

module.exports.cooldown = {
length: 10000, /* in ms */
users: new Set()
Expand Down Expand Up @@ -45,14 +49,7 @@ module.exports.run = async (interaction, utils, guild) =>
.setDescription(`Dear ${target}, this is a notification that your profile got updated.\nServer: ${interaction.guild.name}\nRole: ${role}\nModerator: ${interaction.user.tag}`)
.setTimestamp()

// If the user has the role, remove it. Else add it.
if(target.roles.cache.has(role.id)) {
target.roles.remove(role.id)
interaction.reply({ embeds: [removed], ephemeral: true})
} else {
target.roles.add(role.id)
interaction.reply({ embeds: [added], ephemeral: true})
}

const logs = new MessageEmbed()
.setTitle("✅ | User Updated")
.setDescription(`User updated: ${target}\nRole updated: ${role}\nModerator: ${interaction.user.tag}`)
Expand All @@ -61,10 +58,25 @@ module.exports.run = async (interaction, utils, guild) =>
.setThumbnail(interaction.guild.iconURL({ dynamic: true }))
.setImage(interaction.guild.iconURL({ dynamic: true }))

target.send({ embeds: [userembed]})
// Adding role to the user, if the user already has the role, remove it.
try {
if(target.roles.cache.has(role.id)) {
target.roles.remove(role.id)
interaction.reply({ embeds: [removed], ephemeral: true})
} else {
target.roles.add(role.id)
interaction.reply({ embeds: [added], ephemeral: true})
}
await target.send({ embeds: [userembed]})
} catch(error) {
console.log(error);
return;
}


if (!guildQuery) return;
const guildQuery = await Guild.findOne({ id: interaction.guild.id });
if (!guildQuery) return;

if (guildQuery) {
const webhookid = guildQuery.webhookid;
const webhooktoken = guildQuery.webhooktoken;
Expand Down
148 changes: 90 additions & 58 deletions src/commands/Moderation/ban.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,113 @@
"use strict";

const { SlashCommandBuilder } = require("@discordjs/builders");
const { CommandInteraction, Permissions, MessageEmbed, WebhookClient } = require("discord.js");
const {
CommandInteraction,
Permissions,
MessageEmbed,
WebhookClient,
} = require("discord.js");

// Database queries
const Guild = require("../../models/Logging/logs");

// Configs
const emojis = require("../../../Controller/emojis/emojis");

module.exports.cooldown = {
length: 10000, /* in ms */
users: new Set()
length: 10000 /* in ms */,
users: new Set(),
};

/**
* Runs the command.
* @param {CommandInteraction} interaction The Command Interaciton
* @param {any} utils Additional util
*/
module.exports.run = async (interaction, utils) =>
{
try
{
const target = interaction.options.getMember("target");
const reason = interaction.options.getString('reason') || "No reason provided";

if(!target) return interaction.reply({ content: "This User is invalid"});

let bandm = new MessageEmbed()
.setColor("RED")
.setDescription(`You have been banned from **${interaction.guild.name}**\nModerator: ${interaction.user.tag}\nReason: ${reason}`)
await target.send({ embeds: [bandm] });

target.ban({ target });

let banmsg = new MessageEmbed()
.setColor("GREEN")
.setTitle(`${target.user.tag} Banned`)
.setDescription(`Banned ${target.user.tag} from ${interaction.guild.name}\nModerator: ${interaction.user.tag}\nReason: ${reason}`)
.setTimestamp()
.setColor("GREEN")
.setThumbnail(interaction.guild.iconURL({ dynamic: true }))
.setImage(interaction.guild.iconURL({ dynamic: true }))

const logs = new MessageEmbed()
.setTitle("✅ | User banned")
.setDescription(`User: ${target.user.tag}\nModerator: ${interaction.user.tag}\nReason: ${reason}`)
.setTimestamp()
.setColor("RED")

// Log Channel
const guildQuery = await Guild.findOne({ id: interaction.guild.id })

if(!guildQuery) return;
if(guildQuery) {
const webhookid = guildQuery.webhookid;
const webhooktoken = guildQuery.webhooktoken;

const webhookClient = new WebhookClient({ id: webhookid, token: webhooktoken });

webhookClient.send({ embeds: [logs]})
}

await interaction.reply({ embeds: [banmsg], ephemeral: true });
return;
module.exports.run = async (interaction, utils) => {
try {
const target = interaction.options.getMember("target");
const reason =
interaction.options.getString("reason") || "No reason provided";

if (!target) return interaction.reply({ content: "This User is invalid" });

let bandm = new MessageEmbed()
.setColor("RED")
.setDescription(
`You have been banned from **${interaction.guild.name}**\nModerator: ${interaction.user.tag}\nReason: ${reason}`
);

try {
await target.send({ embeds: [bandm] });
} catch (error) {
interaction.reply({
content: `${emojis.error} | Cannot send messages to this user.\nSuccessfully banned without sending warning through DMs.`,
ephemeral: true,
});
console.log(error);
return target.ban({ target });
}
catch (err)
{
return Promise.reject(err);

let banmsg = new MessageEmbed()
.setColor("GREEN")
.setTitle(`${target.user.tag} Banned`)
.setDescription(
`Banned ${target.user.tag} from ${interaction.guild.name}\nModerator: ${interaction.user.tag}\nReason: ${reason}`
)
.setTimestamp()
.setColor("GREEN")
.setThumbnail(interaction.guild.iconURL({ dynamic: true }))
.setImage(interaction.guild.iconURL({ dynamic: true }));

const logs = new MessageEmbed()
.setTitle("✅ | User banned")
.setDescription(
`User: ${target.user.tag}\nModerator: ${interaction.user.tag}\nReason: ${reason}`
)
.setTimestamp()
.setColor("RED");

// Log Channel
const guildQuery = await Guild.findOne({ id: interaction.guild.id });

if (!guildQuery) return;
if (guildQuery) {
const webhookid = guildQuery.webhookid;
const webhooktoken = guildQuery.webhooktoken;

const webhookClient = new WebhookClient({
id: webhookid,
token: webhooktoken,
});

webhookClient.send({ embeds: [logs] });
}

await interaction.reply({ embeds: [banmsg], ephemeral: true });
return;
} catch (err) {
return Promise.reject(err);
}
};

module.exports.permissions = {
clientPermissions: [Permissions.FLAGS.ADMINISTRATOR],
userPermissions: [Permissions.FLAGS.ADMINISTRATOR]
clientPermissions: [Permissions.FLAGS.ADMINISTRATOR],
userPermissions: [Permissions.FLAGS.ADMINISTRATOR],
};

module.exports.data = new SlashCommandBuilder()
.setName("ban")
.setDescription("Ban a user")
.addUserOption(option => option.setName("target").setDescription("Select the User to ban").setRequired(true))
.addStringOption(option => option.setName('reason').setDescription("Provide a Reason to Ban").setRequired(true))
.setName("ban")
.setDescription("Ban a user")
.addUserOption((option) =>
option
.setName("target")
.setDescription("Select the User to ban")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("reason")
.setDescription("Provide a Reason to Ban")
.setRequired(true)
);
17 changes: 14 additions & 3 deletions src/commands/Moderation/kick.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const {
// Database queries
const Guild = require("../../models/Logging/logs");

// Configs
const emojis = require("../../../Controller/emojis/emojis");

module.exports.cooldown = {
length: 10000 /* in ms */,
users: new Set(),
Expand All @@ -34,16 +37,24 @@ module.exports.run = async (interaction, utils) => {
.setDescription(
`You have been kicked from **${interaction.guild.name}**.\nReason: ${reason}`
);
await target.send({ embeds: [kickdm] });

target.kick({ target });
try {
await target.send({ embeds: [kickdm] });
} catch (error) {
interaction.reply({
content: `${emojis.error} | Cannot send messages to this user.\nSuccessfully kicked without sending warning through DMs.`,
ephemeral: true,
});
console.log(error);
return target.kick({ target });
}

let kickmsg = new MessageEmbed()
.setColor("GREEN")
.setTitle(`${target.user.tag} Kicked`)
.setDescription(
`Kicked ${target.user.tag} from ${interaction.guild.name}.\nReason: ${reason}`
)
);

const logs = new MessageEmbed()
.setTitle("✅ | Member kicked")
Expand Down
26 changes: 14 additions & 12 deletions src/commands/Moderation/removewarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const warnModel = require("../../models/Moderation/warning");
// Server logging
const Guild = require("../../models/Logging/logs");

// Configs
const emojis = require("../../../Controller/emojis/emojis");

module.exports.cooldown = {
length: 10000 /* in ms */,
users: new Set(),
Expand All @@ -25,23 +28,25 @@ module.exports.run = async (interaction, utils) => {
// Fetch the user
const target = interaction.options.getUser("target");

const embed = new MessageEmbed()
.setDescription(`✅ | Successfully deleted ${target}'s warning`)
.setColor("GREEN")
.setTimestamp()

// Find the data of the user and delete one warning, if there isn't any warning saved, return an error.
const data = await warnModel.findOne({
userId: target.id
})
if(!data) {
return interaction.reply({ content: `${target.tag} has no warnings`, ephemeral: true })
}
if(!data) return interaction.reply({ content: `${target.tag} has no warnings`, ephemeral: true })

data.delete();

interaction.reply({ embeds: [embed], ephemeral: true });

// Embed structure
const user = interaction.guild.members.cache.get(target.userId)

const embed = new MessageEmbed()
.setDescription(`✅ | Successfully deleted ${target}'s warning`)
.setColor("GREEN")
.setTimestamp()


const logs = new MessageEmbed()
.setTitle(`✅ | Removed warning`)
.setDescription(`User: ${target}\nModerator: ${interaction.user.tag}`)
Expand All @@ -66,10 +71,7 @@ module.exports.run = async (interaction, utils) => {
.setDescription(`Hey ${target}!\nYour warn got removed from by ${interaction.user.tag}.\nServer:${interaction.guild.name}`)
.setColor("GREEN")
.setTimestamp()

target.send({ embeds: [userlogs] })

interaction.reply({ embeds: [embed] });

} catch (err) {
return Promise.reject(err);
}
Expand Down
2 changes: 2 additions & 0 deletions src/commands/Moderation/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const {
Message,
} = require("discord.js");

// Database queries
const Guild = require("../../models/Report/report");

// Configs
const emojis = require("../../../Controller/emojis/emojis");

module.exports.cooldown = {
Expand Down
Loading

0 comments on commit d93b0f6

Please sign in to comment.