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

Commit

Permalink
Merge pull request #46 from MitchTalmadge/develop
Browse files Browse the repository at this point in the history
Implement student advisory committee permissions.
  • Loading branch information
MitchTalmadge authored Nov 15, 2020
2 parents baa57d3 + f706730 commit 05659cd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 52 deletions.
5 changes: 4 additions & 1 deletion config/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"moderatorRoleId": "8236742893645345",
"moderatorCommandChannelId": "3653476435525426",
"courseSelectionChannelId": "34534643643455456",
"verificationChannelId": "52323425624352354"
"verificationChannelId": "52323425624352354",
"studentAdvisoryCommittee": {
"roleId": "238946293846263423"
}
},
"89233498652365349": {
"majors": ["phys", "chem"],
Expand Down
5 changes: 5 additions & 0 deletions src/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ export interface GuildConfig {
moderatorCommandChannelId: string;
courseSelectionChannelId: string;
verificationChannelId: string;
studentAdvisoryCommittee?: GuildStudentAdvisoryCommitteeConfig;
}

export interface GuildStudentAdvisoryCommitteeConfig {
roleId: string
}
11 changes: 11 additions & 0 deletions src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,18 @@ export class ConfigService {
if(!guild.verificationChannelId) {
throw Error(`Guild ID ${guildId} configuration has missing or empty verification channel ID.`);
}
this.validateGuildStudentAdvisoryCommitteeConfig(guildId);
//TODO: web catalog validation.
});
}

private static validateGuildStudentAdvisoryCommitteeConfig(guildId: string): void {
const guild = this.config.guilds[guildId];
if(guild.studentAdvisoryCommittee) {
if(!guild.studentAdvisoryCommittee.roleId) {
throw Error(`Guild ID ${guildId} configuration has missing or empty student advisory committee role ID.
If you did not mean to enable the student advisory committee functionality, please remove its configuration section.`);
}
}
}
}
90 changes: 45 additions & 45 deletions src/services/implement/course/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,32 @@ export class CourseChannelImplementService {
categoryId: string,
mainRoleId: string,
taRoleId: string): Promise<Discord.Channel> {
let discordChannelType: any;
let topic = null;

switch (type) {
case CourseImplementChannelType.CHAT:
return this.createChatChannel(guildContext, course, categoryId, mainRoleId, taRoleId);
discordChannelType = "text";
topic = course.title ? `:information_source: ${course.title}` : "";
break;
case CourseImplementChannelType.VOICE:
return this.createVoiceChannel(guildContext, course, categoryId, mainRoleId, taRoleId);
discordChannelType = "voice";
break;
}

const channel = await guildContext.guild.channels.create(
CourseUtils.getChannelNameByType(course, type),
{
type: discordChannelType,
topic: topic,
parent: categoryId,
position: 0,
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, type, mainRoleId, taRoleId),
reason: "StudyBot automatic course channel creation.",
}
);

return channel;
}

public static async resetChannelPermissionsByType(
Expand All @@ -36,9 +56,10 @@ export class CourseChannelImplementService {
mainRoleId: string,
taRoleId: string
): Discord.OverwriteResolvable[] {
let permissions: Discord.OverwriteResolvable[] = [];
switch(type) {
case CourseImplementChannelType.CHAT:
return [
permissions = [
{
type: "role",
id: guildContext.guild.roles.everyone.id,
Expand All @@ -60,8 +81,17 @@ export class CourseChannelImplementService {
allow: ["VIEW_CHANNEL"]
}
];

if(guildContext.guildConfig.studentAdvisoryCommittee) {
permissions.push({
type: "role",
id: guildContext.guildConfig.studentAdvisoryCommittee.roleId,
allow: ["VIEW_CHANNEL"]
});
}
break;
case CourseImplementChannelType.VOICE:
return [
permissions = [
{
type: "role",
id: guildContext.guild.roles.everyone.id,
Expand All @@ -84,47 +114,17 @@ export class CourseChannelImplementService {
allow: ["VIEW_CHANNEL"]
}
];
}
}

public static async createChatChannel(
guildContext: GuildContext,
course: Course,
categoryId: string,
mainRoleId: string,
taRoleId: string): Promise<Discord.TextChannel> {
const channel = await guildContext.guild.channels.create(
CourseUtils.getChatChannelName(course),
{
type: "text",
topic: `:information_source: ${course.title}`,
parent: categoryId,
position: 0,
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, CourseImplementChannelType.CHAT, mainRoleId, taRoleId),
reason: "StudyBot automatic course channel creation.",
}
);

return channel;
}

public static async createVoiceChannel(
guildContext: GuildContext,
course: Course,
categoryId: string,
mainRoleId: string,
taRoleId: string): Promise<Discord.VoiceChannel> {
const channel = await guildContext.guild.channels.create(
CourseUtils.getVoiceChannelName(course),
{
type: "voice",
parent: categoryId,
position: 0,
permissionOverwrites: this.generateChannelPermissionsByType(guildContext, CourseImplementChannelType.VOICE, mainRoleId, taRoleId),
reason: "StudyBot automatic course channel creation.",
}
);

return channel;
if(guildContext.guildConfig.studentAdvisoryCommittee) {
permissions.push({
type: "role",
id: guildContext.guildConfig.studentAdvisoryCommittee.roleId,
allow: ["VIEW_CHANNEL", "SPEAK", "STREAM"]
});
}
break;
}

return permissions;
}
}
14 changes: 8 additions & 6 deletions src/utils/course.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as crypto from "crypto";
import { PartialCourse } from "models/course";
import { CourseImplementChannelType } from "models/implement/course";
import { Major } from "models/major";
import { ConfigService } from "services/config";

Expand Down Expand Up @@ -49,11 +50,12 @@ export class CourseUtils {
return `${course.key}-ta`;
}

public static getChatChannelName(course: PartialCourse): string {
return course.key;
}

public static getVoiceChannelName(course: PartialCourse): string {
return `${course.key}-voice`;
public static getChannelNameByType(course: PartialCourse, type: CourseImplementChannelType): string {
switch(type) {
case CourseImplementChannelType.CHAT:
return course.key;
case CourseImplementChannelType.VOICE:
return `${course.key}-voice`;
}
}
}

0 comments on commit 05659cd

Please sign in to comment.