Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
Added Play Command, currently only working if queue exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Shiny1708 committed Oct 9, 2023
1 parent 19d6cb4 commit d168fbf
Showing 1 changed file with 92 additions and 1 deletion.
93 changes: 92 additions & 1 deletion structs/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import express from "express";
import { bot } from "../index";
import { i18n } from "../utils/i18n";
import { canModifyQueue } from "../utils/queue";
import { TextChannel } from "discord.js";
import { ApplicationCommand, ChatInputCommandInteraction, CommandInteraction, TextChannel } from "discord.js";
import { config } from "../utils/config";
import { playlistPattern } from "../utils/patterns";
import { Song } from "./Song";
import { MusicQueue } from "./MusicQueue";
import { DiscordGatewayAdapterCreator, joinVoiceChannel } from "@discordjs/voice";

export default class Server {
public constructor(port: number) {
Expand Down Expand Up @@ -64,6 +68,93 @@ export default class Server {
res.send(i18n.__mf("skip.result", { author: userId }));
});

app.post("/volume", (req: Request, res: Response) => {
const guildId = req.headers.guildid?.toString()!;
const userId = req.headers.userid?.toString()!;
const channelId = req.headers.channelid?.toString()!;
const volumeArg = Number.parseInt(req.headers.volume?.toString()!);
const channel: TextChannel = <TextChannel>bot.client.channels.cache.get(channelId);
const queue = bot.queues.get(guildId);
const guildMemer = bot.client.guilds.cache.get(guildId)?.members.cache.get(userId);

if (!queue) return channel.send({ content: i18n.__("volume.errorNotQueue") }).catch(console.error);

if (!canModifyQueue(guildMemer!))
return channel.send({ content: i18n.__("volume.errorNotChannel") }).catch(console.error);

if (!volumeArg || volumeArg === queue.volume)
return channel
.send({ content: i18n.__mf("volume.currentVolume", { volume: queue.volume }) })
.catch(console.error);

if (isNaN(volumeArg)) return channel.send({ content: i18n.__("volume.errorNotNumber") }).catch(console.error);

if (Number(volumeArg) > 100 || Number(volumeArg) < 0)
return channel.send({ content: i18n.__("volume.errorNotValid") }).catch(console.error);

queue.volume = volumeArg;
queue.resource.volume?.setVolumeLogarithmic(volumeArg / 100);
res.status(200);
res.send(i18n.__mf("volume.result", { arg: volumeArg }));

return channel.send({ content: i18n.__mf("volume.result", { arg: volumeArg }) }).catch(console.error);
});

app.post("/play", async (req: Request, res: Response) => {
const guildId = req.headers.guildid?.toString()!;
const userId = req.headers.userid?.toString()!;
const channelId = req.headers.channelid?.toString()!;
const textChannel = <TextChannel>bot.client.channels.cache.get(channelId);
let argSongName = req.headers.song?.toString()!;
const queue = bot.queues.get(guildId);
const guildMember = bot.client.guilds.cache.get(guildId)?.members.cache.get(userId);
const { channel } = guildMember!.voice;

if (!channel) return textChannel.send({ content: i18n.__("play.errorNotChannel") }).catch(console.error);

if (queue && channel.id !== queue.connection.joinConfig.channelId)
return textChannel
.send({
content: i18n.__mf("play.errorNotInSameChannel", { user: bot.client.user!.username })
})
.catch(console.error);

if (!argSongName)
return textChannel.send({ content: i18n.__mf("play.usageReply", { prefix: bot.prefix }) }).catch(console.error);

const url = argSongName;

let song = new Song({ title: "", url: "", duration: 0 });

try {
song = await Song.from(url, url);
} catch (error: any) {
console.error(error);

if (error.name == "NoResults")
return textChannel
.send({ content: i18n.__mf("play.errorNoResults", { url: `<${url}>` }) })
.catch(console.error);

if (error.name == "InvalidURL")
return textChannel
.send({ content: i18n.__mf("play.errorInvalidURL", { url: `<${url}>` }) })
.catch(console.error);
}

if (queue) {
queue.enqueue(song);
res.status(200);
res.send(i18n.__mf("play.result", { title: song.title, author: userId }));
return textChannel
.send({ content: i18n.__mf("play.queueAdded", { title: song.title, author: userId }) })
.catch(console.error);
}

res.status(500);
res.send("Something went wrong somewhere, somehow");
});

app.listen(port, () => {
console.log(`Server is running at ip http://${config.SERVER_IP}:${port}`);
});
Expand Down

0 comments on commit d168fbf

Please sign in to comment.