Skip to content

Commit

Permalink
Merge pull request #23 from saiteja-madha/bug-fixes
Browse files Browse the repository at this point in the history
warn context permissions, unmute command fix
  • Loading branch information
saiteja-madha authored Sep 24, 2021
2 parents bc3365c + 097fcee commit 2bfa33c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/commands/moderation/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = class Warn extends Command {
},
contextMenu: {
enabled: true,
userPermissions: ["KICK_MEMBERS"],
type: "USER",
},
});
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = class Warn extends Command {
async contextRun(interaction) {
const target = (await interaction.guild.members.fetch(interaction.targetId)) || interaction.member;
if (!canInteract(interaction.member, target, "warn")) {
interaction.followUp("Missing permission to warn this member");
return interaction.followUp("Missing permission to warn this member");
}
let status = await addModAction(interaction.member, target, "", "WARN");
if (status) interaction.followUp(`${target.user.tag} is warned by ${interaction.member.user.tag}`);
Expand Down
16 changes: 16 additions & 0 deletions src/events/interactions/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module.exports = async (client, interaction) => {
// get the slash command
const command = client.slashCommands.get(interaction.commandName);

// Permission check
if (!interaction.member.permissions.has(command.slashCommand.userPermissions || [])) {
interaction.reply({
content: `You do not have enough permissions to use this command`,
ephemeral: true,
});
}

// cooldown check
if (command.cooldown > 0) {
const remaining = command.getRemainingCooldown(interaction.member.id);
Expand Down Expand Up @@ -49,6 +57,14 @@ module.exports = async (client, interaction) => {
// get the context menu
const command = client.contextMenus.get(interaction.commandName);

// Permission check
if (!interaction.member.permissions.has(command.contextMenu.userPermissions || [])) {
return interaction.reply({
content: `You do not have enough permissions to use this command`,
ephemeral: true,
});
}

// cooldown check
if (command.cooldown > 0) {
const remaining = command.getRemainingCooldown(interaction.user.id);
Expand Down
4 changes: 2 additions & 2 deletions src/schemas/modlog-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ module.exports = {
member_id: targetId,
type: "MUTE",
"data.current": true,
}),
}).lean({ defaults: true }),

removeMutes: async (guildId, targetId) =>
Model.updateOne(
Model.updateMany(
{
guild_id: guildId,
member_id: targetId,
Expand Down
37 changes: 10 additions & 27 deletions src/utils/modUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,12 @@ async function muteTarget(issuer, target, reason) {
const previousMute = await getMuteInfo(issuer.guild.id, target.id);

if (previousMute) {
// maybe role was manually removed (so delete mute data)
if (!hasMutedRole(target)) await removeMutes(target.guild.id, target.id);
else {
if (previousMute.data?.isPermanent) {
return "ALREADY_MUTED"; // already muted
}

if (previousMute.data?.expires) {
await removeMutes(target.guild.id, target.id);
}
}
if (previousMute.data?.isPermanent && hasMutedRole(target)) return "ALREADY_MUTED"; // already muted
await removeMutes(target.guild.id, target.id); // remove existing tempmute data
}

try {
await target.roles.add(mutedRole);
if (!hasMutedRole(target)) await target.roles.add(mutedRole);
await logModeration(issuer, target, reason, "Mute", { isPermanent: true });
return true;
} catch (ex) {
Expand All @@ -236,7 +227,7 @@ async function unmuteTarget(issuer, target, reason) {
let mutedRole = getRoleByName(issuer.guild, "muted");
try {
await removeMutes(issuer.guild.id, target.id);
if (target.roles.cache.has(mutedRole)) await target.roles.remove(mutedRole);
if (hasMutedRole(target)) await target.roles.remove(mutedRole);
await logModeration(issuer, target, reason, "Unmute");
return true;
} catch (ex) {
Expand Down Expand Up @@ -379,31 +370,23 @@ async function logModeration(issuer, target, reason, type, data = {}) {
async function addModAction(issuer, target, reason, action) {
switch (action) {
case "WARN":
await warnTarget(issuer, target, reason);
break;
return await warnTarget(issuer, target, reason);

case "MUTE":
await muteTarget(issuer, target, reason);
break;
return await muteTarget(issuer, target, reason);

case "UNMUTE":
await unmuteTarget(issuer, target, reason);
break;
return await unmuteTarget(issuer, target, reason);

case "KICK":
await kickTarget(issuer, target, reason);
break;
return await kickTarget(issuer, target, reason);

case "SOFTBAN":
await softbanTarget(issuer, target, reason);
break;
return await softbanTarget(issuer, target, reason);

case "BAN":
await banTarget(issuer, target, reason);
break;
return await banTarget(issuer, target, reason);
}

return true;
}

module.exports = {
Expand Down

0 comments on commit 2bfa33c

Please sign in to comment.