Advertisement
Guest User

Untitled

a guest
Apr 1st, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import fs from "fs";
  2. import {REST, Routes} from "discord.js";
  3. import ApiUtil from "./src/utils/ApiUtil";
  4. require('dotenv').config()
  5.  
  6. interface Command {
  7. name: string;
  8. description: string;
  9. disabled?: boolean;
  10. }
  11.  
  12. async function deployCommands() {
  13. const guildCommands = []
  14. const dmCommands = []
  15.  
  16. const commandFiles = []
  17. commandFiles.push.apply(commandFiles, fs.readdirSync(`${__dirname}/src/commands`).filter(file => file.endsWith('.ts')))
  18.  
  19. console.log(commandFiles)
  20.  
  21. for (const file of commandFiles) {
  22. const command = await import(`${__dirname}/src/commands/${file}`)
  23.  
  24. if (command.default.allowDm === true || command.default.allowDm === undefined) {
  25. dmCommands.push(command.default.data)
  26. }
  27. }
  28.  
  29. for (const file of commandFiles) {
  30. const command = await import(`${__dirname}/src/commands/${file}`)
  31.  
  32. if (!dmCommands.includes(command.default.data)) {
  33. guildCommands.push(command.default.data)
  34. }
  35. }
  36.  
  37. const mergedCommands: Command[] = guildCommands.concat(dmCommands).map(command => ({
  38. name: command.name,
  39. description: command.description,
  40. disabled: false,
  41. }));
  42.  
  43. if (await ApiUtil.deployCommandsToDatabase(mergedCommands)) {
  44. console.log('Successfully deployed commands to database.')
  45. }
  46.  
  47. const rest: REST = new REST({version: '10'}).setToken(process.env.BOT_TOKEN);
  48.  
  49. await (async (): Promise<void> => {
  50. try {
  51. console.log('Started refreshing application (/) commands.')
  52.  
  53. await rest.put(
  54. Routes.applicationCommands(process.env.BOT_ID),
  55. {body: dmCommands},
  56. )
  57.  
  58. await rest.put(
  59. Routes.applicationGuildCommands(process.env.BOT_ID, process.env.GUILD_ID),
  60. {body: guildCommands},
  61. )
  62.  
  63. console.log('Successfully reloaded application (/) commands.')
  64. } catch (error) {
  65. console.error(error)
  66. }
  67. })()
  68. }
  69.  
  70. deployCommands();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement