Skip to content

Commit

Permalink
Better Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Sep 13, 2024
1 parent 9650b93 commit 6f0d2fd
Show file tree
Hide file tree
Showing 32 changed files with 267 additions and 413 deletions.
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"token": "DISCORD_BOT_TOKEN",
"spotifyClientId": "SPOTIFY_CLIENT_ID",
"port": 18173
"port": 18173,
"ownerId": "OWNER_ID"
}
2 changes: 1 addition & 1 deletion src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as config from '../config.json';
import CacheHandler from './Private/CacheHandler';
import DiscordManager from './Discord/DiscordManager';
import Logger from './utils/Logger';
import Logger from './Private/Logger';
import RequestHandler from './Private/RequestHandler';
import SpotifyManager from './Spotify/SpotifyManager';

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class PauseButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class PlayButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Previous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class PreviousButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class QueueButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
}
}

Expand Down
27 changes: 9 additions & 18 deletions src/Discord/Buttons/QueueTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@ class QueueTrackButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].author ||
!interaction.message.embeds[0].author.name
) {
return;
}
const trackId = interaction.message.embeds[0].author.name.split('ID: ')[1];
await this.discord.Application.spotify.requestHandler.queueTrack(`spotify:track:${trackId}`);
await interaction.reply({ content: 'Song added to queue.', ephemeral: true });
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].author ||
!interaction.message.embeds[0].author.name
) {
return;
}
const trackId = interaction.message.embeds[0].author.name.split('ID: ')[1];
await this.discord.Application.spotify.requestHandler.queueTrack(`spotify:track:${trackId}`);
await interaction.reply({ content: 'Song added to queue.', ephemeral: true });
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ class RefreshButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
await interaction.update({
embeds: [playback.toEmbed(this.discord.emojis)],
components: playback.toButtons(this.discord.emojis)
});
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
}
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
await interaction.update({
embeds: [playback.toEmbed(this.discord.emojis)],
components: playback.toButtons(this.discord.emojis)
});
}
}

Expand Down
39 changes: 15 additions & 24 deletions src/Discord/Buttons/SearchBack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ class SearchBackButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, pageIndex] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[0]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, pageIndex - 1);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, pageIndex] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[0]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, pageIndex - 1);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
}
}

Expand Down
39 changes: 15 additions & 24 deletions src/Discord/Buttons/SearchEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ class SearchEndButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, maxPage] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[1]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, maxPage);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, maxPage] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[1]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, maxPage);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
}
}

Expand Down
39 changes: 15 additions & 24 deletions src/Discord/Buttons/SearchForward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@ class SearchForwardButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, pageIndex] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[0]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, pageIndex + 1);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
if (
!interaction.message.embeds[0] ||
!interaction.message.embeds[0].description ||
!interaction.message.embeds[0].title
) {
return;
}
const [search, pageIndex] = [
interaction.message.embeds[0].title.split(': ')[1].trim(),
Number(interaction.message.embeds[0].description.split('Page ')[1].split('/')[0]) - 1
];
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search, pageIndex + 1);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
}
}

Expand Down
25 changes: 8 additions & 17 deletions src/Discord/Buttons/SearchStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,15 @@ class SearchStartButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
if (!interaction.message.embeds[0] || !interaction.message.embeds[0].title) {
return;
}
const search = interaction.message.embeds[0].title.split(': ')[1].trim();
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
if (!interaction.message.embeds[0] || !interaction.message.embeds[0].title) {
return;
}
const search = interaction.message.embeds[0].title.split(': ')[1].trim();
const res = await this.discord.Application.spotify.requestHandler.searchTracks(search);
await interaction.update({
embeds: [res.toEmbed(this.discord.emojis)],
components: [res.toButtons(this.discord.emojis), res.toSelectMenu()]
});
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/Discord/Buttons/Shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ class ShuffleButton extends Button {
}

async execute(interaction: ButtonInteraction): Promise<void> {
try {
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'Something went wrong. Please try again later.', ephemeral: true });
return;
}
await interaction.reply({ content: 'Something went wrong. Please try again later.', ephemeral: true });
const command = interaction.client.commands.get(interaction.customId);
if (command === undefined) {
await interaction.reply({ content: 'Can i click ur buttons?', ephemeral: true });
return;
}
await command.execute(interaction);
}
}

Expand Down
Loading

0 comments on commit 6f0d2fd

Please sign in to comment.