Skip to content

Commit 027f8fe

Browse files
abe123442AcdSoftCoDomin0de
authored
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]>
1 parent 7912884 commit 027f8fe

File tree

1 file changed

+95
-6
lines changed

1 file changed

+95
-6
lines changed

commands/rolesPermOverride.js

Lines changed: 95 additions & 6 deletions
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 {
@@ -79,16 +136,48 @@ module.exports = {
79136
ephemeral: true,
80137
});
81138
}
82-
83139
await interaction.deferReply();
84140

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

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

0 commit comments

Comments
 (0)