Skip to content

Commit 9d12d77

Browse files
zcDay1imagine-hussaintuneinAcdSoftColhjt
authored
dev/course fix 5 merge (#161)
* 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 <[email protected]> Co-authored-by: zcDay1 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> Co-authored-by: Wolfdragon24 <[email protected]> * dev/course-channel-fix-5 (#160) * 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 * Appended single channel mode for permissions override * Appended check command for role permissions * Resolved merge conflicts in dev/course-channel-fix-4 --------- Co-authored-by: AcdSoftCo <[email protected]> Co-authored-by: Wolfdragon24 <[email protected]> * Update rolesPermOverride.js double equals updated to triple equals on line 156 --------- Co-authored-by: imagine-hussain <[email protected]> Co-authored-by: tunein <[email protected]> Co-authored-by: AcdSoftCo <[email protected]> Co-authored-by: Jared L <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: abiramen <[email protected]> Co-authored-by: Bigbugman <[email protected]> Co-authored-by: Wolfdragon24 <[email protected]> Co-authored-by: Abraham Assariparambil Earnest <[email protected]>
1 parent 17aec19 commit 9d12d77

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

commands/rolesPermOverride.js

+95-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,68 @@ async function editChannels(interaction, channels) {
6565
}
6666
}
6767

68+
async function isFixed(interaction, channel) {
69+
const is_valid = is_valid_course_name(channel.name);
70+
71+
if (!is_valid || channel.type !== "GUILD_TEXT") return true;
72+
73+
const role = interaction.guild.roles.cache.find(
74+
(r) => r.name.toLowerCase() === channel.name.toLowerCase(),
75+
);
76+
77+
if (!role) return false;
78+
79+
const permissions = channel.permissionOverwrites.cache;
80+
81+
// clear every individual user permission overwrite for the channel
82+
for (const user of channel.members) {
83+
const userId = user[0];
84+
const userObj = user[1];
85+
86+
if (userObj.user.bot) continue;
87+
88+
// Check if the member has access via individual perms
89+
if (in_overwrites(permissions, userId)) return false;
90+
}
91+
92+
if (!in_overwrites(permissions, role.id)) return false;
93+
94+
return true;
95+
}
96+
97+
async function allFixed(interaction, channels) {
98+
const unfixed = [];
99+
for (const data of channels) {
100+
const channel = data[1];
101+
const fixed = await isFixed(interaction, channel);
102+
103+
if (!fixed) unfixed.push(channel.name);
104+
}
105+
106+
return unfixed;
107+
}
108+
68109
module.exports = {
69110
data: new SlashCommandBuilder()
70111
.setName("rolespermoverride")
71112
.setDescription(
72113
"Looks for matches between roles and course chats and attaches permissions.",
114+
)
115+
.addBooleanOption((option) =>
116+
option
117+
.setName("singlechannel")
118+
.setDescription(
119+
"Should this command only be run on the current channel? (Default: False)",
120+
)
121+
.setRequired(false),
122+
)
123+
.addBooleanOption((option) =>
124+
option
125+
.setName("check")
126+
.setDescription(
127+
"Should a check be run on if the channel is fixed? (Default: False)",
128+
)
129+
.setRequired(false),
73130
),
74131
async execute(interaction) {
75132
try {
@@ -84,11 +141,44 @@ module.exports = {
84141

85142
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,
86143

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.");
144+
if (!interaction.options.getBoolean("singlechannel")) {
145+
// Get all channels and run specified function
146+
const channels = await interaction.guild.channels.fetch();
147+
148+
if (!interaction.options.getBoolean("check")) {
149+
await editChannels(interaction, channels);
150+
await interaction.editReply(
151+
"Successfully ported all user permissions to roles.",
152+
);
153+
} else {
154+
const unfixed = await allFixed(interaction, channels);
155+
156+
if (unfixed.length === 0) {
157+
await interaction.editReply("All channels in this server appear fixed.");
158+
} else {
159+
await interaction.editReply(
160+
`The following channels appear unfixed: ${unfixed.join(", ")}`,
161+
);
162+
}
163+
}
164+
} else {
165+
const channel = interaction.channel;
166+
167+
if (!interaction.options.getBoolean("check")) {
168+
await editChannels(interaction, [[undefined, channel]]);
169+
await interaction.editReply(
170+
"Successfully ported user permissions to roles in this channel",
171+
);
172+
} else {
173+
const fixed = await isFixed(interaction, channel);
174+
175+
if (fixed) {
176+
await interaction.editReply("This channel appears fixed.");
177+
} else {
178+
await interaction.editReply("This channel appears unfixed.");
179+
}
180+
}
181+
}
92182
} catch (error) {
93183
await interaction.editReply("Error: " + error);
94184
}

0 commit comments

Comments
 (0)