Skip to content
This repository was archived by the owner on Jan 21, 2024. It is now read-only.

Commit f706730

Browse files
committed
Implement student advisory committee permissions.
1 parent a9df183 commit f706730

5 files changed

Lines changed: 73 additions & 52 deletions

File tree

config/config.example.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
"moderatorRoleId": "8236742893645345",
4040
"moderatorCommandChannelId": "3653476435525426",
4141
"courseSelectionChannelId": "34534643643455456",
42-
"verificationChannelId": "52323425624352354"
42+
"verificationChannelId": "52323425624352354",
43+
"studentAdvisoryCommittee": {
44+
"roleId": "238946293846263423"
45+
}
4346
},
4447
"89233498652365349": {
4548
"majors": ["phys", "chem"],

src/models/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ export interface GuildConfig {
5555
moderatorCommandChannelId: string;
5656
courseSelectionChannelId: string;
5757
verificationChannelId: string;
58+
studentAdvisoryCommittee?: GuildStudentAdvisoryCommitteeConfig;
59+
}
60+
61+
export interface GuildStudentAdvisoryCommitteeConfig {
62+
roleId: string
5863
}

src/services/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,18 @@ export class ConfigService {
164164
if(!guild.verificationChannelId) {
165165
throw Error(`Guild ID ${guildId} configuration has missing or empty verification channel ID.`);
166166
}
167+
this.validateGuildStudentAdvisoryCommitteeConfig(guildId);
167168
//TODO: web catalog validation.
168169
});
169170
}
171+
172+
private static validateGuildStudentAdvisoryCommitteeConfig(guildId: string): void {
173+
const guild = this.config.guilds[guildId];
174+
if(guild.studentAdvisoryCommittee) {
175+
if(!guild.studentAdvisoryCommittee.roleId) {
176+
throw Error(`Guild ID ${guildId} configuration has missing or empty student advisory committee role ID.
177+
If you did not mean to enable the student advisory committee functionality, please remove its configuration section.`);
178+
}
179+
}
180+
}
170181
}

src/services/implement/course/channel.ts

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,32 @@ export class CourseChannelImplementService {
1212
categoryId: string,
1313
mainRoleId: string,
1414
taRoleId: string): Promise<Discord.Channel> {
15+
let discordChannelType: any;
16+
let topic = null;
17+
1518
switch (type) {
1619
case CourseImplementChannelType.CHAT:
17-
return this.createChatChannel(guildContext, course, categoryId, mainRoleId, taRoleId);
20+
discordChannelType = "text";
21+
topic = course.title ? `:information_source: ${course.title}` : "";
22+
break;
1823
case CourseImplementChannelType.VOICE:
19-
return this.createVoiceChannel(guildContext, course, categoryId, mainRoleId, taRoleId);
24+
discordChannelType = "voice";
25+
break;
2026
}
27+
28+
const channel = await guildContext.guild.channels.create(
29+
CourseUtils.getChannelNameByType(course, type),
30+
{
31+
type: discordChannelType,
32+
topic: topic,
33+
parent: categoryId,
34+
position: 0,
35+
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, type, mainRoleId, taRoleId),
36+
reason: "StudyBot automatic course channel creation.",
37+
}
38+
);
39+
40+
return channel;
2141
}
2242

2343
public static async resetChannelPermissionsByType(
@@ -36,9 +56,10 @@ export class CourseChannelImplementService {
3656
mainRoleId: string,
3757
taRoleId: string
3858
): Discord.OverwriteResolvable[] {
59+
let permissions: Discord.OverwriteResolvable[] = [];
3960
switch(type) {
4061
case CourseImplementChannelType.CHAT:
41-
return [
62+
permissions = [
4263
{
4364
type: "role",
4465
id: guildContext.guild.roles.everyone.id,
@@ -60,8 +81,17 @@ export class CourseChannelImplementService {
6081
allow: ["VIEW_CHANNEL"]
6182
}
6283
];
84+
85+
if(guildContext.guildConfig.studentAdvisoryCommittee) {
86+
permissions.push({
87+
type: "role",
88+
id: guildContext.guildConfig.studentAdvisoryCommittee.roleId,
89+
allow: ["VIEW_CHANNEL"]
90+
});
91+
}
92+
break;
6393
case CourseImplementChannelType.VOICE:
64-
return [
94+
permissions = [
6595
{
6696
type: "role",
6797
id: guildContext.guild.roles.everyone.id,
@@ -84,47 +114,17 @@ export class CourseChannelImplementService {
84114
allow: ["VIEW_CHANNEL"]
85115
}
86116
];
87-
}
88-
}
89-
90-
public static async createChatChannel(
91-
guildContext: GuildContext,
92-
course: Course,
93-
categoryId: string,
94-
mainRoleId: string,
95-
taRoleId: string): Promise<Discord.TextChannel> {
96-
const channel = await guildContext.guild.channels.create(
97-
CourseUtils.getChatChannelName(course),
98-
{
99-
type: "text",
100-
topic: `:information_source: ${course.title}`,
101-
parent: categoryId,
102-
position: 0,
103-
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, CourseImplementChannelType.CHAT, mainRoleId, taRoleId),
104-
reason: "StudyBot automatic course channel creation.",
105-
}
106-
);
107-
108-
return channel;
109-
}
110-
111-
public static async createVoiceChannel(
112-
guildContext: GuildContext,
113-
course: Course,
114-
categoryId: string,
115-
mainRoleId: string,
116-
taRoleId: string): Promise<Discord.VoiceChannel> {
117-
const channel = await guildContext.guild.channels.create(
118-
CourseUtils.getVoiceChannelName(course),
119-
{
120-
type: "voice",
121-
parent: categoryId,
122-
position: 0,
123-
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, CourseImplementChannelType.VOICE, mainRoleId, taRoleId),
124-
reason: "StudyBot automatic course channel creation.",
125-
}
126-
);
127117

128-
return channel;
118+
if(guildContext.guildConfig.studentAdvisoryCommittee) {
119+
permissions.push({
120+
type: "role",
121+
id: guildContext.guildConfig.studentAdvisoryCommittee.roleId,
122+
allow: ["VIEW_CHANNEL", "SPEAK", "STREAM"]
123+
});
124+
}
125+
break;
126+
}
127+
128+
return permissions;
129129
}
130130
}

src/utils/course.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as crypto from "crypto";
22
import { PartialCourse } from "models/course";
3+
import { CourseImplementChannelType } from "models/implement/course";
34
import { Major } from "models/major";
45
import { ConfigService } from "services/config";
56

@@ -49,11 +50,12 @@ export class CourseUtils {
4950
return `${course.key}-ta`;
5051
}
5152

52-
public static getChatChannelName(course: PartialCourse): string {
53-
return course.key;
54-
}
55-
56-
public static getVoiceChannelName(course: PartialCourse): string {
57-
return `${course.key}-voice`;
53+
public static getChannelNameByType(course: PartialCourse, type: CourseImplementChannelType): string {
54+
switch(type) {
55+
case CourseImplementChannelType.CHAT:
56+
return course.key;
57+
case CourseImplementChannelType.VOICE:
58+
return `${course.key}-voice`;
59+
}
5860
}
5961
}

0 commit comments

Comments
 (0)