Skip to content

Commit

Permalink
Rewrite Requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Sep 11, 2024
1 parent 539816a commit dfaa2a2
Show file tree
Hide file tree
Showing 30 changed files with 299 additions and 658 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"discord.js": "^14.16.1",
"express": "^4.19.2",
"express-session": "^1.18.0",
"node-cache": "^5.1.2",
"winston": "^3.14.2"
},
"devDependencies": {
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/Application.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import * as config from '../config.json';
import CacheHandler from './Private/CacheHandler';
import DiscordManager from './Discord/DiscordManager';
import Logger from './utils/Logger';
import RequestHandler from './Private/RequestHandler';
import SpotifyManager from './Spotify/SpotifyManager';

interface Errors {
NOT_LOGGED_IN: string;
NOTHING_PLAYING: string;
}

const errors: Errors = {
NOT_LOGGED_IN: 'Account isnt logged in.',
NOTHING_PLAYING: 'Nothing is playing.'
};

class Application {
Logger: Logger;
discord: DiscordManager;
spotify: SpotifyManager;
readonly config: typeof config;
declare Logger: Logger;
declare discord: DiscordManager;
declare spotify: SpotifyManager;
declare cacheHandler: CacheHandler;
declare requestHandler: RequestHandler;
declare errors: Errors;
constructor() {
this.config = config;
this.Logger = new Logger();
this.discord = new DiscordManager(this);
this.spotify = new SpotifyManager(this);
this.cacheHandler = new CacheHandler();
this.requestHandler = new RequestHandler(this);
this.errors = errors;
}

connect(): void {
Expand Down
22 changes: 2 additions & 20 deletions src/Discord/commands/Pause.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,25 +22,8 @@ class PauseCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const res = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/pause`);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.' });
return;
}
if (204 === res.status) {
await interaction.followUp({ content: 'Nothing is playing.' });
return;
}
const data = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === data.status || 401 === data.status) {
await interaction.followUp({ content: 'Account isnt logged in.' });
return;
}
if (204 === data.status) {
await interaction.followUp({ content: 'Nothing is playing.' });
return;
}
const playback = new Playback((await data.json()).data);
await this.discord.Application.spotify.requestHandler.pause();
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
Expand Down
22 changes: 2 additions & 20 deletions src/Discord/commands/Play.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,25 +22,8 @@ class PlayCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const res = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/play`);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === res.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const data = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === data.status || 401 === data.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === data.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const playback = new Playback((await data.json()).data);
await this.discord.Application.spotify.requestHandler.play();
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
Expand Down
12 changes: 1 addition & 11 deletions src/Discord/commands/Playback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,16 +22,7 @@ class PlaybackCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const res = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === res.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const playback = new Playback((await res.json()).data);
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
Expand Down
24 changes: 2 additions & 22 deletions src/Discord/commands/Previous.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,27 +22,8 @@ class PreviousCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const previousData = await fetch(
`http://localhost:${this.discord.Application.config.port}/proxy/playback/previous`
);
if (403 === previousData.status || 401 === previousData.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (200 !== previousData.status) {
await interaction.followUp({ content: 'Something went wrong! Please try again.', ephemeral: true });
return;
}
const data = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === data.status || 401 === data.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === data.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const playback = new Playback((await data.json()).data);
await this.discord.Application.spotify.requestHandler.previous();
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
Expand Down
14 changes: 2 additions & 12 deletions src/Discord/commands/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Queue from '../../Spotify/Private/API/Queue';
import {
ApplicationIntegrationType,
ButtonInteraction,
Expand All @@ -23,17 +22,8 @@ class QueueCommand extends Command {
async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
if (!interaction.deferred) await interaction.deferReply({ ephemeral: true });
const res = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/queue`);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === res.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const data = new Queue((await res.json()).data);
await interaction.followUp({ embeds: [data.toEmbed()], ephemeral: true });
const res = await this.discord.Application.spotify.requestHandler.getQueue();
await interaction.followUp({ embeds: [res.toEmbed()], ephemeral: true });
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
Expand Down
20 changes: 2 additions & 18 deletions src/Discord/commands/Search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Search from '../../Spotify/Private/API/Search/Search';
import {
ApplicationIntegrationType,
ChatInputCommandInteraction,
Expand Down Expand Up @@ -30,23 +29,8 @@ class SearchCommand extends Command {
await interaction.reply({ content: 'Please provide a search query.', ephemeral: true });
return;
}
const res = await fetch(
`http://localhost:${this.discord.Application.config.port}/proxy/search/track/${query}/${page}`
);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (200 !== res.status) {
await interaction.followUp({ content: 'Something went wrong! Please try again.', ephemeral: true });
return;
}
const data = new Search((await res.json()).data);
await interaction.followUp({
embeds: [data.toEmbed()],
components: [data.toButtons(), data.toSelectMenu()],
ephemeral: true
});
const res = await this.discord.Application.spotify.requestHandler.searchTracks(query, page);
await interaction.followUp({ embeds: [res.toEmbed()], components: [res.toButtons(), res.toSelectMenu()] });
} catch (error) {
if (error instanceof Error) this.discord.Application.Logger.error(error);
if (interaction.replied || interaction.deferred) {
Expand Down
24 changes: 3 additions & 21 deletions src/Discord/commands/Shuffle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,33 +22,16 @@ class ShuffleCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const res = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/shuffle`);
if (403 === res.status || 401 === res.status) {
await interaction.followUp({ content: 'Account isnt logged in.' });
return;
}
if (204 === res.status) {
await interaction.followUp({ content: 'Nothing is playing.' });
return;
}
const data = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === data.status || 401 === data.status) {
await interaction.followUp({ content: 'Account isnt logged in.' });
return;
}
if (204 === data.status) {
await interaction.followUp({ content: 'Nothing is playing.' });
return;
}
const playback = new Playback((await data.json()).data);
await this.discord.Application.spotify.requestHandler.shuffle();
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
} else {
await interaction.followUp(sendData);
}
await interaction.followUp({
content: `Shuffle ${(await res.json()).state ? 'Enabled' : 'Disabled'}.`,
content: `Shuffle ${playback.shuffleState ? 'Enabled' : 'Disabled'}.`,
ephemeral: true
});
} catch (error) {
Expand Down
22 changes: 2 additions & 20 deletions src/Discord/commands/Skip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Command from '../Private/Command';
import DiscordManager from '../DiscordManager';
import Playback from '../../Spotify/Private/API/Playback';
import {
ApplicationIntegrationType,
BaseMessageOptions,
Expand All @@ -23,25 +22,8 @@ class SkipCommand extends Command {

async execute(interaction: ChatInputCommandInteraction | ButtonInteraction): Promise<void> {
try {
const skipData = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/next`);
if (403 === skipData.status || 401 === skipData.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (200 !== skipData.status) {
await interaction.followUp({ content: 'Something went wrong! Please try again.', ephemeral: true });
return;
}
const data = await fetch(`http://localhost:${this.discord.Application.config.port}/proxy/playback/status`);
if (403 === data.status || 401 === data.status) {
await interaction.followUp({ content: 'Account isnt logged in.', ephemeral: true });
return;
}
if (204 === data.status) {
await interaction.followUp({ content: 'Nothing is playing.', ephemeral: true });
return;
}
const playback = new Playback((await data.json()).data);
await this.discord.Application.spotify.requestHandler.skip();
const playback = await this.discord.Application.spotify.requestHandler.getStatus();
const sendData: BaseMessageOptions = { embeds: [playback.toEmbed()], components: playback.toButtons() };
if (interaction.isButton()) {
await interaction.update(sendData);
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/commands/Uptime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UptimeCommand extends Command {

async execute(interaction: ChatInputCommandInteraction): Promise<void> {
try {
await interaction.reply({
await interaction.followUp({
content: `Online since <t:${Math.floor((Date.now() - interaction.client.uptime) / 1000)}:R>`
});
} catch (error) {
Expand Down
Loading

0 comments on commit dfaa2a2

Please sign in to comment.