Skip to content

Commit 17aec19

Browse files
zcDay1imagine-hussaintuneinAcdSoftColhjt
authored
test - do not merge until agreed (#159)
* migrate from insou api to circles for the handbook commands (#128) * migrate from insou api to circles for the handbook commands * remove trailing forward slash in handbook.json * handbook: the handbook is fixed * Update handbook.js commented out a console.log --------- Co-authored-by: tunein <z5371683@ad.unsw.edu.au> Co-authored-by: zcDay1 <113964162+zcDay1@users.noreply.github.com> * prettier formatted * chore(deps): update `renovate` config * chore(deps): update docker/build-push-action action to v4 (#134) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * Projects description command feature (#145) * projects-descriptions v1 commit * project-descriptions command v2 commit * linting fixes for project-descriptions.js, and for handbook.js --------- Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> * disable annoying carrotboard pins * rolesPermOverride command is a script or one-time use command to attach every course role with permissions to view and send messages in any text channel with identical name. Please review and test before merging * Revert "rolesPermOverride command is a script or one-time use command to attach every course role with permissions to view and send messages in any text channel with identical name. Please review and test before merging" This reverts commit 2f59c71. * rolesPermOverride command (#152) * rolesPermOverride command is a script or one-time use command to attach every course role with permissions to view and send messages in any text channel with identical name. Please review and test before mergin * BUGGY CODE but still progress over the previous commit, I will come back this evening to fix * rolesPermOverride command: now admin-only command, and I've tested it for various cases regarding if a channel exists, if a role exists for a channel that doesnt exist, if there are two channels with the same name as a role and vice versa, and it works without error --------- Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> * Roles perm override (#153) * rolesPermOverride command is a script or one-time use command to attach every course role with permissions to view and send messages in any text channel with identical name. Please review and test before mergin * BUGGY CODE but still progress over the previous commit, I will come back this evening to fix * rolesPermOverride command: now admin-only command, and I've tested it for various cases regarding if a channel exists, if a role exists for a channel that doesnt exist, if there are two channels with the same name as a role and vice versa, and it works without error * course.js: removed code to create individual user permission overwrites in each channel --------- Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> * Project descriptions (#154) * project-descriptions.js: updated course descriptions written by the directors for 2023 levelling up with Projects Fair Day, and the discord bot CTF flag easter egg entry under this command included too for this event. * made messages ephemeral to prevent chat from being spammed by users. And got rid of link embeds in the messages. * lint update --------- Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> * 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> --------- Co-authored-by: imagine-hussain <93496985+imagine-hussain@users.noreply.github.com> Co-authored-by: tunein <z5371683@ad.unsw.edu.au> Co-authored-by: AcdSoftCo <106219586+AcdSoftCo@users.noreply.github.com> Co-authored-by: Jared L <48422312+lhjt@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: abiramen <7523422+abiramen@users.noreply.github.com> Co-authored-by: Bigbugman <101852152+Bigbugman@users.noreply.github.com> Co-authored-by: Wolfdragon24 <dragonbomber24@gmail.com>
1 parent b85105a commit 17aec19

File tree

2 files changed

+104
-58
lines changed

2 files changed

+104
-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: 51 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,18 @@ module.exports = {
7579
ephemeral: true,
7680
});
7781
}
82+
83+
await interaction.deferReply();
84+
7885
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,
7986

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);
87+
// Get all channels and run function
88+
const channels = await interaction.guild.channels.fetch();
89+
90+
await editChannels(interaction, channels);
91+
await interaction.editReply("Successfully ported all user permissions to roles.");
8992
} catch (error) {
90-
await interaction.reply("Error: " + error);
93+
await interaction.editReply("Error: " + error);
9194
}
9295
},
9396
};

0 commit comments

Comments
 (0)