89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
// Require the necessary discord.js classes
|
|
import dotenv from 'dotenv';
|
|
dotenv.config()
|
|
import { Client, Events, GatewayIntentBits, MessageFlags, REST, Routes } from 'discord.js';
|
|
import { commands } from "./commands";
|
|
import { AudioPlayerState, createAudioPlayer } from '@discordjs/voice';
|
|
import { connectToChannel, playSong } from './util/helpers';
|
|
import { updatePlayer } from './playback';
|
|
import SpotifyWebApi from 'spotify-web-api-node';
|
|
|
|
export const DATA_DIR = "./data";
|
|
|
|
// AUDIO
|
|
export const player = createAudioPlayer();
|
|
player.on('stateChange', (oldState: AudioPlayerState, newState: AudioPlayerState) => {
|
|
updatePlayer()
|
|
});
|
|
|
|
|
|
// Create a new client instance
|
|
const client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.GuildVoiceStates]
|
|
});
|
|
|
|
export const spotify = new SpotifyWebApi({
|
|
clientId: process.env.SPOTIFY_CLIENT_ID,
|
|
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
|
|
});
|
|
|
|
|
|
// When the client is ready, run this code (only once).
|
|
// The distinction between `client: Client<boolean>` and `readyClient: Client<true>` is important for TypeScript developers.
|
|
// It makes some properties non-nullable.
|
|
client.once(Events.ClientReady, (readyClient) => {
|
|
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
|
registerCommands()
|
|
});
|
|
|
|
async function registerCommands() {
|
|
const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
|
|
const registry = Object.values(commands).map((cmd) => cmd.register().toJSON())
|
|
console.log("komendy: \n", registry.map(r => `${r.name} : ${r.description}`).join("\n"))
|
|
await rest.put(Routes.applicationGuildCommands(
|
|
process.env.DISCORD_APP_ID!,
|
|
process.env.GUILD_ID!
|
|
), {
|
|
body: registry
|
|
});
|
|
// await rest.put(Routes.applicationCommands(
|
|
// process.env.DISCORD_APP_ID!,
|
|
// ), {
|
|
// body: registry
|
|
// });
|
|
}
|
|
|
|
|
|
// Log in to Discord with your client's token
|
|
client.login(process.env.DISCORD_TOKEN!);
|
|
|
|
client.on(Events.InteractionCreate, async (interaction) => {
|
|
if (!interaction.isChatInputCommand()) return;
|
|
const cmd = commands[interaction.commandName];
|
|
if (!cmd) {
|
|
console.error(`No command matching ${interaction.commandName} was found.`);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await cmd.execute(interaction)
|
|
} catch (error) {
|
|
console.error(error);
|
|
if (interaction.replied || interaction.deferred) {
|
|
await interaction.followUp({
|
|
content: 'There was an error while executing this command!',
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
} else {
|
|
await interaction.reply({
|
|
content: 'There was an error while executing this command!',
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
}
|
|
}
|
|
})
|
|
|