of fucking course its oop again
This commit is contained in:
parent
6359da85ea
commit
6ea0e02ee8
4 changed files with 134 additions and 52 deletions
7
src/command.ts
Normal file
7
src/command.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import {ChatInputCommandInteraction, SharedSlashCommand} from "discord.js";
|
||||||
|
|
||||||
|
export class Command {
|
||||||
|
constructor(){}
|
||||||
|
async run (interaction: ChatInputCommandInteraction, config): Promise<void>{};
|
||||||
|
slashCommand: SharedSlashCommand
|
||||||
|
}
|
||||||
79
src/commands/nowplaying.ts
Normal file
79
src/commands/nowplaying.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import { Command } from "../command.ts";
|
||||||
|
import {
|
||||||
|
ActionRowBuilder,
|
||||||
|
ApplicationIntegrationType,
|
||||||
|
ButtonBuilder,
|
||||||
|
ButtonStyle,
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
EmbedBuilder,
|
||||||
|
InteractionContextType,
|
||||||
|
SlashCommandBuilder
|
||||||
|
} from "discord.js";
|
||||||
|
|
||||||
|
import { getSongOnPreferredProvider } from "../helper.ts"
|
||||||
|
|
||||||
|
function keepV(url) {
|
||||||
|
const urlObj = new URL(url);
|
||||||
|
const vParam = urlObj.searchParams.get("v");
|
||||||
|
|
||||||
|
urlObj.search = "";
|
||||||
|
|
||||||
|
if (vParam) {
|
||||||
|
urlObj.searchParams.set("v", vParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
return urlObj.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class PingCommand extends Command {
|
||||||
|
async run(interaction: ChatInputCommandInteraction, config) {
|
||||||
|
await interaction.deferReply()
|
||||||
|
|
||||||
|
|
||||||
|
const meow = await fetch(`https://api.listenbrainz.org/1/user/${config.listenbrainzAccount}/playing-now`).then((res) => res.json());
|
||||||
|
if (!meow) {
|
||||||
|
await interaction.followUp("something shat itself!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (meow.payload.count === 0) {
|
||||||
|
await interaction.followUp("user isnt listening to music");
|
||||||
|
} else {
|
||||||
|
const track_metadata = meow.payload.listens[0].track_metadata
|
||||||
|
const link = keepV(track_metadata.additional_info.origin_url)
|
||||||
|
|
||||||
|
const preferredApi = getSongOnPreferredProvider(await fetch(`https://api.song.link/v1-alpha.1/links?url=${link}`).then(a => a.json()), link)
|
||||||
|
if (!preferredApi) {
|
||||||
|
interaction.followUp("song not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setAuthor({
|
||||||
|
name: preferredApi.artist,
|
||||||
|
})
|
||||||
|
.setTitle(preferredApi.title)
|
||||||
|
.setThumbnail(preferredApi.thumbnailUrl)
|
||||||
|
.setFooter({
|
||||||
|
text: "amy jr",
|
||||||
|
});
|
||||||
|
const nya = new ActionRowBuilder<ButtonBuilder>().addComponents(new ButtonBuilder().setURL(preferredApi.link).setLabel("link").setStyle(ButtonStyle.Link))
|
||||||
|
interaction.followUp({
|
||||||
|
components: [
|
||||||
|
nya
|
||||||
|
],
|
||||||
|
embeds: [embed]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
slashCommand = new SlashCommandBuilder()
|
||||||
|
.setName("nowplaying")
|
||||||
|
.setDescription("balls").setIntegrationTypes([
|
||||||
|
ApplicationIntegrationType.UserInstall
|
||||||
|
])
|
||||||
|
.setContexts([
|
||||||
|
InteractionContextType.BotDM,
|
||||||
|
InteractionContextType.Guild,
|
||||||
|
InteractionContextType.PrivateChannel
|
||||||
|
]);
|
||||||
|
}
|
||||||
26
src/commands/ping.ts
Normal file
26
src/commands/ping.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import {Command} from "../command.ts";
|
||||||
|
import {
|
||||||
|
ApplicationIntegrationType,
|
||||||
|
ChatInputCommandInteraction,
|
||||||
|
InteractionContextType,
|
||||||
|
SlashCommandBuilder
|
||||||
|
} from "discord.js";
|
||||||
|
|
||||||
|
export default class PingCommand extends Command {
|
||||||
|
async run(interaction: ChatInputCommandInteraction, config) {
|
||||||
|
await interaction.reply({
|
||||||
|
content: 'Pong!',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
slashCommand = new SlashCommandBuilder()
|
||||||
|
.setName("ping")
|
||||||
|
.setDescription("Pong!").setIntegrationTypes([
|
||||||
|
ApplicationIntegrationType.UserInstall
|
||||||
|
])
|
||||||
|
.setContexts([
|
||||||
|
InteractionContextType.BotDM,
|
||||||
|
InteractionContextType.Guild,
|
||||||
|
InteractionContextType.PrivateChannel
|
||||||
|
]);
|
||||||
|
}
|
||||||
74
src/index.ts
74
src/index.ts
|
|
@ -16,6 +16,13 @@ import {
|
||||||
SlashCommandBuilder
|
SlashCommandBuilder
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { getSongOnPreferredProvider } from "./helper.ts";
|
import { getSongOnPreferredProvider } from "./helper.ts";
|
||||||
|
import path from "node:path";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import { Command } from "./command.ts";
|
||||||
|
import { fileURLToPath } from "url";
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: Object.keys(GatewayIntentBits).map((a) => {
|
intents: Object.keys(GatewayIntentBits).map((a) => {
|
||||||
|
|
@ -24,38 +31,22 @@ const client = new Client({
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
function keepV(url) {
|
const commands: Command[] = []
|
||||||
const urlObj = new URL(url);
|
|
||||||
const vParam = urlObj.searchParams.get("v");
|
|
||||||
|
|
||||||
urlObj.search = "";
|
|
||||||
|
|
||||||
if (vParam) {
|
const commandDir = path.join(__dirname, "commands");
|
||||||
urlObj.searchParams.set("v", vParam);
|
for (const file of fs.readdirSync(commandDir)) {
|
||||||
}
|
let command = await import(path.join(commandDir, file));
|
||||||
|
commands.push(new command.default())
|
||||||
return urlObj.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const commands = [
|
const commandLookup = Object.fromEntries(commands.map(it => [it.slashCommand.name, it]))
|
||||||
new SlashCommandBuilder().setName("nowplaying")
|
|
||||||
.setDescription("balls")
|
|
||||||
.setIntegrationTypes([
|
|
||||||
ApplicationIntegrationType.UserInstall
|
|
||||||
])
|
|
||||||
.setContexts([
|
|
||||||
InteractionContextType.BotDM,
|
|
||||||
InteractionContextType.Guild,
|
|
||||||
InteractionContextType.PrivateChannel
|
|
||||||
])
|
|
||||||
].map(command => command.toJSON())
|
|
||||||
|
|
||||||
|
|
||||||
client.once(Events.ClientReady, async () => {
|
client.once(Events.ClientReady, async () => {
|
||||||
console.log("Ready");
|
console.log("Ready");
|
||||||
const rest = new REST().setToken(config.token);
|
const rest = new REST().setToken(config.token);
|
||||||
const data = await rest.put(
|
const data = await rest.put(
|
||||||
Routes.applicationCommands(client.user.id), { body: commands },
|
Routes.applicationCommands(client.user.id), { body: commands.map(command => command.slashCommand.toJSON()) },
|
||||||
);
|
);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
|
||||||
|
|
@ -65,38 +56,17 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||||
if (!interaction.isChatInputCommand()) return;
|
if (!interaction.isChatInputCommand()) return;
|
||||||
|
|
||||||
const { commandName } = interaction;
|
const { commandName } = interaction;
|
||||||
if (commandName !== "nowplaying") return;
|
|
||||||
await interaction.deferReply()
|
|
||||||
|
|
||||||
|
const command = commandLookup[commandName]
|
||||||
const meow = await fetch(`https://api.listenbrainz.org/1/user/${config.listenbrainzAccount}/playing-now`).then((res) => res.json());
|
if (!command) {
|
||||||
if (!meow) {
|
console.error("unknown command: " + commandName)
|
||||||
interaction.followUp("something shat itself!");
|
return
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (meow.payload.count === 0) {
|
|
||||||
interaction.followUp("user isnt listening to music");
|
|
||||||
} else {
|
|
||||||
const track_metadata = meow.payload.listens[0].track_metadata
|
|
||||||
const link = keepV(track_metadata.additional_info.origin_url)
|
|
||||||
|
|
||||||
const preferredApi = getSongOnPreferredProvider(await fetch(`https://api.song.link/v1-alpha.1/links?url=${link}`).then(a => a.json()), link)
|
try {
|
||||||
const embed = new EmbedBuilder()
|
await command.run(interaction, config);
|
||||||
.setAuthor({
|
} catch {
|
||||||
name: preferredApi.artist,
|
interaction.reply("something sharted itself")
|
||||||
})
|
|
||||||
.setTitle(preferredApi.title)
|
|
||||||
.setThumbnail(preferredApi.thumbnailUrl)
|
|
||||||
.setFooter({
|
|
||||||
text: "amy jr",
|
|
||||||
});
|
|
||||||
const nya = new ActionRowBuilder<ButtonBuilder>().addComponents(new ButtonBuilder().setURL(preferredApi.link).setLabel("link").setStyle(ButtonStyle.Link))
|
|
||||||
interaction.followUp({
|
|
||||||
components: [
|
|
||||||
nya
|
|
||||||
],
|
|
||||||
embeds: [embed]
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue