From b6056ad3db84cb375d37b4baf9b8b12455cc7481 Mon Sep 17 00:00:00 2001 From: Struck713 Date: Thu, 22 Feb 2024 19:09:06 -0500 Subject: [PATCH] Reorganized command structure --- src/app.ts | 2 +- src/commands/{ => admin}/deploy.ts | 6 +- src/commands/index.ts | 13 ++-- src/commands/{ => misc}/help.ts | 6 +- src/commands/{ => misc}/status.ts | 8 +-- src/commands/poll.ts | 103 ----------------------------- src/commands/{ => silly}/meme.ts | 4 +- src/commands/{ => silly}/oob.ts | 4 +- src/commands/voice/play.ts | 52 +++++++-------- src/lib/utils/embeds.ts | 6 +- 10 files changed, 49 insertions(+), 155 deletions(-) rename src/commands/{ => admin}/deploy.ts (84%) rename src/commands/{ => misc}/help.ts (82%) rename src/commands/{ => misc}/status.ts (80%) delete mode 100644 src/commands/poll.ts rename src/commands/{ => silly}/meme.ts (94%) rename src/commands/{ => silly}/oob.ts (87%) diff --git a/src/app.ts b/src/app.ts index 2e50424..05b57ca 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,7 +4,7 @@ import commands from "./commands"; import { StateManager } from "./lib/state"; import YTDlpWrap from "yt-dlp-wrap"; import { Embeds } from "./lib/utils/embeds"; -import { PollManager } from "./commands/poll"; +import { PollManager } from "./commands/admin/poll"; export const ytdl = new YTDlpWrap(youtube.binary_path); export const client = new Client({ diff --git a/src/commands/deploy.ts b/src/commands/admin/deploy.ts similarity index 84% rename from src/commands/deploy.ts rename to src/commands/admin/deploy.ts index cab5482..7e1ed03 100644 --- a/src/commands/deploy.ts +++ b/src/commands/admin/deploy.ts @@ -1,7 +1,7 @@ import { SlashCommandBuilder } from "discord.js"; -import { Command, deploy } from "../lib/command"; -import { Embeds } from "../lib/utils/embeds"; -import commands from "."; +import { Command, deploy } from "../../lib/command"; +import { Embeds } from "../../lib/utils/embeds"; +import commands from ".."; const MY_SNOWFLAKE = "140520164629151744"; diff --git a/src/commands/index.ts b/src/commands/index.ts index 316358a..2f44b0d 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -2,12 +2,11 @@ import { Play } from "../commands/voice/play"; import { Skip } from "../commands/voice/skip" import { Queue } from "../commands/voice/queue"; import { Stop } from "../commands/voice/stop"; -import { Deploy } from "../commands/deploy"; -import { Status } from "../commands/status"; -import { Meme } from "./meme"; -import { Help } from "./help"; -import { Poll } from "./poll"; -import { Oobinate } from "./oob"; +import { Deploy } from "./admin/deploy"; +import { Status } from "./misc/status"; +import { Meme } from "./silly/meme"; +import { Help } from "./misc/help"; +import { Oobinate } from "./silly/oob"; -const commands = [ Help, Play, Skip, Queue, Stop, Meme, Oobinate, Poll, Status, Deploy ]; +const commands = [ Help, Play, Skip, Queue, Stop, Meme, Oobinate, Status, Deploy ]; export default commands; \ No newline at end of file diff --git a/src/commands/help.ts b/src/commands/misc/help.ts similarity index 82% rename from src/commands/help.ts rename to src/commands/misc/help.ts index 3e5eb5c..89a57bf 100644 --- a/src/commands/help.ts +++ b/src/commands/misc/help.ts @@ -1,7 +1,7 @@ import { SlashCommandBuilder } from "discord.js"; -import { Command } from "../lib/command"; -import { Embeds } from "../lib/utils/embeds"; -import commands from "."; +import { Command } from "../../lib/command"; +import { Embeds } from "../../lib/utils/embeds"; +import commands from ".."; export const Help: Command = { data: new SlashCommandBuilder() diff --git a/src/commands/status.ts b/src/commands/misc/status.ts similarity index 80% rename from src/commands/status.ts rename to src/commands/misc/status.ts index e30e45e..b38d0a0 100644 --- a/src/commands/status.ts +++ b/src/commands/misc/status.ts @@ -1,8 +1,8 @@ import { SlashCommandBuilder } from "discord.js"; -import { Command } from "../lib/command"; -import { Embeds } from "../lib/utils/embeds"; -import { Style } from "../lib/utils/style"; -import Time from "../lib/utils/time"; +import { Command } from "../../lib/command"; +import { Embeds } from "../../lib/utils/embeds"; +import { Style } from "../../lib/utils/style"; +import Time from "../../lib/utils/time"; const STARTUP_TIME = Date.now(); diff --git a/src/commands/poll.ts b/src/commands/poll.ts deleted file mode 100644 index aa90484..0000000 --- a/src/commands/poll.ts +++ /dev/null @@ -1,103 +0,0 @@ -import { Events, SlashCommandBuilder } from "discord.js"; -import { Command } from "../lib/command"; -import { Pair, Text } from "../lib/utils/misc"; -import { client, pollManager } from "../app"; -import { Embeds } from "../lib/utils/embeds"; - -export class PollManager { - - private polls: Map[]>; - - constructor() { - this.polls = new Map(); - - client.addListener(Events.MessageReactionAdd, async (reaction, user) => { - console.log(reaction, user); - }); - client.addListener(Events.MessageReactionRemove, async (reaction, user) => { - console.log(reaction, user); - }); - } - - public create = (name: string, options: string[]) => this.polls.set(name, options.map(option => ({ key: option, value: 0 }))); - public get = (name: string) => this.polls.get(name); - -} - -// weirdly declare subcommands first -export const Poll: Command = { - data: new SlashCommandBuilder() - .addSubcommand(subcommand => - subcommand.setName('create') - .addStringOption(option => option.setName('name').setDescription('The name of the poll.').setRequired(true)) - .setDescription('Create a poll with up to 4 options.')) - .addSubcommand(subcommand => - subcommand.setName('add') - .setDescription('Add an option to a poll.') - .addStringOption(option => option.setName('name').setDescription('The name of the poll.').setRequired(true)) - .addStringOption(option => option.setName('option').setDescription('The option to add.').setRequired(true))) - .addSubcommand(subcommand => - subcommand.setName('show') - .addStringOption(option => option.setName('name').setDescription('The name of the poll.').setRequired(true)) - .setDescription('Display the poll in chat.')) - .setName('poll') - .setDescription('Manage or display a poll.'), - execute: async (client, interaction) => { - let { value: name } = interaction.options.get("name", true); - let poll = pollManager.get(name as string); - let subcommand = interaction.options.getSubcommand(); - - if (subcommand === "create") { - if (poll) { - await Embeds.error(interaction, `A poll with the name \`${name}\` already exists!`); - return; - } - - pollManager.create(name as string, []); - await Embeds.create() - .setAuthor({ name: "Poll" }) - .setTitle(name as string) - .setDescription("Poll has been created.") - .send(interaction); - return; - } - - if (subcommand === "add") { - if (!poll) { - await Embeds.error(interaction, `The poll named \`${name}\` does not exist!`); - return; - } - - if (poll.length >= 4) { - await Embeds.error(interaction, `You cannot add anymore options to \`${name}\`!`); - return; - } - - let { value: option } = interaction.options.get("option", true); - poll.push({ key: option as string, value: 0 }); - await Embeds.create() - .setAuthor({ name: "Poll" }) - .setTitle(name as string) - .setDescription(`Added option, \`${option}\`.`) - .send(interaction); - return; - } - - if (subcommand === "show") { - if (!poll) { - await Embeds.error(interaction, `The poll named \`${name}\` does not exist!`); - return; - } - - let fields = poll.map((pair, index) => ({ name: `${Text.NUMBERS[index + 1]} ${pair.key}`, value: Text.number(pair.value, "vote") })); - let message = await Embeds.create() - .setAuthor({ name: "Poll" }) - .setTitle(name as string) - .addFields(fields) - .send(interaction); - await Promise.all(poll.map((_, index) => message.react(Text.NUMBERS[index + 1]))); - return; - } - - }, -} \ No newline at end of file diff --git a/src/commands/meme.ts b/src/commands/silly/meme.ts similarity index 94% rename from src/commands/meme.ts rename to src/commands/silly/meme.ts index bde72b1..c9a39b9 100644 --- a/src/commands/meme.ts +++ b/src/commands/silly/meme.ts @@ -1,7 +1,7 @@ import { AttachmentBuilder, SlashCommandBuilder } from "discord.js"; -import { Command } from "../lib/command"; +import { Command } from "../../lib/command"; import axios from "axios"; -import { Embeds } from "../lib/utils/embeds"; +import { Embeds } from "../../lib/utils/embeds"; export const Meme: Command = { data: new SlashCommandBuilder() diff --git a/src/commands/oob.ts b/src/commands/silly/oob.ts similarity index 87% rename from src/commands/oob.ts rename to src/commands/silly/oob.ts index 67871d0..02850f0 100644 --- a/src/commands/oob.ts +++ b/src/commands/silly/oob.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder } from "discord.js"; -import { Command } from "../lib/command"; -import { Embeds } from "../lib/utils/embeds"; +import { Command } from "../../lib/command"; +import { Embeds } from "../../lib/utils/embeds"; export const Oobinate: Command = { data: new SlashCommandBuilder() diff --git a/src/commands/voice/play.ts b/src/commands/voice/play.ts index 6840033..4050260 100644 --- a/src/commands/voice/play.ts +++ b/src/commands/voice/play.ts @@ -8,9 +8,9 @@ import * as VoiceManager from "../../lib/voice"; export const Play: Command = { data: new SlashCommandBuilder() .setName("play") - .setDescription("Play sound in a voice channel using a URL.") + .setDescription("Play a song by search.") .addStringOption(option => option.setName("search").setRequired(true).setDescription("A valid YouTube search, picking the top result.")), - execute: async (client, interaction) => { + execute: async (_, interaction) => { if (!interaction.guild || !interaction.member) { await Embeds.error(interaction, "You are not in a guild!"); @@ -23,32 +23,18 @@ export const Play: Command = { return; } - let search = interaction.options.get("search", false); - if (search) { - let metadata = await YouTubeAPI.search(search.value as string); - if (!metadata) { - await Embeds.error(interaction, `Could not find anything associated with \`${search.value}\``); - return; - } - - let connection = VoiceManager.getVoiceFromGuildId(interaction.guild.id); - if (connection) { - connection.play(metadata); - Embeds.create() - .setAuthor({ name: "Added to queue" }) - .setTitle(metadata.getTitle()) - .setURL(metadata.getUrl()) - .setDescription(`by ${metadata.getAuthor()}`) - .setImage(metadata.getThumbnailUrl()) - .send(interaction); - return; - } - - connection = VoiceManager.initializeVoice(user.voice.channel); + let search = interaction.options.get("search", true); + let metadata = await YouTubeAPI.search(search.value as string); + if (!metadata) { + await Embeds.error(interaction, `Could not find anything associated with \`${search.value}\``); + return; + } + + let connection = VoiceManager.getVoiceFromGuildId(interaction.guild.id); + if (connection) { connection.play(metadata); - - await Embeds.create() - .setAuthor({ name: "Now Playing"}) + Embeds.create() + .setAuthor({ name: "Added to queue" }) .setTitle(metadata.getTitle()) .setURL(metadata.getUrl()) .setDescription(`by ${metadata.getAuthor()}`) @@ -57,7 +43,15 @@ export const Play: Command = { return; } - await Embeds.error(interaction, `Please specify either a search or valid YouTube URL.`); - return; + connection = VoiceManager.initializeVoice(user.voice.channel); + connection.play(metadata); + + await Embeds.create() + .setAuthor({ name: "Now Playing"}) + .setTitle(metadata.getTitle()) + .setURL(metadata.getUrl()) + .setDescription(`by ${metadata.getAuthor()}`) + .setImage(metadata.getThumbnailUrl()) + .send(interaction); }, } \ No newline at end of file diff --git a/src/lib/utils/embeds.ts b/src/lib/utils/embeds.ts index b6b0612..c579700 100644 --- a/src/lib/utils/embeds.ts +++ b/src/lib/utils/embeds.ts @@ -12,7 +12,11 @@ export namespace Embeds { .setColor(Style.Color.DEFAULT) .setTimestamp() .setFooter({ text: Style.ENGINE_VERSION, iconURL: Style.PROFILE_URL }); - return Object.assign(embed, { send: (interaction: CommandInteraction) => { return interaction.followUp({ embeds: [embed] }); } }); + return Object.assign(embed, { + send: (interaction: CommandInteraction) => { + return interaction.followUp({ embeds: [embed] }); + } + }); } export const error = async (interaction: CommandInteraction, message: string, subtitle?: string) => {