Skip to content

Commit 2f59c71

Browse files
committed
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
1 parent 9a4c9ec commit 2f59c71

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

commands/rolesPermOverride.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const { SlashCommandBuilder } = require("@discordjs/builders");
2+
const { Permissions } = require("discord.js");
3+
4+
5+
6+
// map of course aliases to their actual names
7+
const course_aliases = {
8+
comp6841: "comp6441",
9+
comp9044: "comp2041",
10+
comp3891: "comp3231",
11+
comp9201: "comp3231",
12+
comp9101: "comp3121",
13+
comp9331: "comp3331",
14+
comp9415: "comp3421",
15+
comp9801: "comp3821",
16+
comp9102: "comp3131",
17+
comp9154: "comp3151",
18+
comp9164: "comp3161",
19+
comp9211: "comp3211",
20+
comp9222: "comp3221",
21+
comp9814: "comp3411",
22+
comp9511: "comp3511",
23+
comp9900: "comp3900",
24+
seng4920: "comp4920",
25+
comp9337: "comp4337",
26+
math1141: "math1131",
27+
math1241: "math1231",
28+
};
29+
30+
const get_real_course_name = (course) => {
31+
if (course_aliases[course.toLowerCase()]) {
32+
return course_aliases[course.toLowerCase()];
33+
}
34+
return course.toLowerCase();
35+
};
36+
37+
38+
39+
40+
const is_valid_course = (course) => {
41+
const reg_comp_course = /^comp\d{4}$/;
42+
const reg_math_course = /^math\d{4}$/;
43+
const reg_binf_course = /^binf\d{4}$/;
44+
const reg_engg_course = /^engg\d{4}$/;
45+
const reg_seng_course = /^seng\d{4}$/;
46+
const reg_desn_course = /^desn\d{4}$/;
47+
48+
return (
49+
reg_comp_course.test(course.toLowerCase()) ||
50+
reg_math_course.test(course.toLowerCase()) ||
51+
reg_binf_course.test(course.toLowerCase()) ||
52+
reg_engg_course.test(course.toLowerCase()) ||
53+
reg_seng_course.test(course.toLowerCase()) ||
54+
reg_desn_course.test(course.toLowerCase())
55+
);
56+
};
57+
58+
module.exports = {
59+
data: new SlashCommandBuilder()
60+
.setName("rolespermoverride")
61+
.setDescription("Looks for matches between roles and course chats and attaches permissions."),
62+
async execute(interaction) {
63+
try {
64+
// for all roles with name == chat name involving 4 letter prefix comp, seng, engg or binf,
65+
// give the role the permission override to participate in the matching channel.
66+
message.guild.roles.forEach(role => {
67+
if (is_valid_course(role)) {
68+
console.log("hi");
69+
message.guild.channels.forEach(channel => {
70+
if (channel.type === "GUILD_TEXT" && channel.name.toLowerCase() === role.toLowerCase()) {
71+
// Remove all permissions from a role
72+
role.setPermissions(0n)
73+
.then(updated => console.log(`Updated permissions to ${updated.permissions.bitfield}`))
74+
.catch(console.error);
75+
// Set the permissions of the role
76+
// Add the member to the channel's permission overwrites
77+
channel.permissionOverwrites.create(role, {
78+
VIEW_CHANNEL: true,
79+
SEND_MESSAGES: true,
80+
});
81+
interaction.reply({
82+
content: `✅ | removed all permissions and set new permission overwrites for
83+
${interaction.channel.name} and ${role.name}.`,
84+
ephemeral: true,
85+
});
86+
}
87+
});
88+
}
89+
});
90+
91+
92+
} catch (error) {
93+
await interaction.reply("Error: " + error);
94+
}
95+
},
96+
};

0 commit comments

Comments
 (0)