mirror of
https://github.com/sern-handler/cli
synced 2026-06-06 09:26:52 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f9a2aaca7 | ||
|
|
d1832e44ce | ||
|
|
e55ceedadd | ||
|
|
6bb212a3be | ||
|
|
2d6e65a1e6 |
14
CHANGELOG.md
14
CHANGELOG.md
@@ -2,6 +2,20 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [1.2.1](https://github.com/sern-handler/cli/compare/v1.2.0...v1.2.1) (2024-02-08)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* better error messages for publish ([d1832e4](https://github.com/sern-handler/cli/commit/d1832e44ce1b10aeb5b9dc902b7d35ab51c41ff3))
|
||||
|
||||
## [1.2.0](https://github.com/sern-handler/cli/compare/v1.1.0...v1.2.0) (2024-01-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* make application id optional, thanks [@trueharuu](https://github.com/trueharuu) ([#130](https://github.com/sern-handler/cli/issues/130)) ([2d6e65a](https://github.com/sern-handler/cli/commit/2d6e65a1e6073f605aab192b8cea33037a04af2c))
|
||||
|
||||
## [1.1.0](https://github.com/sern-handler/cli/compare/v1.0.3...v1.1.0) (2024-01-28)
|
||||
|
||||
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@sern/cli",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@sern/cli",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.1",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@esbuild-kit/cjs-loader": "^2.4.2",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@sern/cli",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.1",
|
||||
"description": "Official CLI for @sern/handler",
|
||||
"exports": "./dist/index.js",
|
||||
"bin": {
|
||||
|
||||
@@ -23,9 +23,7 @@ const getConfirmation = (args: Record<string,any> ) => {
|
||||
export async function commandClear(args: Record<string,any>) {
|
||||
dotenv.configDotenv({ path: args.env || resolve('.env') })
|
||||
const token = process.env.token || process.env.DISCORD_TOKEN;
|
||||
const appid = process.env.applicationId || process.env.APPLICATION_ID;
|
||||
assert(token, 'Could not find a token for this bot in .env or commandline. Do you have DISCORD_TOKEN in env?');
|
||||
assert(appid, 'Could not find an application id for this bot in .env or commandline. Do you have APPLICATION_ID in env?');
|
||||
|
||||
const confirmation = await getConfirmation(args);
|
||||
|
||||
@@ -34,7 +32,7 @@ export async function commandClear(args: Record<string,any>) {
|
||||
text: `Deleting ALL application commands...`,
|
||||
spinner: 'aesthetic',
|
||||
}).start();
|
||||
const rest = Rest.create(appid, token);
|
||||
const rest = await Rest.create(token);
|
||||
let guildCommands: Record<GuildId, CommandData[]>
|
||||
try {
|
||||
guildCommands = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8'))
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function publish(commandDir: string | undefined, args: Partial<Publ
|
||||
args.import ??= [];
|
||||
|
||||
args.token && console.info('Token passed through command line');
|
||||
args.applicationId && console.info('applicationId passed through command line');
|
||||
args.applicationId && console.info(magentaBright('WARNING')+ ' This option is deprecated. Do not pass applicationId through command line');
|
||||
commandDir && console.info('Publishing with override path: ', commandDir);
|
||||
|
||||
const dotenvLocation = new URL('../node_modules/dotenv/config.js', rootPath),
|
||||
|
||||
@@ -10,7 +10,8 @@ import { once } from 'node:events';
|
||||
import * as Rest from './rest';
|
||||
import type { sernConfig } from './utilities/getConfig';
|
||||
import type { PublishableData, PublishableModule, Typeable } from './create-publish.d.ts';
|
||||
import { cyanBright, greenBright, redBright } from 'colorette';
|
||||
import { cyanBright, greenBright, magentaBright, redBright } from 'colorette';
|
||||
import { inspect } from 'node:util'
|
||||
import ora from 'ora';
|
||||
|
||||
async function deriveFileInfo(dir: string, file: string) {
|
||||
@@ -163,8 +164,7 @@ const publishableData = modules.map(makePublishData),
|
||||
appid = process.env.applicationId || process.env.APPLICATION_ID;
|
||||
|
||||
assert(token, 'Could not find a token for this bot in .env or commandline. Do you have DISCORD_TOKEN in env?');
|
||||
assert(appid, 'Could not find an application id for this bot in .env or commandline. Do you have APPLICATION_ID in env?');
|
||||
|
||||
appid && console.warn(`${magentaBright('WARNING')}: APPLICATION_ID is not necessary anymore`);
|
||||
// partition globally published and guilded commands
|
||||
const [globalCommands, guildedCommands] = publishableData.reduce(
|
||||
([globals, guilded], module) => {
|
||||
@@ -181,7 +181,7 @@ const spin = ora(`Publishing ${cyanBright('Global')} commands`);
|
||||
|
||||
globalCommands.length && spin.start();
|
||||
|
||||
const rest = Rest.create(appid, token);
|
||||
const rest = await Rest.create(token);
|
||||
const res = await rest.updateGlobal(globalCommands);
|
||||
|
||||
let globalCommandsResponse: unknown;
|
||||
@@ -192,25 +192,29 @@ if (res.ok) {
|
||||
globalCommandsResponse = await res.json();
|
||||
} else {
|
||||
spin.fail(`Failed to publish global commands [Code: ${redBright(res.status)}]`);
|
||||
switch(res.status) {
|
||||
case 400 :
|
||||
throw Error("400: Ensure your commands have proper fields and data with nothing left out");
|
||||
case 404 :
|
||||
throw Error("Forbidden 404. Is you application id and/or token correct?")
|
||||
case 429:
|
||||
throw Error('Chill out homie, too many requests')
|
||||
}
|
||||
console.error('errors:',
|
||||
await res
|
||||
.json()
|
||||
.then((res) => {
|
||||
const errors = Object.values(res?.errors ?? {});
|
||||
// @ts-ignore
|
||||
return errors.map((err) => err?.name?._errors);
|
||||
})
|
||||
.catch(() => "No errors found (Unparsable json for a request with bad status code). Read the status code.")
|
||||
);
|
||||
let err: Error
|
||||
console.error("Status Text ", res.statusText);
|
||||
switch(res.status) {
|
||||
case 400 : {
|
||||
const validation_errors = await res.json()
|
||||
console.error('errors:', inspect(validation_errors, { depth: Infinity }));
|
||||
console.error("Modules with validation errors:"
|
||||
+ inspect(Object.keys(validation_errors.errors).map(idx => globalCommands[idx as any])))
|
||||
throw Error("400: Ensure your commands have proper fields and data with nothing left out");
|
||||
}
|
||||
case 404 : {
|
||||
console.error('errors:', inspect(await res.json(), { depth: Infinity }));
|
||||
throw Error("Forbidden 404. Is you application id and/or token correct?")
|
||||
}
|
||||
case 429: {
|
||||
console.error('errors:', inspect(await res.json(), { depth: Infinity }));
|
||||
err = Error('Chill out homie, too many requests')
|
||||
} break;
|
||||
default: {
|
||||
console.error('errors:', inspect(await res.json(), { depth: Infinity }));
|
||||
throw Error(res.status.toString() + " error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function associateGuildIdsWithData(data: PublishableModule[]): Map<string, PublishableData[]> {
|
||||
@@ -249,12 +253,21 @@ for (const [guildId, array] of guildCommandMap.entries()) {
|
||||
} else {
|
||||
spin.fail(`[${redBright(guildId)}] Failed to update commands for guild, Reason: ${result.message}`);
|
||||
switch(response.status) {
|
||||
case 400 :
|
||||
case 400 : {
|
||||
console.error(inspect(result, { depth: Infinity }))
|
||||
console.error("Modules with validation errors:"
|
||||
+ inspect(Object.keys(result.errors).map(idx => array[idx as any])))
|
||||
|
||||
throw Error("400: Ensure your commands have proper fields and data and nothing left out");
|
||||
case 404 :
|
||||
}
|
||||
case 404 : {
|
||||
console.error(inspect(result, { depth: Infinity }))
|
||||
throw Error("Forbidden 404. Is you application id and/or token correct?")
|
||||
case 429:
|
||||
}
|
||||
case 429: {
|
||||
console.error(inspect(result, { depth: Infinity }))
|
||||
throw Error('Chill out homie, too many requests')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
src/rest.ts
14
src/rest.ts
@@ -11,12 +11,22 @@ const publishablesIntoJson = (ps: PublishableModule[]) =>
|
||||
4
|
||||
);
|
||||
|
||||
export const create = (appid: string, token: string) => {
|
||||
const globalURL = new URL(`${appid}/commands`, baseURL);
|
||||
export const create = async (token: string) => {
|
||||
const headers = {
|
||||
Authorization: 'Bot ' + token,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
let me;
|
||||
let appid: string;
|
||||
try {
|
||||
me = await fetch(new URL('@me', baseURL), { headers }).then(res => res.json());
|
||||
appid = me.id;
|
||||
} catch(e) {
|
||||
console.log("Something went wrong while trying to fetch your application:");
|
||||
throw e;
|
||||
}
|
||||
const globalURL = new URL(`${appid}/commands`, baseURL);
|
||||
|
||||
return {
|
||||
updateGlobal: (commands: PublishableModule[]) =>
|
||||
fetch(globalURL, {
|
||||
|
||||
2
src/types/config.d.ts
vendored
2
src/types/config.d.ts
vendored
@@ -13,7 +13,7 @@ export interface sernConfig {
|
||||
|
||||
export interface TheoreticalEnv {
|
||||
DISCORD_TOKEN: string;
|
||||
APPLICATION_ID: string;
|
||||
APPLICATION_ID?: string;
|
||||
MODE: 'PROD' | 'DEV';
|
||||
[name: string]: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user