Skip to content

Commit 56931ff

Browse files
committed
transpiled
1 parent b46f568 commit 56931ff

5 files changed

Lines changed: 120 additions & 118 deletions

File tree

.gitignore

Lines changed: 17 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,36 @@
1-
# Logs
2-
logs
3-
*.log
1+
# Node.js
2+
node_modules/
43
npm-debug.log*
54
yarn-debug.log*
65
yarn-error.log*
7-
lerna-debug.log*
8-
.pnpm-debug.log*
9-
10-
# Diagnostic reports (https://nodejs.org/api/report.html)
11-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12-
13-
# Runtime data
14-
pids
15-
*.pid
16-
*.seed
17-
*.pid.lock
18-
19-
# Directory for instrumented libs generated by jscoverage/JSCover
20-
lib-cov
21-
22-
# Coverage directory used by tools like istanbul
23-
coverage
24-
*.lcov
25-
26-
# nyc test coverage
27-
.nyc_output
286

29-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30-
.grunt
31-
32-
# Bower dependency directory (https://bower.io/)
33-
bower_components
34-
35-
# node-waf configuration
36-
.lock-wscript
7+
# TypeScript
8+
*.tsbuildinfo
379

38-
# Compiled binary addons (https://nodejs.org/api/addons.html)
39-
build/Release
10+
# Logs
11+
logs
12+
*.log
13+
logs/*.log
4014

4115
# Dependency directories
42-
node_modules/
4316
jspm_packages/
4417

45-
# Snowpack dependency directory (https://snowpack.dev/)
46-
web_modules/
47-
48-
# TypeScript cache
49-
*.tsbuildinfo
50-
5118
# Optional npm cache directory
5219
.npm
5320

5421
# Optional eslint cache
5522
.eslintcache
5623

57-
# Optional stylelint cache
58-
.stylelintcache
59-
60-
# Microbundle cache
61-
.rpt2_cache/
62-
.rts2_cache_cjs/
63-
.rts2_cache_es/
64-
.rts2_cache_umd/
65-
66-
# Optional REPL history
67-
.node_repl_history
24+
# Output directories
25+
dist/
26+
build/
6827

69-
# Output of 'npm pack'
70-
*.tgz
28+
# IDE specific files
29+
.vscode/
30+
.idea/
31+
.DS_Store
7132

72-
# Yarn Integrity file
73-
.yarn-integrity
74-
75-
# dotenv environment variable files
33+
# Environment variables
7634
.env
77-
.env.development.local
78-
.env.test.local
79-
.env.production.local
8035
.env.local
81-
82-
# parcel-bundler cache (https://parceljs.org/)
83-
.cache
84-
.parcel-cache
85-
86-
# Next.js build output
87-
.next
88-
out
89-
90-
# Nuxt.js build / generate output
91-
.nuxt
92-
dist
93-
94-
# Gatsby files
95-
.cache/
96-
# Comment in the public line in if your project uses Gatsby and not Next.js
97-
# https://nextjs.org/blog/next-9-1#public-directory-support
98-
# public
99-
100-
# vuepress build output
101-
.vuepress/dist
102-
103-
# vuepress v2.x temp and cache directory
104-
.temp
105-
.cache
106-
107-
# Docusaurus cache and generated files
108-
.docusaurus
109-
110-
# Serverless directories
111-
.serverless/
112-
113-
# FuseBox cache
114-
.fusebox/
115-
116-
# DynamoDB Local files
117-
.dynamodb/
118-
119-
# TernJS port file
120-
.tern-port
121-
122-
# Stores VSCode versions used for testing VSCode extensions
123-
.vscode-test
124-
125-
# yarn v2
126-
.yarn/cache
127-
.yarn/unplugged
128-
.yarn/build-state.yml
129-
.yarn/install-state.gz
130-
.pnp.*
131-
132-
# tests
133-
tests
36+
.env.*.local

index.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { REST } from "@discordjs/rest";
2+
import { readdirSync } from "node:fs";
3+
import path from "node:path";
4+
const Routes = {
5+
commands: (appId) => {
6+
return `/applications/${appId}/commands`;
7+
},
8+
command: (appId, cmdId) => {
9+
return `/applications/${appId}/commands/${cmdId}`;
10+
},
11+
guildCommands: (appId, guildId) => {
12+
return `/applications/${appId}/guilds/${guildId}/commands`;
13+
},
14+
guildCommand: (appId, guildId, cmdId) => {
15+
return `/applications/${appId}/guilds/${guildId}/commands/${cmdId}`;
16+
},
17+
};
18+
/**
19+
* Create, update and delete global and guild application commands.
20+
*
21+
* To update guild-specific commands correctly, make sure the bot is logged in.\
22+
* Otherwise the check for a guild ID is omitted, and you could make pointless requests which can also result in an error
23+
*/
24+
export async function deployCommands(folderPath, opts) {
25+
var _a;
26+
if (!opts.appToken || !opts.appId) {
27+
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
28+
}
29+
let commands = [];
30+
let privateCommands = [];
31+
const commandFiles = readdirSync(folderPath).filter((file) => file.endsWith(".js"));
32+
if (opts.logs)
33+
console.log(`🔁 Started refreshing global and guild commands.`);
34+
try {
35+
const rest = new REST().setToken(opts.appToken);
36+
for (const file of commandFiles) {
37+
const filePath = path.join(folderPath, file);
38+
const command = require(filePath);
39+
if (!("data" in command)) {
40+
console.error(`- Command '${command.name}' is missing the 'data' property!`);
41+
continue;
42+
}
43+
else if ("data" in command && Boolean((_a = command.ignore) !== null && _a !== void 0 ? _a : false)) {
44+
if (opts.logs)
45+
console.log(`- Command '${command.data.name}' is ignored!`);
46+
continue;
47+
}
48+
if ((command.guildIds || []).length > 0) {
49+
privateCommands.push({
50+
data: command.data,
51+
guildIds: command.guildIds,
52+
});
53+
}
54+
else {
55+
commands.push(command.data);
56+
}
57+
}
58+
let data = await rest.put(Routes.commands(opts.appId), {
59+
body: commands,
60+
});
61+
if (opts.logs)
62+
console.log(`✅ ${data.length} global commands refreshed`);
63+
for (let cmd of privateCommands) {
64+
for (let gid of cmd.guildIds) {
65+
data = null;
66+
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
67+
body: cmd.data,
68+
});
69+
if (opts.logs)
70+
console.log(`✅ Guild command '${data.name}' refreshed`);
71+
}
72+
}
73+
return true;
74+
}
75+
catch (err) {
76+
console.error("❌ Error while refreshing commands:", err);
77+
return false;
78+
}
79+
}
80+
/**
81+
* Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
82+
*/
83+
export async function deleteCommand(commandId, opts) {
84+
var _a;
85+
const guildId = (_a = opts.guildId) !== null && _a !== void 0 ? _a : null;
86+
const commandPath = guildId
87+
? Routes.guildCommand(opts.appId, guildId, commandId)
88+
: Routes.command(opts.appId, commandId);
89+
if (commandId.match(/^\d+$/i)) {
90+
await new REST({ version: "10" })
91+
.setToken(opts.appToken)
92+
.delete(commandPath);
93+
}
94+
else {
95+
throw new Error("'commandId' is not a only-number-string!");
96+
}
97+
return;
98+
}

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { REST } from "@discordjs/rest";
22
import { readdirSync } from "node:fs";
33
import path from "node:path";
4-
import { DeleteOptions, DeployOptions } from "./types";
4+
import { DeleteOptions, DeployOptions } from "./types.js";
55

66
const Routes = {
77
commands: (appId: string): `/${string}` => {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "djs-command-helper",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "A simple, easy to use module that houses functions to can refresh global and guild specific commands for your Discord App.",
55
"main": "./index.js",
66
"types": "./types.d.ts",
7-
"scripts": {},
7+
"type": "module",
8+
"scripts": { },
89
"keywords": [
910
"discord",
1011
"discord.js",

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2016",
3+
"target": "ES2017",
44
"module": "NodeNext",
55
"rootDir": "./",
66
"moduleResolution": "NodeNext",

0 commit comments

Comments
 (0)