deploy-commands.ts
1 import { PermissionFlagsBits, REST, Routes, SlashCommandBuilder } from "discord.js"; 2 import { DEV_COMMAND_POSTFIX } from "./config.ts"; 3 4 if (!process.env["BOT_ID"]) throw new Error("You must provide a BOT_ID env variable"); 5 6 let guildId: string | undefined = undefined; 7 const clear = process.argv.includes("--clear"); 8 9 const idx = process.argv.indexOf("--guild"); 10 if (idx !== -1 && idx < process.argv.length - 1) { 11 guildId = process.argv[idx + 1]; 12 } 13 14 let commands = [ 15 new SlashCommandBuilder() 16 .setName("pat") 17 .setDescription("Pats a user") 18 .addUserOption((option) => 19 option.setName("user").setDescription("The user to pat").setRequired(true) 20 ), 21 22 new SlashCommandBuilder().setName("help").setDescription("Shows a list of commands"), 23 24 new SlashCommandBuilder() 25 .setName("sub") 26 .setDescription("Get a random image from a subreddit") 27 .addStringOption((option) => 28 option 29 .setName("subreddit") 30 .setDescription("The subreddit to search in") 31 .setRequired(true) 32 ) 33 .addBooleanOption((option) => 34 option 35 .setName("nsfw") 36 .setDescription("Whether to fetch only NSFW posts") 37 .setRequired(false) 38 ) 39 .addBooleanOption((option) => 40 option.setName("force").setDescription("Force fetch posts").setRequired(false) 41 ), 42 43 new SlashCommandBuilder() 44 .setName("urban") 45 .setDescription("Searches for a term on Urban Dictionary") 46 .addStringOption((option) => 47 option.setName("term").setDescription("The term to search for").setRequired(false) 48 ) 49 .addBooleanOption((option) => 50 option 51 .setName("random") 52 .setDescription("Whether to search for random terms") 53 .setRequired(false) 54 ), 55 56 new SlashCommandBuilder() 57 .setName("convert") 58 .setDescription("Converts from one currency to another") 59 .addStringOption((option) => 60 option.setName("from").setDescription("The currency to convert from").setRequired(true) 61 ) 62 .addStringOption((option) => 63 option.setName("to").setDescription("The currency to convert to").setRequired(true) 64 ) 65 .addNumberOption((option) => 66 option 67 .setName("amount") 68 .setDescription("The amount to convert") 69 .setRequired(false) 70 .setMinValue(Number.MIN_VALUE) 71 ), 72 73 new SlashCommandBuilder() 74 .setName("qr") 75 .setDescription("Generates a QR code") 76 .addStringOption((option) => 77 option.setName("data").setDescription("The data to encode").setRequired(true) 78 ), 79 80 new SlashCommandBuilder() 81 .setName("avatar") 82 .setDescription("Gets the avatar of a user") 83 .addUserOption((option) => 84 option 85 .setName("user") 86 .setDescription("The user to get the avatar of (You if omitted)") 87 .setRequired(false) 88 ), 89 90 new SlashCommandBuilder() 91 .setName("leet") 92 .setDescription("Converts text to leet speak") 93 .addStringOption((option) => 94 option.setName("input").setDescription("The text to convert").setRequired(true) 95 ), 96 97 new SlashCommandBuilder() 98 .setName("beautiful") 99 .setDescription("Generates a beautiful image") 100 .addUserOption((option) => 101 option 102 .setName("user") 103 .setDescription("The user to get the avatar of (You if omitted)") 104 .setRequired(false) 105 ), 106 107 new SlashCommandBuilder() 108 .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild) 109 .setDMPermission(false) 110 .setName("prefix") 111 .setDescription("Updates the prefix for the server") 112 .addStringOption((option) => 113 option.setName("prefix").setDescription("The new prefix").setRequired(true) 114 ), 115 ].map((command) => command.toJSON()); 116 117 if (process.env.DEV_MODE === "true") { 118 commands = commands.map((command) => { 119 command.name = `${command.name}${DEV_COMMAND_POSTFIX}`; 120 command.description = `${command.description} (dev)`; 121 return command; 122 }); 123 } 124 125 const rest = new REST({ version: "10" }).setToken(process.env.BOT_TOKEN); 126 127 try { 128 if (guildId) { 129 await rest.put(Routes.applicationGuildCommands(process.env["BOT_ID"], guildId), { 130 body: clear ? [] : commands, 131 }); 132 const msg = clear 133 ? "Successfully cleared all commands in your test guild." 134 : "Successfully registered all commands in your test guild."; 135 console.log(msg); 136 } else { 137 await rest.put(Routes.applicationCommands(process.env["BOT_ID"]), { 138 body: clear ? [] : commands, 139 }); 140 const msg = clear 141 ? "Successfully cleared all commands globally." 142 : "Successfully registered all commands globally."; 143 console.log(msg); 144 } 145 } catch (error) { 146 console.error(error); 147 }