You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A TypeScript version would be great, it's very hard to upgrade as-is #23
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
// Slash Commands Deployment Script// https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands/// Importing modules using ES6 syntaximport{REST,RESTPutAPIApplicationGuildCommandsJSONBody,Routes}from"discord.js";import{config}from"dotenv";importfsfrom"node:fs";config();// Using dotenv config function directlyif(!process.env.TOKEN||!process.env.CLIENTID||!process.env.SERVERID){thrownewError("Missing one or more required environment variables: TOKEN, CLIENTID, SERVERID");}typeCommand=RESTPutAPIApplicationGuildCommandsJSONBody[number];constcommands: Command[]=[];constcommandFiles=fs.readdirSync("./commands").filter((file)=>file.endsWith(".ts"));// Grab the SlashCommandBuilder#toJSON() output of each command's data for deploymentfor(constfileofcommandFiles){constcommand=awaitimport(`./commands/${file}`);// Using dynamic importif("data"incommand&&"execute"incommand){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 moduleconstrest=newREST().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 setconstdata=awaitrest.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 tsc or npx as I haven't really learned that stuff yet and I was surprised to find Coding Train hasn't done any TypeScript videos at all or I'd follow one.
To convert a node project in javascript to typescript with tsx:
Install tsx and typescript packages (npm install -D tsx typescript)
Convert js code to ts. (Can import ts files normally.)
Run your code with npx tsx bot.ts (instead of node bot.js)
(Here, npx is "Node Package Execute", which can run node packages as command line tools. Here, npx will run the package tsx, which runs typescript code with node.)
You can check out my branch where i have converted the code to typescript to run with tsx.
*Behind the scenes, tsx will compile the typescript code to js code, and run it normally with node, but this step is hidden from the user. There are other ts runtimes as well which run node, like ts-node.
Alternatively, a node project can be setup by using tsc - the typescript compiler, to compile source ts files into js files, and then the output js files are run with node. In this case, you'd write the ts files (with imports to other ts files), and all the ts code would be compiled to js by the compiler. node would then be pointed to the output files to run them. This is possible, but usually people just use a package like tsx which does all this behind the scenes.
Activity
dipamsen commentedon Dec 16, 2023
Here's
deploy-command.jsconverted to typescript:hippietrail commentedon Dec 23, 2023
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
tscornpxas I haven't really learned that stuff yet and I was surprised to find Coding Train hasn't done any TypeScript videos at all or I'd follow one.For instance, these:
Can we
import.tsfiles directly with Node?dipamsen commentedon Dec 23, 2023
@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:
npm install -D tsx typescript)npx tsx bot.ts(instead ofnode bot.js)(Here,
npxis "Node Package Execute", which can run node packages as command line tools. Here,npxwill run the packagetsx, which runs typescript code with node.)You can check out my branch where i have converted the code to typescript to run with
tsx.*Behind the scenes,
tsxwill compile the typescript code to js code, and run it normally with node, but this step is hidden from the user. There are other ts runtimes as well which run node, likets-node.Alternatively, a node project can be setup by using
tsc- the typescript compiler, to compile source ts files into js files, and then the output js files are run with node. In this case, you'd write the ts files (with imports to other ts files), and all the ts code would be compiled to js by the compiler. node would then be pointed to the output files to run them. This is possible, but usually people just use a package liketsxwhich does all this behind the scenes.