Skip to content

Commit 65f32ef

Browse files
BigbugmanAcdSoftCoDomin0de
authored
Dev/course channel fix 3 (#158)
* course-chat-fix-2, make new roles where there arent any in /rolespermoverride and also remove individual permission overwrites, and also updates to /course join to catch when there doesn't exist a role for a course and also /course leave to attempt to remove individual permission overwrites along with removing course role PLEASE TEST THOROUGHLY AND INSPECT CODE THOROUGHLY. I wrote this ASAP and haven't got time to properly review it. * Modified functions for course channel fixes * Check for if user only has read perms for overwrite replacing and fix looping over members * Fix to functionality of iteration and editing roles * Fix to initial working state * Fix linting * Additional fixes to ordering and component operations to remove role and individual permissions correctly * Appended filter for both view only perms and view + send messages perms --------- Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> Co-authored-by: Wolfdragon24 <dragonbomber24@gmail.com>
1 parent 9f7c78d commit 65f32ef

File tree

2 files changed

+103
-58
lines changed

2 files changed

+103
-58
lines changed

commands/course.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { SlashCommandBuilder } = require("@discordjs/builders");
2-
const { Permissions } = require("discord.js");
32

4-
const MODERATION_REQUEST_CHANNEL = 824506830641561600;
53
const COMMAND_JOIN = "join";
64
const COMMAND_LEAVE = "leave";
75

@@ -54,6 +52,9 @@ const is_valid_course = (course) => {
5452
);
5553
};
5654

55+
const in_overwrites = (overwrites, id) =>
56+
[1024n, 3072n].includes(overwrites.find((v, k) => k === id)?.allow?.bitfield);
57+
5758
module.exports = {
5859
data: new SlashCommandBuilder()
5960
.setName("course")
@@ -128,8 +129,9 @@ module.exports = {
128129
});
129130
}
130131

132+
// if there isn't a role that matches the name of the course
131133
return await interaction.reply({
132-
content: `✅ | End of command - ${course_with_alias}.`,
134+
content: `There doesn't exist a role for \`${course_with_alias}\`. If you believe there should be, please inform a member of the Discord Bot team or staff.`,
133135
ephemeral: true,
134136
});
135137
} else if (interaction.options.getSubcommand() === COMMAND_LEAVE) {
@@ -143,24 +145,65 @@ module.exports = {
143145
});
144146
}
145147

146-
// First, let's see if there's a role that matches the name of the course
148+
// Check and fetch a channel corresponding to input
149+
const channel = await interaction.guild.channels.cache.find(
150+
(c) => c.name.toLowerCase() === course.toLowerCase(),
151+
);
152+
153+
if (channel === undefined) {
154+
return await interaction.reply({
155+
content: `❌ | The course chat for \`${course}\` does not exist.`,
156+
ephemeral: true,
157+
});
158+
} else if (channel.type !== "GUILD_TEXT") {
159+
return await interaction.reply({
160+
content: `❌ | The course chat for \`${course}\` is not a text channel.`,
161+
ephemeral: true,
162+
});
163+
}
164+
165+
const permissions = channel.permissionOverwrites.cache;
166+
167+
// Then check if there's a role that matches the name of the course
147168
const role = await interaction.guild.roles.cache.find(
148169
(r) => r.name.toLowerCase() === course.toLowerCase(),
149170
);
150171

151-
// If there is, let's see if the member already has that role
172+
// Check if the role exists
152173
if (role !== undefined) {
153-
if (!interaction.member.roles.cache.has(role.id)) {
174+
// Check if the member has access via individual perms
175+
if (in_overwrites(permissions, interaction.member.id)) {
176+
// Remove the member from the channel's permission overwrites if so
177+
await channel.permissionOverwrites.delete(interaction.member);
178+
}
179+
180+
// Check if the member has access via role
181+
if (
182+
interaction.member.roles.cache.has(role.id) &&
183+
in_overwrites(permissions, role.id)
184+
) {
185+
// If they do remove the role
186+
await interaction.member.roles.remove(role);
187+
return await interaction.reply({
188+
content: `✅ | Removed you from the role and chat for \`${course}\`.`,
189+
ephemeral: true,
190+
});
191+
} else {
154192
return await interaction.reply({
155193
content: `❌ | You do not have the role for \`${course}\`.`,
156194
ephemeral: true,
157195
});
158196
}
159-
160-
// If they do, let's remove the role from them
161-
await interaction.member.roles.remove(role);
197+
} else if (in_overwrites(permissions, interaction.member.id)) {
198+
// Check if the user has individual perms and removes if so
199+
await channel.permissionOverwrites.delete(interaction.member);
200+
return await interaction.reply({
201+
content: `✅ | Removed you from the chat for \`${course}\`.`,
202+
ephemeral: true,
203+
});
204+
} else {
162205
return await interaction.reply({
163-
content: ` | Removed you from the role and chat for \`${course}\`.`,
206+
content: ` | You do not have access to the chat for \`${course}\`.`,
164207
ephemeral: true,
165208
});
166209
}

commands/rolesPermOverride.js

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { SlashCommandBuilder } = require("@discordjs/builders");
22
const { Permissions } = require("discord.js");
33

4-
const is_valid_course = (course) => {
4+
const is_valid_course_name = (course) => {
55
const reg_comp_course = /^comp\d{4}$/;
66
const reg_math_course = /^math\d{4}$/;
77
const reg_binf_course = /^binf\d{4}$/;
@@ -18,47 +18,51 @@ const is_valid_course = (course) => {
1818
);
1919
};
2020

21-
function editChannels(interaction, channels, role) {
22-
channels.forEach((channel) => {
23-
if (
24-
channel.type === "GUILD_TEXT" &&
25-
channel.name.toLowerCase() === role.name.toLowerCase()
26-
) {
27-
// Remove all permissions from a role
28-
role.setPermissions(0n)
29-
.then((updated) =>
30-
console.log(`Updated permissions to ${updated.permissions.bitfield}`),
31-
)
32-
.catch(console.error);
33-
// Set the permissions of the role
34-
// Add the member to the channel's permission overwrites
35-
channel.permissionOverwrites.create(role, {
36-
VIEW_CHANNEL: true,
37-
SEND_MESSAGES: true,
21+
const in_overwrites = (overwrites, id) =>
22+
[1024n, 3072n].includes(overwrites.find((v, k) => k === id)?.allow?.bitfield);
23+
24+
async function editChannels(interaction, channels) {
25+
for (const data of channels) {
26+
const channel = data[1];
27+
const is_valid = is_valid_course_name(channel.name);
28+
29+
if (!is_valid || channel.type !== "GUILD_TEXT") continue;
30+
31+
let role = interaction.guild.roles.cache.find(
32+
(r) => r.name.toLowerCase() === channel.name.toLowerCase(),
33+
);
34+
35+
if (!role) {
36+
role = await interaction.guild.roles.create({
37+
name: channel.name.toLowerCase(),
38+
color: "BLUE",
3839
});
39-
console.log(channel.name, role.name);
4040
}
41-
});
42-
}
4341

44-
function editRoles(interaction, roles) {
45-
roles.forEach((role) => {
46-
if (is_valid_course(role.name)) {
47-
interaction.guild.channels
48-
.fetch()
49-
.then(
50-
(channels) => (
51-
editChannels(interaction, channels, role),
52-
console.log(`There are ${channels.size} channels.`)
53-
),
54-
)
55-
.catch(console.error);
42+
const permissions = channel.permissionOverwrites.cache;
43+
44+
// clear every individual user permission overwrite for the channel
45+
for (const user of channel.members) {
46+
const userId = user[0];
47+
const userObj = user[1];
48+
49+
if (userObj.user.bot) continue;
50+
51+
// Check if the member has access via individual perms
52+
if (in_overwrites(permissions, userId)) {
53+
// Remove the member from the channel's permission overwrites
54+
await channel.permissionOverwrites.delete(userObj);
55+
await userObj.roles.add(role);
56+
}
5657
}
57-
});
58-
interaction.reply({
59-
content: `✅ | found course chats and matching roles, cleared and set permission overwrites for roles.`,
60-
ephemeral: true,
61-
});
58+
59+
// set the permissions for the new role on a channel
60+
// const channel = interaction.guild.channels.cache.get('CHANNEL_ID');
61+
await channel.permissionOverwrites.create(role, {
62+
VIEW_CHANNEL: true,
63+
SEND_MESSAGES: true,
64+
});
65+
}
6266
}
6367

6468
module.exports = {
@@ -75,19 +79,17 @@ module.exports = {
7579
ephemeral: true,
7680
});
7781
}
82+
await interaction.deferReply();
83+
7884
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,
7985

80-
// give the role the permission override to participate in the matching channel.
81-
interaction.guild.roles
82-
.fetch()
83-
.then(
84-
(roles) => (
85-
editRoles(interaction, roles), console.log(`There are ${roles.size} roles.`)
86-
),
87-
)
88-
.catch(console.error);
86+
// Get all channels and run function
87+
const channels = await interaction.guild.channels.fetch();
88+
89+
await editChannels(interaction, channels);
90+
await interaction.editReply("Successfully ported all user permissions to roles.");
8991
} catch (error) {
90-
await interaction.reply("Error: " + error);
92+
await interaction.editReply("Error: " + error);
9193
}
9294
},
9395
};

0 commit comments

Comments
 (0)