-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A TypeScript version would be great, it's very hard to upgrade as-is #23
Comments
Here's // Slash Commands Deployment Script
// https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands/
// Importing modules using ES6 syntax
import { REST, RESTPutAPIApplicationGuildCommandsJSONBody, Routes } from "discord.js";
import { config } from "dotenv";
import fs from "node:fs";
config(); // Using dotenv config function directly
if (!process.env.TOKEN || !process.env.CLIENTID || !process.env.SERVERID) {
throw new Error("Missing one or more required environment variables: TOKEN, CLIENTID, SERVERID");
}
type Command = RESTPutAPIApplicationGuildCommandsJSONBody[number];
const commands: Command[] = [];
const commandFiles = fs.readdirSync("./commands").filter((file) => file.endsWith(".ts"));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = await import(`./commands/${file}`); // Using dynamic import
if ("data" in command && "execute" in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command ${file} is missing a required "data" or "execute" property.`);
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.TOKEN);
// and deploy your commands!
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(Routes.applicationGuildCommands(process.env.CLIENTID, process.env.SERVERID), {
body: commands,
});
if (Array.isArray(data)) {
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
}
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
} |
Thanks! Finally got around to trying this. It's working with Bun but will it work with Node? I don't have my project set up properly for For instance, these:
Can we |
@hippietrail To run typescript in node, usually it is useful to use a package to do so, eg. tsx (https://www.npmjs.com/package/tsx), which can run* typescript files in node. To convert a node project in javascript to typescript with tsx:
(Here, You can check out my branch where i have converted the code to typescript to run with *Behind the scenes, Alternatively, a node project can be setup by using |
The new JS version of this came out just when I was looking to learn to make a Discord bot. It worked great after previous false starts in other languages.
But after getting to a certain point I tried to convert it to TypeScript and found it far beyond my capabilities. Especially the
deploy-command.js
The text was updated successfully, but these errors were encountered: