-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c49632b
commit bc8a9dc
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
DISCORD_BOT_TOKEN= | ||
CLIENT_ID= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Client, GatewayIntentBits } from "discord.js"; | ||
import fs from "fs/promises"; | ||
import path from "path"; | ||
import { Command } from "./types/Command"; | ||
|
||
export class PlayNowApplication { | ||
private client: Client; | ||
public commands: Map<string, Command>; | ||
public static commandDir = path.join(__dirname, "commands"); | ||
|
||
constructor() { | ||
this.client = new Client({ | ||
intents: [ | ||
GatewayIntentBits.Guilds, | ||
GatewayIntentBits.GuildMessages, | ||
GatewayIntentBits.GuildVoiceStates | ||
] | ||
}); | ||
this.commands = new Map<string, Command>(); | ||
} | ||
|
||
async start() { | ||
await this.commandSetup(); | ||
this.eventSetup(); | ||
this.client.login(process.env.DISCORD_BOT_TOKEN) | ||
} | ||
|
||
private async commandSetup() { | ||
const dir = await fs.opendir(PlayNowApplication.commandDir) | ||
for await (const dirent of dir) { | ||
console.log(`Loading command from ${dirent.name}`); | ||
const importedModule = await import(path.join(PlayNowApplication.commandDir, dirent.name)) | ||
this.commands.set(importedModule.data.name, importedModule); | ||
} | ||
} | ||
|
||
private eventSetup() { | ||
this.client.once("ready", async (client) => { | ||
console.log("Client ready"); | ||
}); | ||
|
||
process.on("SIGINT", () => { | ||
process.exit(0); | ||
}); | ||
|
||
process.on("exit", () => { | ||
this.client.destroy(); | ||
console.log("Logged out"); | ||
}); | ||
|
||
this.client.on("interactionCreate", async (interaction) => { | ||
|
||
if (!interaction.isChatInputCommand()) | ||
return; | ||
|
||
const command = this.commands.get(interaction.commandName); | ||
|
||
if (!command) | ||
return; | ||
|
||
try { | ||
await command.execute(interaction); | ||
} catch (error) { | ||
console.error(error); | ||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { SlashCommandBuilder } from "discord.js"; | ||
import { Command } from "../types/Command"; | ||
|
||
const play: Command = { | ||
data: new SlashCommandBuilder().setName("p").setDescription("Play a song"), | ||
execute: async (interaction) => { | ||
interaction.reply("hello") | ||
} | ||
}; | ||
|
||
export = play; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const { REST } = require('@discordjs/rest'); | ||
const { Routes } = require('discord.js'); | ||
|
||
const commands = []; | ||
const commandsPath = path.join(__dirname, 'commands'); | ||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); | ||
|
||
for (const file of commandFiles) { | ||
const filePath = path.join(commandsPath, file); | ||
const command = require(filePath); | ||
commands.push(command.data.toJSON()); | ||
} | ||
|
||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_BOT_TOKEN); | ||
|
||
rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { body: commands }) | ||
.then(() => console.log('Successfully registered application commands.')) | ||
.catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {PlayNowApplication} from "./PlayNowApplication" | ||
|
||
const app = new PlayNowApplication(); | ||
app.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js" | ||
|
||
export interface Command { | ||
data: SlashCommandBuilder; | ||
execute: (interaction: ChatInputCommandInteraction)=> Promise<void>; | ||
} |