-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggle-role.js
136 lines (121 loc) · 4.95 KB
/
toggle-role.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const {
Client,
Interaction,
EmbedBuilder,
ApplicationCommandOptionType,
PermissionsBitField,
} = require("discord.js");
const fs = require("fs");
const path = require("path");
const logChannelsPath = path.join(__dirname, "../logChannels.json");
module.exports = {
/**
* @param {Client} client
* @param {Interaction} interaction
*/
callback: async (client, interaction) => {
try {
if (!interaction.guild) {
return interaction.reply({
content: "This command can only be used in a server.",
ephemeral: true,
});
}
const member = interaction.guild.members.cache.get(interaction.user.id);
if (!member || !member.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
return interaction.reply({
content: "You do not have permission to manage roles.",
ephemeral: true,
});
}
const userId = interaction.options.getString("userid");
const role = interaction.options.getRole("role");
const user = await client.users.fetch(userId).catch(() => null);
const targetMember = await interaction.guild.members.fetch(userId).catch(() => null);
if (!user || !targetMember) {
return interaction.reply({
content: "Could not find a member with the provided User ID.",
ephemeral: true,
});
}
const botMember = interaction.guild.members.cache.get(client.user.id);
if (role.position >= botMember.roles.highest.position) {
return interaction.reply({
content: "I cannot manage a role that is higher than or equal to my highest role.",
ephemeral: true,
});
}
if (role.position >= member.roles.highest.position && member.id !== interaction.guild.ownerId) {
return interaction.reply({
content: "You cannot manage a role that is higher than or equal to your highest role.",
ephemeral: true,
});
}
let action;
if (targetMember.roles.cache.has(role.id)) {
await targetMember.roles.remove(role);
action = "removed";
} else {
await targetMember.roles.add(role);
action = "assigned";
}
const embed = new EmbedBuilder()
.setTitle("Success")
.setColor("#4ea554")
.setDescription(
`Successfully **${action}** the role ${role} for **${user.tag}**.`
)
.setTimestamp()
.setFooter({
text: `Action performed by ${interaction.user.tag}`,
iconURL: interaction.user.displayAvatarURL({ dynamic: true }),
});
await interaction.reply({ embeds: [embed] });
let logChannels = {};
if (fs.existsSync(logChannelsPath)) {
logChannels = JSON.parse(fs.readFileSync(logChannelsPath, "utf-8"));
}
const logChannelId = logChannels[interaction.guild.id];
if (logChannelId) {
const logChannel = interaction.guild.channels.cache.get(logChannelId);
if (logChannel) {
const logEmbed = new EmbedBuilder()
.setColor("#4ea554")
.setTitle("Role Change Log")
.setDescription(
`**User:** <@${user.id}> (${user.id})\n` +
`**Action:** Role ${action}\n` +
`**Role:** <@&${role.id}> (${role.name})\n` +
`**Moderator:** <@${interaction.user.id}> (${interaction.user.id})`
)
.setTimestamp();
await logChannel.send({ embeds: [logEmbed] });
} else {
console.error("Log channel not found.");
}
}
} catch (error) {
console.error("Error managing role:", error);
await interaction.reply({
content: "An error occurred while managing the role.",
ephemeral: true,
});
}
},
name: "toggle-role",
description: "Toggle a server role for a user by their User ID.",
options: [
{
name: "userid",
description: "The User ID of the person you want to manage the role for.",
type: ApplicationCommandOptionType.String,
required: true,
},
{
name: "role",
description: "The role to toggle.",
type: ApplicationCommandOptionType.Role,
required: true,
},
],
};