Skip to content

Commit

Permalink
Single source of truth for queue names (#208)
Browse files Browse the repository at this point in the history
* change for help queue, queue display, and role

* Single source of truth for queue names
Fixes #188

* fix a few errors

* seperate server and calendar extension code

* fix changes

* fix await/async
  • Loading branch information
gracexichen authored Jan 15, 2024
1 parent 04ff133 commit 0d8caef
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getHandler,
interactionExtensions
} from './interaction-handling/interaction-entry-point.js';
import { Guild, Events } from 'discord.js';
import { Guild, Events, ChannelType } from 'discord.js';
import { AttendingServer } from './attending-server/base-attending-server.js';
import { black, green, red, yellow } from './utils/command-line-colors.js';
import { EmbedColor, SimpleEmbed } from './utils/embed-helper.js';
Expand Down Expand Up @@ -128,6 +128,19 @@ client.on(Events.GuildMemberAdd, async member => {
}
});

/**
* If a queue is renamed manually, it renames the queue name for queue embeds, roles, and calendar extension
*/
client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
if (
oldChannel.type === ChannelType.GuildCategory &&
newChannel.type === ChannelType.GuildCategory &&
oldChannel.name !== newChannel.name
) {
await AttendingServer.safeGet(oldChannel.guild.id)?.updateQueueName(oldChannel, newChannel);
}
});

/**
* Used for inviting YABOB to a server with existing roles
* Once YABOB has the highest role, start the initialization call
Expand Down
50 changes: 50 additions & 0 deletions src/attending-server/base-attending-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,56 @@ class AttendingServer {
await this.getQueueChannels(false);
}

/**
* Updates the queue name for queue embeds, roles, and calendar extension
* @param oldChannel Old category channel before the name was updated
* @param newChannel New category channel after the name was updated
*/
async updateQueueName(oldChannel: CategoryChannel, newChannel: CategoryChannel) {
const oldName = oldChannel.name;
const newName = newChannel.name;
const channelQueue = this._queues.get(oldChannel.id);
if (!channelQueue) {
return;
}
const role = this.guild.roles.cache.find(role => role.name === oldChannel.name);
const newQueueChannel: QueueChannel = {
channelObj: channelQueue.queueChannel.channelObj,
queueName: newName,
parentCategoryId: channelQueue.parentCategoryId
};
const nameTaken = this._queues.some(
queue => queue.queueName === newName && queue !== channelQueue
);
const roleTaken = this.guild.roles.cache.some(
role => role.name === newChannel.name
);
const cachedChannelIndex = this.queueChannelsCache.findIndex(
queueChannel => queueChannel.queueName === oldName
);
if (cachedChannelIndex !== -1){
this.queueChannelsCache.splice(cachedChannelIndex, 1);
this.queueChannelsCache.push(newQueueChannel);
}
if (nameTaken) {
await newChannel.setName(oldName);
LOGGER.error(
`Queue name '${newName}' already exists. Rename '${oldName}' to a different name.`
);
return;
}
if (role && !roleTaken) {
await role.setName(newName);
}
channelQueue.queueChannel = newQueueChannel;
await channelQueue.triggerRender();
await Promise.all(
this.serverExtensions.map(extension =>
extension.onQueueChannelUpdate(this, oldName, newQueueChannel)
)
);
}

/**
* Deletes a queue by categoryID
* @param parentCategoryId CategoryChannel.id of the target queue
Expand Down
12 changes: 12 additions & 0 deletions src/extensions/extension-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
SelectMenuHandlerProps
} from '../interaction-handling/handler-interface.js';
import { CommandData } from '../utils/type-aliases.js';
import { QueueChannel } from '../attending-server/base-attending-server.js';

interface InteractionExtension {
/**
Expand Down Expand Up @@ -161,6 +162,14 @@ interface ServerExtension {
* @param server the server to backup
*/
onServerRequestBackup: (server: FrozenServer) => Promise<void>;
/**
* When a queue category channel is updated
* @param server the server of the category queue
* @param oldName old name of queue category channel
* @param newChannel new queue category channel object
* @returns
*/
onQueueChannelUpdate: (server: FrozenServer, oldName: string, newChannel: QueueChannel) => Promise<void>;
}

/** Extensions for individual queues */
Expand Down Expand Up @@ -333,6 +342,9 @@ abstract class BaseServerExtension implements ServerExtension {
onServerRequestBackup(server: FrozenServer): Promise<void> {
return Promise.resolve();
}
onQueueChannelUpdate(server: FrozenServer, oldName: string, newChannel: QueueChannel): Promise<void>{
return Promise.resolve();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CalendarQueueExtension extends BaseQueueExtension {
*/
private constructor(
private readonly renderIndex: RenderIndex,
private readonly queueChannel: QueueChannel,
public queueChannel: QueueChannel,
private readonly display: FrozenDisplay
) {
super();
Expand Down
16 changes: 16 additions & 0 deletions src/extensions/session-calendar/calendar-server-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BaseServerExtension } from '../extension-interface.js';
import { FrozenServer } from '../extension-utils.js';
import { CalendarExtensionState } from './calendar-states.js';
import { CALENDAR_LOGGER } from './shared-calendar-functions.js';
import { QueueChannel } from '../../attending-server/base-attending-server.js';

/**
* Server extension of session calendar
Expand Down Expand Up @@ -50,6 +51,21 @@ class CalendarServerExtension extends BaseServerExtension {
clearInterval(this.timerId);
CalendarExtensionState.allStates.delete(server.guild.id);
}

/**
* If a queue channel name is changed on discord, update the calendar name
* @param server the server of the changed queue channel
* @param oldName new name of queue channel
* @param newChannel new queue channel object
*/
override async onQueueChannelUpdate(
server: FrozenServer,
oldName: string,
newChannel: QueueChannel
): Promise<void> {
const state = CalendarExtensionState.get(server.guild.id);
await state.renameCalendar(oldName, newChannel);
}
}

export { CalendarServerExtension };
18 changes: 18 additions & 0 deletions src/extensions/session-calendar/calendar-states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { z } from 'zod';
import { CalendarServerExtension } from './calendar-server-extension.js';
import { ExpectedCalendarErrors } from './calendar-constants/expected-calendar-errors.js';
import { ServerExtension } from '../extension-interface.js';
import { QueueChannel } from '../../attending-server/base-attending-server.js';
import { Logger } from 'pino';

/**
Expand Down Expand Up @@ -230,6 +231,23 @@ class CalendarExtensionState {
await this.emitStateChangeEvent();
}

/**
* Resets the name of the queue extension
* change the key(queue channel name) that maps to the value (queue extension object)
* @param oldName
* @param newQueueChannel
*/
async renameCalendar(oldName: string, newQueueChannel: QueueChannel) {
const newName = newQueueChannel.queueName;
const object = this.queueExtensions.get(oldName);
if (object) {
this.queueExtensions.set(newName, object);
this.queueExtensions.delete(oldName);
object.queueChannel = newQueueChannel;
await this.emitStateChangeEvent();
}
}

/**
* Backs up the calendar config to firebase
* - This is synchronous, we don't need to `await backupToFirebase`
Expand Down
2 changes: 1 addition & 1 deletion src/help-queue/help-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class HelpQueue {
* @param backupData if defined, use this data to restore the students array
*/
protected constructor(
public readonly queueChannel: QueueChannel,
public queueChannel: QueueChannel,
private readonly queueExtensions: QueueExtension[],
private readonly display: QueueDisplay,
backupData?: QueueBackup & {
Expand Down

0 comments on commit 0d8caef

Please sign in to comment.