Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: various qol changes #1130

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
POSTGRES_USER=root
POSTGRES_PASSWORD=database_password
POSTGRES_DB=db
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@bot-database:5432/${POSTGRES_DB}?connect_timeout=300
APP_ENV=development
DISCORD_TOKEN=""
DISCORD_GUILD_ID=""
DISCORD_EVENT_CATEGORY_ID=""
DISCORD_ARCHIVE_CATEGORY_ID=""
DISCORD_PLAYER_ROLE_ID=""
DISCORD_MEMBER_ROLE_ID=""
DISCORD_VOICE_CATEGORY_ID=""
DISCORD_DYNAMIC_ROLES_DIVIDER_ID=""
DISCORD_DYNAMIC_ROLES_CATEGORY_ID=""
DISCORD_RETIRED_DYNAMIC_ROLES_CATEGORY_ID=""
DISCORD_MOD_ROLE_ID=""
DISCORD_VOICE_CATEGORY_ID=""
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.18.0
22.11.0
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"@types/supertest": "6.0.2",
"@typescript-eslint/eslint-plugin": "8.10.0",
"@typescript-eslint/parser": "8.10.0",
"eslint": "9.13.0",
"eslint-config-prettier": "9.1.0",
"eslint": "8.57.1",
"eslint-config-prettier": "8.10.0",
"eslint-plugin-prettier": "5.2.1",
"husky": "9.1.6",
"jest": "29.7.0",
Expand Down
270 changes: 133 additions & 137 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import configuration from './config/configuration';
status: 'online',
activities: [
{
name: 'Pac-Man',
name: 'CS2 on de_cache',
type: ActivityType.Playing
}
]
Expand Down
3 changes: 2 additions & 1 deletion src/bot/channels/channels.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ChannelsGateway {
await this.channelsService.initVoiceChannels();
this.logger.log(`Successfully initialized dynamic voice channels`);
} catch (error) {
this.logger.log(`Failed to initialize dynamic voice channels`);
this.logger.error(`Failed to initialize dynamic voice channels:`, error);
}
}

Expand All @@ -27,6 +27,7 @@ export class ChannelsGateway {
await this.channelsService.handleOnVoiceStateUpdate(oldState, newState);
this.logger.log(`Successfully handled voice state update`);
} catch (error) {
console.log('🚀 ~ ChannelsGateway ~ onVoiceStateUpdate ~ error:', error);
this.logger.log(`Failed to handle voice state update`);
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/bot/channels/channels.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export class ChannelsService extends GuildService {
private async getCategoryChannels(): Promise<VoiceChannel[]> {
const guild = await this.getGuild();
const allChannels = Array.from(guild.channels.cache.values());
return allChannels.filter(channel => channel.parentId === this.getVoiceCategoryId()) as VoiceChannel[];
return allChannels.filter(
channel => channel.parentId === this.getVoiceCategoryId() && channel.type === ChannelType.GuildVoice
) as VoiceChannel[];
}

private async getMaxBitrate(): Promise<number> {
Expand Down Expand Up @@ -160,7 +162,7 @@ export class ChannelsService extends GuildService {
type: ChannelType.GuildVoice,
parent: this.getVoiceCategoryId(),
position: index,
// topic: `dynamically created voice channel number ${index + 1}`,
// topic: `i love apples`,
bitrate: await this.getMaxBitrate()
});

Expand All @@ -178,8 +180,8 @@ export class ChannelsService extends GuildService {
private async updateChannel(channel: VoiceBasedChannel, index: number): Promise<void> {
try {
const updatedChannel = await channel.edit({
name: `voice ${index + 1}`,
topic: `dynamically created voice channel number ${index + 1}`
name: `voice ${index + 1}`
// topic: `i love apples`
});

this.logger.log(`Renamed channel ${channel.name} to ${updatedChannel.name}`);
Expand All @@ -195,13 +197,26 @@ export class ChannelsService extends GuildService {

private async deleteChannel(channel: VoiceBasedChannel): Promise<void> {
try {
await channel.delete();
const botMember = channel.guild.members.me;

if (!botMember.permissions.has('ManageChannels')) {
this.logger.error('Bot lacks ManageChannels permission at server level');
return;
}

if (!channel.permissionsFor(botMember).has('ManageChannels')) {
this.logger.error(`Bot lacks ManageChannels permission for channel ${channel.name}`);
return;
}

await channel.delete();
this.logger.log(`Deleted channel ${channel.name}`);
} catch (e) {
const error = e as DiscordAPIError;
if (error.code === 10003) {
this.logger.log('Channel not found');
} else if (error.code === 50013) {
this.logger.error(`Missing permissions to delete channel ${channel.name}`);
} else {
throw error;
}
Expand Down
3 changes: 2 additions & 1 deletion src/bot/channels/commands/lock-channel.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class LockChannelCommand implements DiscordTransformedCommand<LockChannel
await interaction.reply(await this.channelsService.handleLockChannel(voice, lockChannelDto));
this.logger.log(`Lock command executed successfully`);
} catch (error) {
this.logger.log(`Failed to execute lock command`);
const e = error as Error;
this.logger.error(`Failed to execute lock command: ${e.message}`);
}
}
}
29 changes: 17 additions & 12 deletions src/bot/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export class EventsGateway {
try {
await this.eventsService.handleOnCreate(event);
this.logger.log(`Event ${event.name} created successfully`);
} catch (error) {
this.logger.log(`Failed to create event ${event.name}`);
} catch (e) {
const error = e as Error;
this.logger.error(`Failed to create event ${event.name}: ${error.message}`);
}
}

Expand All @@ -26,8 +27,9 @@ export class EventsGateway {
try {
await this.eventsService.handleOnUpdate(oldEvent, newEvent);
this.logger.log(`Event ${newEvent.name} updated successfully`);
} catch (error) {
this.logger.log(`Failed to update event ${oldEvent.name}`);
} catch (e) {
const error = e as Error;
this.logger.error(`Failed to update event ${oldEvent.name}: ${error.message}`);
}
}

Expand All @@ -36,28 +38,31 @@ export class EventsGateway {
try {
await this.eventsService.handleOnDelete(event);
this.logger.log(`Event ${event.name} deleted successfully`);
} catch (error) {
this.logger.log(`Failed to delete event ${event.name}`);
} catch (e) {
const error = e as Error;
this.logger.error(`Failed to delete event ${event.name}: ${error.message}`);
}
}

@On('guildScheduledEventUserAdd')
async onUserAdd(event: GuildScheduledEvent, user: User): Promise<void> {
try {
await this.eventsService.handleOnUserAdd(event, user);
this.logger.log(`${user.username} was added to Event ${event.name} successfully`);
} catch (error) {
this.logger.log(`Failed to add ${user.username} to Event ${event.name}`);
this.logger.log(`User ${user.username} was added to event ${event.name} successfully`);
} catch (e) {
const error = e as Error;
this.logger.error(`Failed to add user ${user.username} to event ${event.name}: ${error.message}`);
}
}

@On('guildScheduledEventUserRemove')
async onUserRemove(event: GuildScheduledEvent, user: User): Promise<void> {
try {
await this.eventsService.handleOnUserRemove(event, user);
this.logger.log(`${user.username} was removed to Event ${event.name} successfully`);
} catch (error) {
this.logger.log(`Failed to remove ${user.username} to Event ${event.name}`);
this.logger.log(`User ${user.username} was removed from event ${event.name} successfully`);
} catch (e) {
const error = e as Error;
this.logger.error(`Failed to remove user ${user.username} from event ${event.name}: ${error.message}`);
}
}
}
20 changes: 0 additions & 20 deletions src/bot/guild.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export class GuildService {
private static memberRoleId: string;
private static playerRoleId: string;
private static voiceCategoryId: string;
private static dynamicRolesDividerId: string;
private static dynamicRolesCategoryId: string;
private static retiredDynamicRolesCategoryId: string;

constructor(
private readonly client: Client,
Expand All @@ -23,11 +20,6 @@ export class GuildService {
GuildService.memberRoleId = this.configService.getOrThrow<string>('discord.memberRoleId');
GuildService.playerRoleId = this.configService.getOrThrow<string>('discord.playerRoleId');
GuildService.voiceCategoryId = this.configService.getOrThrow<string>('discord.voiceCategoryId');
GuildService.dynamicRolesDividerId = this.configService.getOrThrow<string>('discord.dynamicRolesDividerId');
GuildService.dynamicRolesCategoryId = this.configService.getOrThrow<string>('discord.dynamicRolesCategoryId');
GuildService.retiredDynamicRolesCategoryId = this.configService.getOrThrow<string>(
'discord.retiredDynamicRolesCategoryId'
);
}

protected async getGuild(): Promise<Guild> {
Expand Down Expand Up @@ -61,16 +53,4 @@ export class GuildService {
protected getVoiceCategoryId(): string {
return GuildService.voiceCategoryId;
}

protected getdynamicRolesDividerId(): string {
return GuildService.dynamicRolesDividerId;
}

protected getDynamicRolesCategoryId(): string {
return GuildService.dynamicRolesCategoryId;
}

protected getRetiredDynamicRolesCategoryId(): string {
return GuildService.retiredDynamicRolesCategoryId;
}
}
13 changes: 0 additions & 13 deletions src/bot/role.guard.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import IDatabaseConfiguration from './interfaces/database-configuration.interface copy';
import IDiscordConfiguration from './interfaces/discord-configuration.interface';

export default interface IConfiguration {
db: IDatabaseConfiguration;
discord: IDiscordConfiguration;
}
11 changes: 1 addition & 10 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ export default (): IConfiguration => ({
archiveCategoryId: process.env.DISCORD_ARCHIVE_CATEGORY_ID,
voiceCategoryId: process.env.DISCORD_VOICE_CATEGORY_ID,
playerRoleId: process.env.DISCORD_PLAYER_ROLE_ID,
memberRoleId: process.env.DISCORD_MEMBER_ROLE_ID,
dynamicRolesDividerId: process.env.DISCORD_DYNAMIC_ROLES_DIVIDER_ID,
dynamicRolesCategoryId: process.env.DISCORD_DYNAMIC_ROLES_CATEGORY_ID,
retiredDynamicRolesCategoryId: process.env.DISCORD_RETIRED_DYNAMIC_ROLES_CATEGORY_ID
},
db: {
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
db: process.env.POSTGRES_DB,
url: process.env.DATABASE_URL
memberRoleId: process.env.DISCORD_MEMBER_ROLE_ID
}
});

This file was deleted.

3 changes: 0 additions & 3 deletions src/config/interfaces/discord-configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ export default interface IDiscordConfiguration {
voiceCategoryId: string;
memberRoleId: string;
playerRoleId: string;
dynamicRolesDividerId: string;
dynamicRolesCategoryId: string;
retiredDynamicRolesCategoryId: string;
}