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

Commit a08926a

Browse files
committed
Wipe channels on reset
1 parent 539e1f8 commit a08926a

5 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/constants/discord.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export namespace DiscordConstants {
22

3-
export const MAX_CHANNELS_PER_CATEGORY = 50;
3+
export const MAX_CHANNELS_PER_CATEGORY = 3;
44

55
}

src/controllers/command/moderator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class ModeratorCommandController extends CommandController {
6666
+" \n\tEither re-send a verification email, or forcefully assign the given student ID to the user."
6767
+ "\n`!!reset [days]`"
6868
+ "\n\tPerforms a full reset of all course assignments for anyone who has not updated their courses in the last [days] days. (Defaults to 30 days)."
69+
+ "\n\tWill also wipe all messages from course channels."
6970
+ "\n\tOnly affects this guild; not the entire network."
7071
);
7172
}
@@ -226,6 +227,7 @@ export class ModeratorCommandController extends CommandController {
226227
DiscordMessageUtils.sendReply(message, `now resetting all course assignments older than ${pendingReset.days} days. Please wait...`);
227228
try {
228229
const numReset = await this.resetService.resetCourseAssignments(pendingReset.days);
230+
await this.resetService.wipeCourseChannels();
229231
DiscordMessageUtils.sendReply(message, `reset complete! ${numReset} members have had their course assignments removed.`);
230232
} catch (err) {
231233
console.error("Failed to run course assignment reset by command:", err);

src/guild-context.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { WebCatalogFactory } from "./services/web-catalog/web-catalog-factory";
2424
* Since each guild would have its own major, users, roles, channels, etc., this helps keep things separate.
2525
*/
2626
export class GuildContext {
27+
private healthAssuranceService: HealthAssuranceService;
28+
2729
private resetService: ResetService;
2830

2931
private courseSelectionController: CourseSelectionChannelController;
@@ -81,14 +83,14 @@ export class GuildContext {
8183
}
8284

8385
private async initHealth(): Promise<void> {
84-
let has = new HealthAssuranceService(this);
85-
this.verificationRoleId = await has.guaranteeVerificationRole();
86-
await has.guaranteeCourseImplements();
87-
await has.identifyAndFixHealthIssues();
86+
this.healthAssuranceService = new HealthAssuranceService(this);
87+
this.verificationRoleId = await this.healthAssuranceService.guaranteeVerificationRole();
88+
await this.healthAssuranceService.guaranteeCourseImplements();
89+
await this.healthAssuranceService.identifyAndFixHealthIssues();
8890
}
8991

9092
private initServices(): void {
91-
this.resetService = new ResetService(this);
93+
this.resetService = new ResetService(this, this.healthAssuranceService);
9294
}
9395

9496
private initControllers(): void {

src/services/health-assurance.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class HealthAssuranceService {
3434
await CourseImplementService.guarantee(this.guildContext, course);
3535
}
3636
}
37+
38+
// New channels could require re-sorting.
39+
await MajorImplementService.sortAll(this.guildContext);
3740
}
3841

3942
public async identifyAndFixHealthIssues() {

src/services/reset.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { GuildContext } from "guild-context";
2+
import { CourseImplementChannelType } from "models/implement/course";
23
import moment from "moment";
34

5+
import { GuildStorageDatabaseService } from "./database/guild-storage";
46
import { UserDatabaseService } from "./database/user";
7+
import { HealthAssuranceService } from "./health-assurance";
58
import { MemberUpdateService } from "./member-update";
69

710
export class ResetService {
8-
constructor(private guildContext: GuildContext) {
11+
constructor(private guildContext: GuildContext, private healthAssuranceService: HealthAssuranceService) {
912
}
1013

1114
/**
@@ -17,7 +20,7 @@ export class ResetService {
1720
* @returns The number of members who had their courses unassigned.
1821
*/
1922
public async resetCourseAssignments(gracePeriodDays = 30): Promise<number> {
20-
this.guildContext.guildLog("!!! Reset initiated!");
23+
this.guildContext.guildLog("!!! Resetting Course Assignments!");
2124

2225
// Figure out who has not updated their assignments recently.
2326
let usersToReset = (await UserDatabaseService.getAllUsers()).filter(user => {
@@ -41,8 +44,29 @@ export class ResetService {
4144
// This will automatically clean up any un-used courses as well.
4245
await MemberUpdateService.queueUnassignAllCoursesManyMembers(this.guildContext, membersToReset);
4346

44-
this.guildContext.guildLog("!!! Reset complete!");
47+
this.guildContext.guildLog("!!! Course Assignment Reset Complete!");
4548

4649
return membersToReset.length;
4750
}
51+
52+
public async wipeCourseChannels(): Promise<void> {
53+
this.guildContext.guildLog("!!! Wiping Course Channels!");
54+
55+
// Delete all course text channels, since Discord does not expose a way to clear a channel.
56+
const guildStorage = await GuildStorageDatabaseService.findOrCreateGuildStorage(this.guildContext);
57+
for(let majorImplement of guildStorage.majorImplements) {
58+
let courseImplements = majorImplement[1].courseImplements;
59+
for (let courseImplement of courseImplements) {
60+
const channel = this.guildContext.guild.channels.resolve(courseImplement[1].channelIds[CourseImplementChannelType.CHAT]);
61+
if(channel) {
62+
await channel.delete("StudyBot Course Wipe");
63+
}
64+
}
65+
}
66+
67+
// Re-build the deleted channels.
68+
await this.healthAssuranceService.guaranteeCourseImplements();
69+
70+
this.guildContext.guildLog("!!! Course Channel Wipe Complete!");
71+
}
4872
}

0 commit comments

Comments
 (0)