Skip to content

Commit

Permalink
Scaffold App
Browse files Browse the repository at this point in the history
  • Loading branch information
Babbiorsetto committed Jul 24, 2022
1 parent c49632b commit bc8a9dc
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DISCORD_BOT_TOKEN=
CLIENT_ID=
69 changes: 69 additions & 0 deletions src/PlayNowApplication.ts
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 });
}
});
}
}
11 changes: 11 additions & 0 deletions src/commands/play.ts
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;
20 changes: 20 additions & 0 deletions src/deploy-commands.js
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);
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {PlayNowApplication} from "./PlayNowApplication"

const app = new PlayNowApplication();
app.start();
6 changes: 6 additions & 0 deletions src/types/Command.ts
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>;
}

0 comments on commit bc8a9dc

Please sign in to comment.