mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
chore: improve linting (#7244)
This commit is contained in:
@@ -8,9 +8,5 @@
|
||||
"ignorePatterns": ["**/dist/*"],
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"rules": {
|
||||
"no-redeclare": 0,
|
||||
"@typescript-eslint/naming-convention": 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
# Autogenerated
|
||||
CHANGELOG.md
|
||||
.turbo
|
||||
dist/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
coverage/
|
||||
|
||||
@@ -178,14 +178,14 @@ describe('Slash Commands', () => {
|
||||
expect(() =>
|
||||
getBuilder().addStringOption(
|
||||
// @ts-expect-error Checking if check works JS-side too
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
|
||||
getStringOption().setAutocomplete(true).addChoice('Fancy Pants', 'fp_1'),
|
||||
),
|
||||
).toThrowError();
|
||||
|
||||
expect(() =>
|
||||
getBuilder().addStringOption(
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
|
||||
getStringOption()
|
||||
.setAutocomplete(true)
|
||||
// @ts-expect-error Checking if check works JS-side too
|
||||
@@ -200,7 +200,7 @@ describe('Slash Commands', () => {
|
||||
expect(() =>
|
||||
getBuilder().addStringOption(
|
||||
// @ts-expect-error Checking if check works JS-side too
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
|
||||
getStringOption().addChoice('Fancy Pants', 'fp_1').setAutocomplete(true),
|
||||
),
|
||||
).toThrowError();
|
||||
@@ -384,6 +384,7 @@ describe('Slash Commands', () => {
|
||||
test('GIVEN builder with a subcommand that tries to add an invalid result THEN throw error', () => {
|
||||
expect(() =>
|
||||
// @ts-expect-error Checking if check works JS-side too
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
||||
getNamedBuilder().addSubcommand(getSubcommand()).addInteger(getInteger()),
|
||||
).toThrowError();
|
||||
});
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"test": "jest --pass-with-no-tests",
|
||||
"lint": "eslint src --ext mjs,js,ts",
|
||||
"lint:fix": "eslint src --ext mjs,js,ts --fix",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint src __tests__ --ext mjs,js,ts",
|
||||
"format": "prettier --write . && eslint src __tests__ --ext mjs,js,ts --fix",
|
||||
"docs": "typedoc --json docs/typedoc-out.json src/index.ts && node scripts/docs.mjs",
|
||||
"prepublishOnly": "yarn build && yarn lint && yarn test",
|
||||
"changelog": "git cliff --prepend ./CHANGELOG.md -l -c ../../cliff.toml -r ../../ --include-path './*'"
|
||||
@@ -61,16 +60,16 @@
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.5",
|
||||
"@babel/plugin-proposal-decorators": "^7.16.5",
|
||||
"@babel/preset-env": "^7.16.5",
|
||||
"@babel/preset-env": "^7.16.8",
|
||||
"@babel/preset-typescript": "^7.16.5",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||
"@typescript-eslint/parser": "^5.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
||||
"@typescript-eslint/parser": "^5.9.1",
|
||||
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-config-marine": "^9.1.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
|
||||
@@ -2,6 +2,28 @@ import { z } from 'zod';
|
||||
import { ApplicationCommandType } from 'discord-api-types/v9';
|
||||
import type { ContextMenuCommandType } from './ContextMenuCommandBuilder';
|
||||
|
||||
const namePredicate = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(32)
|
||||
.regex(/^( *[\p{L}\p{N}_-]+ *)+$/u);
|
||||
|
||||
const typePredicate = z.union([z.literal(ApplicationCommandType.User), z.literal(ApplicationCommandType.Message)]);
|
||||
|
||||
const booleanPredicate = z.boolean();
|
||||
|
||||
export function validateDefaultPermission(value: unknown): asserts value is boolean {
|
||||
booleanPredicate.parse(value);
|
||||
}
|
||||
|
||||
export function validateName(name: unknown): asserts name is string {
|
||||
namePredicate.parse(name);
|
||||
}
|
||||
|
||||
export function validateType(type: unknown): asserts type is ContextMenuCommandType {
|
||||
typePredicate.parse(type);
|
||||
}
|
||||
|
||||
export function validateRequiredParameters(name: string, type: number) {
|
||||
// Assert name matches all conditions
|
||||
validateName(name);
|
||||
@@ -9,25 +31,3 @@ export function validateRequiredParameters(name: string, type: number) {
|
||||
// Assert type is valid
|
||||
validateType(type);
|
||||
}
|
||||
|
||||
const namePredicate = z
|
||||
.string()
|
||||
.min(1)
|
||||
.max(32)
|
||||
.regex(/^( *[\p{L}\p{N}_-]+ *)+$/u);
|
||||
|
||||
export function validateName(name: unknown): asserts name is string {
|
||||
namePredicate.parse(name);
|
||||
}
|
||||
|
||||
const typePredicate = z.union([z.literal(ApplicationCommandType.User), z.literal(ApplicationCommandType.Message)]);
|
||||
|
||||
export function validateType(type: unknown): asserts type is ContextMenuCommandType {
|
||||
typePredicate.parse(type);
|
||||
}
|
||||
|
||||
const booleanPredicate = z.boolean();
|
||||
|
||||
export function validateDefaultPermission(value: unknown): asserts value is boolean {
|
||||
booleanPredicate.parse(value);
|
||||
}
|
||||
|
||||
@@ -5,21 +5,6 @@ import type { ApplicationCommandOptionBase } from './mixins/ApplicationCommandOp
|
||||
import type { ToAPIApplicationCommandOptions } from './SlashCommandBuilder';
|
||||
import type { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder } from './SlashCommandSubcommands';
|
||||
|
||||
export function validateRequiredParameters(
|
||||
name: string,
|
||||
description: string,
|
||||
options: ToAPIApplicationCommandOptions[],
|
||||
) {
|
||||
// Assert name matches all conditions
|
||||
validateName(name);
|
||||
|
||||
// Assert description conditions
|
||||
validateDescription(description);
|
||||
|
||||
// Assert options conditions
|
||||
validateMaxOptionsLength(options);
|
||||
}
|
||||
|
||||
const namePredicate = z
|
||||
.string()
|
||||
.min(1)
|
||||
@@ -36,6 +21,27 @@ export function validateDescription(description: unknown): asserts description i
|
||||
descriptionPredicate.parse(description);
|
||||
}
|
||||
|
||||
const maxArrayLengthPredicate = z.unknown().array().max(25);
|
||||
|
||||
export function validateMaxOptionsLength(options: unknown): asserts options is ToAPIApplicationCommandOptions[] {
|
||||
maxArrayLengthPredicate.parse(options);
|
||||
}
|
||||
|
||||
export function validateRequiredParameters(
|
||||
name: string,
|
||||
description: string,
|
||||
options: ToAPIApplicationCommandOptions[],
|
||||
) {
|
||||
// Assert name matches all conditions
|
||||
validateName(name);
|
||||
|
||||
// Assert description conditions
|
||||
validateDescription(description);
|
||||
|
||||
// Assert options conditions
|
||||
validateMaxOptionsLength(options);
|
||||
}
|
||||
|
||||
const booleanPredicate = z.boolean();
|
||||
|
||||
export function validateDefaultPermission(value: unknown): asserts value is boolean {
|
||||
@@ -46,12 +52,6 @@ export function validateRequired(required: unknown): asserts required is boolean
|
||||
booleanPredicate.parse(required);
|
||||
}
|
||||
|
||||
const maxArrayLengthPredicate = z.unknown().array().max(25);
|
||||
|
||||
export function validateMaxOptionsLength(options: unknown): asserts options is ToAPIApplicationCommandOptions[] {
|
||||
maxArrayLengthPredicate.parse(options);
|
||||
}
|
||||
|
||||
export function validateMaxChoicesLength(choices: APIApplicationCommandOptionChoice[]) {
|
||||
maxArrayLengthPredicate.parse(choices);
|
||||
}
|
||||
|
||||
@@ -133,5 +133,5 @@ export interface SlashCommandOptionsOnlyBuilder
|
||||
Pick<SlashCommandBuilder, 'toJSON'> {}
|
||||
|
||||
export interface ToAPIApplicationCommandOptions {
|
||||
toJSON(): APIApplicationCommandOption;
|
||||
toJSON: () => APIApplicationCommandOption;
|
||||
}
|
||||
|
||||
@@ -48,8 +48,10 @@ export class SlashCommandSubcommandGroupBuilder implements ToAPIApplicationComma
|
||||
validateMaxOptionsLength(options);
|
||||
|
||||
// Get the final result
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
const result = typeof input === 'function' ? input(new SlashCommandSubcommandBuilder()) : input;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
assertReturnOfBuilder(result, SlashCommandSubcommandBuilder);
|
||||
|
||||
// Push it
|
||||
|
||||
@@ -8,9 +8,5 @@
|
||||
"ignorePatterns": ["**/dist/*"],
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"rules": {
|
||||
"no-redeclare": 0,
|
||||
"@typescript-eslint/naming-convention": 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
# Autogenerated
|
||||
CHANGELOG.md
|
||||
.turbo
|
||||
dist/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
coverage/
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
"scripts": {
|
||||
"test": "jest --pass-with-no-tests",
|
||||
"build": "tsup",
|
||||
"lint": "eslint src --ext mjs,js,ts",
|
||||
"lint:fix": "eslint src --ext mjs,js,ts --fix",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint src __tests__ --ext mjs,js,ts",
|
||||
"format": "prettier --write . && eslint src __tests__ --ext mjs,js,ts --fix",
|
||||
"docs": "typedoc --json docs/typedoc-out.json src/index.ts && node scripts/docs.mjs",
|
||||
"prepublishOnly": "yarn build && yarn lint && yarn test",
|
||||
"changelog": "git cliff --prepend ./CHANGELOG.md -l -c ../../cliff.toml -r ../../ --include-path './*'"
|
||||
@@ -49,15 +48,15 @@
|
||||
"homepage": "https://discord.js.org",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.5",
|
||||
"@babel/preset-env": "^7.16.5",
|
||||
"@babel/preset-env": "^7.16.8",
|
||||
"@babel/preset-typescript": "^7.16.5",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||
"@typescript-eslint/parser": "^5.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
||||
"@typescript-eslint/parser": "^5.9.1",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-config-marine": "^9.1.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
|
||||
@@ -73,10 +73,12 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
public first(): V | undefined;
|
||||
public first(amount: number): V[];
|
||||
public first(amount?: number): V | V[] | undefined {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
if (typeof amount === 'undefined') return this.values().next().value;
|
||||
if (amount < 0) return this.last(amount * -1);
|
||||
amount = Math.min(this.size, amount);
|
||||
const iter = this.values();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return Array.from({ length: amount }, (): V => iter.next().value);
|
||||
}
|
||||
|
||||
@@ -91,10 +93,12 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
public firstKey(): K | undefined;
|
||||
public firstKey(amount: number): K[];
|
||||
public firstKey(amount?: number): K | K[] | undefined {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
if (typeof amount === 'undefined') return this.keys().next().value;
|
||||
if (amount < 0) return this.lastKey(amount * -1);
|
||||
amount = Math.min(this.size, amount);
|
||||
const iter = this.keys();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return Array.from({ length: amount }, (): K => iter.next().value);
|
||||
}
|
||||
|
||||
@@ -398,6 +402,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const iter = this.entries();
|
||||
return Array.from({ length: this.size }, (): T => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const [key, value] = iter.next().value;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
return fn(value, key, this);
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
# Autogenerated
|
||||
CHANGELOG.md
|
||||
.turbo
|
||||
dist/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
coverage/
|
||||
|
||||
@@ -3,12 +3,10 @@
|
||||
"version": "14.0.0-dev",
|
||||
"description": "A powerful library for interacting with the Discord API",
|
||||
"scripts": {
|
||||
"test": "yarn docs:test && yarn lint:typings && yarn test:typescript",
|
||||
"test": "yarn docs:test && yarn test:typescript",
|
||||
"test:typescript": "tsc --noEmit && tsd",
|
||||
"lint": "eslint ./src",
|
||||
"lint:fix": "eslint ./src --fix",
|
||||
"lint:typings": "tslint ./typings/index.d.ts",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint src && tslint typings/index.d.ts",
|
||||
"format": "prettier --write . && eslint src --fix",
|
||||
"docs": "docgen --source ./src --custom ./docs/index.yml --output ./docs/docs.json",
|
||||
"docs:test": "docgen --source ./src --custom ./docs/index.yml",
|
||||
"prepublishOnly": "yarn lint && yarn test",
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const fetch = require('node-fetch');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const process = require('node:process');
|
||||
const { setTimeout: sleep } = require('node:timers/promises');
|
||||
const util = require('node:util');
|
||||
const fetch = require('node-fetch');
|
||||
const { owner, token } = require('./auth.js');
|
||||
const { Client, Intents, MessageAttachment, MessageEmbed } = require('../src');
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const fetch = require('node-fetch');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { setTimeout: sleep } = require('node:timers/promises');
|
||||
const util = require('node:util');
|
||||
const fetch = require('node-fetch');
|
||||
const { owner, token, webhookChannel, webhookToken } = require('./auth.js');
|
||||
const { Client, Intents, MessageAttachment, MessageEmbed, WebhookClient } = require('../src');
|
||||
|
||||
|
||||
@@ -7,9 +7,5 @@
|
||||
"ignorePatterns": ["**/dist/*"],
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"rules": {
|
||||
"no-redeclare": 0,
|
||||
"@typescript-eslint/naming-convention": 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
# Autogenerated
|
||||
CHANGELOG.md
|
||||
.turbo
|
||||
dist/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
coverage/
|
||||
|
||||
@@ -41,6 +41,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
.post('/postFile')
|
||||
.times(5)
|
||||
.reply(200, (_, body) => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
body: body
|
||||
.replace(/\r\n/g, '\n')
|
||||
.replace(/-+\d+-*\n?/g, '')
|
||||
|
||||
@@ -21,12 +21,31 @@ const sublimitIntervals = {
|
||||
const sublimit = { body: { name: 'newname' } };
|
||||
const noSublimit = { body: { bitrate: 40000 } };
|
||||
|
||||
function startSublimitIntervals() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!sublimitIntervals.reset) {
|
||||
sublimitResetAfter = Date.now() + 250;
|
||||
sublimitIntervals.reset = setInterval(() => {
|
||||
sublimitRequests = 0;
|
||||
sublimitResetAfter = Date.now() + 250;
|
||||
}, 250);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!sublimitIntervals.retry) {
|
||||
retryAfter = Date.now() + 1000;
|
||||
sublimitIntervals.retry = setInterval(() => {
|
||||
sublimitHits = 0;
|
||||
retryAfter = Date.now() + 1000;
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
.persist()
|
||||
.replyDate()
|
||||
.get('/standard')
|
||||
.times(3)
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
const response = Date.now() >= resetAfter ? 204 : 429;
|
||||
resetAfter = Date.now() + 250;
|
||||
if (response === 204) {
|
||||
@@ -62,8 +81,8 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
];
|
||||
})
|
||||
.get('/triggerGlobal')
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
return [
|
||||
.reply(
|
||||
(): nock.ReplyFnResult => [
|
||||
204,
|
||||
{ global: true },
|
||||
{
|
||||
@@ -71,12 +90,12 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
'retry-after': '1',
|
||||
via: '1.1 google',
|
||||
},
|
||||
];
|
||||
})
|
||||
],
|
||||
)
|
||||
.get('/regularRequest')
|
||||
.reply(204, { test: true })
|
||||
.patch('/channels/:id', (body) => ['name', 'topic'].some((key) => Reflect.has(body as Record<string, unknown>, key)))
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
sublimitHits += 1;
|
||||
sublimitRequests += 1;
|
||||
const response = 2 - sublimitHits >= 0 && 10 - sublimitRequests >= 0 ? 204 : 429;
|
||||
@@ -113,7 +132,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
.patch('/channels/:id', (body) =>
|
||||
['name', 'topic'].every((key) => !Reflect.has(body as Record<string, unknown>, key)),
|
||||
)
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
sublimitRequests += 1;
|
||||
const response = 10 - sublimitRequests >= 0 ? 204 : 429;
|
||||
startSublimitIntervals();
|
||||
@@ -148,7 +167,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
})
|
||||
.get('/unexpected')
|
||||
.times(2)
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
if (unexpected429) {
|
||||
unexpected429 = false;
|
||||
return [
|
||||
@@ -164,7 +183,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
})
|
||||
.get('/unexpected-cf')
|
||||
.times(2)
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
if (unexpected429cf) {
|
||||
unexpected429cf = false;
|
||||
return [
|
||||
@@ -179,7 +198,7 @@ nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`)
|
||||
})
|
||||
.get('/temp')
|
||||
.times(2)
|
||||
.reply(function handler(): nock.ReplyFnResult {
|
||||
.reply((): nock.ReplyFnResult => {
|
||||
if (serverOutage) {
|
||||
serverOutage = false;
|
||||
return [500];
|
||||
@@ -345,22 +364,3 @@ test('Reject on RateLimit', async () => {
|
||||
test('malformedRequest', async () => {
|
||||
expect(await api.get('/malformedRequest')).toBe(null);
|
||||
});
|
||||
|
||||
function startSublimitIntervals() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!sublimitIntervals.reset) {
|
||||
sublimitResetAfter = Date.now() + 250;
|
||||
sublimitIntervals.reset = setInterval(() => {
|
||||
sublimitRequests = 0;
|
||||
sublimitResetAfter = Date.now() + 250;
|
||||
}, 250);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!sublimitIntervals.retry) {
|
||||
retryAfter = Date.now() + 1000;
|
||||
sublimitIntervals.retry = setInterval(() => {
|
||||
sublimitHits = 0;
|
||||
retryAfter = Date.now() + 1000;
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
"scripts": {
|
||||
"build": "tsup && tsc --emitDeclarationOnly --incremental",
|
||||
"test": "jest --pass-with-no-tests --collect-coverage",
|
||||
"lint": "eslint src __tests__ --ext mjs,js,ts",
|
||||
"lint:fix": "eslint src __tests__ --ext mjs,js,ts --fix",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint src __tests__ --ext mjs,js,ts",
|
||||
"format": "prettier --write . && eslint src __tests__ --ext mjs,js,ts --fix",
|
||||
"prepublishOnly": "yarn build && yarn lint && yarn test",
|
||||
"changelog": "git cliff --prepend ./CHANGELOG.md -l -c ../../cliff.toml -r ../../ --include-path './*'"
|
||||
},
|
||||
@@ -60,16 +59,16 @@
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.7",
|
||||
"@babel/plugin-proposal-decorators": "^7.16.7",
|
||||
"@babel/preset-env": "^7.16.7",
|
||||
"@babel/preset-env": "^7.16.8",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node-fetch": "^2.5.10",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||
"@typescript-eslint/parser": "^5.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
||||
"@typescript-eslint/parser": "^5.9.1",
|
||||
"babel-plugin-const-enum": "^1.2.0",
|
||||
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-config-marine": "^9.1.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
|
||||
@@ -171,20 +171,20 @@ export interface RestEvents {
|
||||
}
|
||||
|
||||
export interface REST {
|
||||
on<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
on<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
once<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
once<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
emit<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]): boolean;
|
||||
emit<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]): boolean;
|
||||
emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
|
||||
|
||||
off<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
off<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
removeAllListeners<K extends keyof RestEvents>(event?: K): this;
|
||||
removeAllListeners<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>): this;
|
||||
removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) &
|
||||
(<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
|
||||
}
|
||||
|
||||
export class REST extends EventEmitter {
|
||||
|
||||
@@ -126,20 +126,20 @@ export interface RouteData {
|
||||
}
|
||||
|
||||
export interface RequestManager {
|
||||
on<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
on<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
once<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
once<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
emit<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]): boolean;
|
||||
emit<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]): boolean;
|
||||
emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
|
||||
|
||||
off<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void): this;
|
||||
off<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void): this;
|
||||
off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) &
|
||||
(<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
|
||||
|
||||
removeAllListeners<K extends keyof RestEvents>(event?: K): this;
|
||||
removeAllListeners<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>): this;
|
||||
removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) &
|
||||
(<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,10 +2,10 @@ import type { RequestInit } from 'node-fetch';
|
||||
import type { InternalRequest, RouteData } from '../RequestManager';
|
||||
|
||||
export interface IHandler {
|
||||
queueRequest(
|
||||
queueRequest: (
|
||||
routeId: RouteData,
|
||||
url: string,
|
||||
options: RequestInit,
|
||||
bodyData: Pick<InternalRequest, 'files' | 'body'>,
|
||||
): Promise<unknown>;
|
||||
) => Promise<unknown>;
|
||||
}
|
||||
|
||||
@@ -304,11 +304,12 @@ export class SequentialHandler {
|
||||
try {
|
||||
// node-fetch typings are a bit weird, so we have to cast to any to get the correct signature
|
||||
// Type 'AbortSignal' is not assignable to type 'import("discord.js-modules/node_modules/@types/node-fetch/externals").AbortSignal'
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
res = await fetch(url, { ...options, signal: controller.signal as any });
|
||||
} catch (error: unknown) {
|
||||
// Retry the specified number of times for possible timed out requests
|
||||
if (error instanceof Error && error.name === 'AbortError' && retries !== this.manager.options.retries) {
|
||||
return this.runRequest(routeId, url, options, bodyData, ++retries);
|
||||
return await this.runRequest(routeId, url, options, bodyData, ++retries);
|
||||
}
|
||||
|
||||
throw error;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { APIVersion } from 'discord-api-types/v9';
|
||||
import type { RESTOptions } from '../REST';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-assignment
|
||||
const Package = require('../../../package.json');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
||||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-member-access
|
||||
export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.version})`;
|
||||
|
||||
export const DefaultRestOptions: Required<RESTOptions> = {
|
||||
|
||||
@@ -8,9 +8,5 @@
|
||||
"ignorePatterns": ["**/dist/*"],
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"rules": {
|
||||
"no-redeclare": 0,
|
||||
"@typescript-eslint/naming-convention": 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,8 @@
|
||||
# Autogenerated
|
||||
CHANGELOG.md
|
||||
.turbo
|
||||
dist/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
coverage/
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
/* eslint-disable @typescript-eslint/dot-notation */
|
||||
import { GatewayOpcodes } from 'discord-api-types/v9';
|
||||
import * as DataStore from '../src/DataStore';
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||
/* eslint-disable @typescript-eslint/dot-notation */
|
||||
import {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
import { joinVoiceChannel } from '../src/joinVoiceChannel';
|
||||
import * as VoiceConnection from '../src/VoiceConnection';
|
||||
|
||||
|
||||
@@ -5,9 +5,8 @@
|
||||
"scripts": {
|
||||
"build": "tsup && node scripts/postbuild.mjs",
|
||||
"test": "jest --pass-with-no-tests --collect-coverage",
|
||||
"lint": "eslint src --ext mjs,js,ts",
|
||||
"lint:fix": "eslint src --ext mjs,js,ts --fix",
|
||||
"format": "prettier --write .",
|
||||
"lint": "prettier --check . && eslint src __tests__ --ext mjs,js,ts",
|
||||
"format": "prettier --write . && eslint src __tests__ --ext mjs,js,ts --fix",
|
||||
"docs": "typedoc --json docs/typedoc-out.json src/index.ts && node scripts/docs.mjs",
|
||||
"prepublishOnly": "yarn build && yarn lint && yarn test",
|
||||
"changelog": "git cliff --prepend ./CHANGELOG.md -l -c ../../cliff.toml -r ../../ --include-path './*'"
|
||||
@@ -59,15 +58,15 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.0",
|
||||
"@babel/preset-env": "^7.16.0",
|
||||
"@babel/preset-env": "^7.16.8",
|
||||
"@babel/preset-typescript": "^7.16.0",
|
||||
"@discordjs/ts-docgen": "^0.3.2",
|
||||
"@types/jest": "^27.0.2",
|
||||
"@types/node": "^16.11.7",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
||||
"@typescript-eslint/parser": "^5.9.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.9.1",
|
||||
"@typescript-eslint/parser": "^5.9.1",
|
||||
"eslint": "^8.2.0",
|
||||
"eslint-config-marine": "^9.0.6",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
|
||||
@@ -125,6 +125,7 @@ function audioCycleStep() {
|
||||
// eslint-disable-next-line @typescript-eslint/dot-notation
|
||||
available.forEach((player) => player['_stepDispatch']());
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
prepareNextAudioFrame(available);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
|
||||
import type { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v9';
|
||||
import type { CreateVoiceConnectionOptions } from '.';
|
||||
import type { AudioPlayer } from './audio/AudioPlayer';
|
||||
@@ -261,11 +262,11 @@ export class VoiceConnection extends TypedEmitter<VoiceConnectionEvents> {
|
||||
*/
|
||||
public set state(newState: VoiceConnectionState) {
|
||||
const oldState = this._state;
|
||||
const oldNetworking: Networking | undefined = Reflect.get(oldState, 'networking');
|
||||
const newNetworking: Networking | undefined = Reflect.get(newState, 'networking');
|
||||
const oldNetworking = Reflect.get(oldState, 'networking') as Networking | undefined;
|
||||
const newNetworking = Reflect.get(newState, 'networking') as Networking | undefined;
|
||||
|
||||
const oldSubscription: PlayerSubscription | undefined = Reflect.get(oldState, 'subscription');
|
||||
const newSubscription: PlayerSubscription | undefined = Reflect.get(newState, 'subscription');
|
||||
const oldSubscription = Reflect.get(oldState, 'subscription') as PlayerSubscription | undefined;
|
||||
const newSubscription = Reflect.get(newState, 'subscription') as PlayerSubscription | undefined;
|
||||
|
||||
if (oldNetworking !== newNetworking) {
|
||||
if (oldNetworking) {
|
||||
@@ -365,6 +366,7 @@ export class VoiceConnection extends TypedEmitter<VoiceConnectionEvents> {
|
||||
newUdp?.on('message', this.receiver.onUdpMessage);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
this.receiver.connectionData = Reflect.get(newState, 'connectionData') ?? {};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
|
||||
import { addAudioPlayer, deleteAudioPlayer } from '../DataStore';
|
||||
import { Awaited, noop } from '../util/util';
|
||||
import { VoiceConnection, VoiceConnectionStatus } from '../VoiceConnection';
|
||||
@@ -163,6 +164,19 @@ export type AudioPlayerEvents = {
|
||||
) => Awaited<void>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stringifies an AudioPlayerState instance.
|
||||
*
|
||||
* @param state - The state to stringify
|
||||
*/
|
||||
function stringifyState(state: AudioPlayerState) {
|
||||
return JSON.stringify({
|
||||
...state,
|
||||
resource: Reflect.has(state, 'resource'),
|
||||
stepTimeout: Reflect.has(state, 'stepTimeout'),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to play audio resources (i.e. tracks, streams) to voice connections.
|
||||
*
|
||||
@@ -606,19 +620,6 @@ export class AudioPlayer extends TypedEmitter<AudioPlayerEvents> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stringifies an AudioPlayerState instance.
|
||||
*
|
||||
* @param state - The state to stringify
|
||||
*/
|
||||
function stringifyState(state: AudioPlayerState) {
|
||||
return JSON.stringify({
|
||||
...state,
|
||||
resource: Reflect.has(state, 'resource'),
|
||||
stepTimeout: Reflect.has(state, 'stepTimeout'),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AudioPlayer to be used.
|
||||
*/
|
||||
|
||||
@@ -151,7 +151,7 @@ export class AudioResource<T = unknown> {
|
||||
this.silenceRemaining--;
|
||||
return SILENCE_FRAME;
|
||||
}
|
||||
const packet: Buffer | null = this.playStream.read();
|
||||
const packet = this.playStream.read() as Buffer | null;
|
||||
if (packet) {
|
||||
this.playbackDuration += 20;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
/* eslint-disable @typescript-eslint/dot-notation */
|
||||
import { AudioResource } from '../../audio/AudioResource';
|
||||
import { createAudioPlayer, AudioPlayerStatus, AudioPlayer, SILENCE_FRAME } from '../AudioPlayer';
|
||||
@@ -372,7 +374,7 @@ test('play() throws when playing a resource that has already ended', async () =>
|
||||
expect(resource.playStream.readableEnded).toBe(true);
|
||||
player.stop(true);
|
||||
expect(player.state.status).toBe(AudioPlayerStatus.Idle);
|
||||
expect(() => player.play(resource)).toThrow();
|
||||
expect(() => player?.play(resource)).toThrow();
|
||||
});
|
||||
|
||||
test('Propagates errors from streams', async () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
import { opus, VolumeTransformer } from 'prism-media';
|
||||
import { PassThrough, Readable } from 'node:stream';
|
||||
import { SILENCE_FRAME } from '../AudioPlayer';
|
||||
|
||||
@@ -157,6 +157,41 @@ export interface NetworkingEvents {
|
||||
close: (code: number) => Awaited<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stringifies a NetworkingState.
|
||||
*
|
||||
* @param state - The state to stringify
|
||||
*/
|
||||
function stringifyState(state: NetworkingState) {
|
||||
return JSON.stringify({
|
||||
...state,
|
||||
ws: Reflect.has(state, 'ws'),
|
||||
udp: Reflect.has(state, 'udp'),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Chooses an encryption mode from a list of given options. Chooses the most preferred option.
|
||||
*
|
||||
* @param options - The available encryption options
|
||||
*/
|
||||
function chooseEncryptionMode(options: string[]): string {
|
||||
const option = options.find((option) => SUPPORTED_ENCRYPTION_MODES.includes(option));
|
||||
if (!option) {
|
||||
throw new Error(`No compatible encryption modes. Available include: ${options.join(', ')}`);
|
||||
}
|
||||
return option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random number that is in the range of n bits.
|
||||
*
|
||||
* @param n - The number of bits
|
||||
*/
|
||||
function randomNBit(n: number) {
|
||||
return Math.floor(Math.random() * 2 ** n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages the networking required to maintain a voice connection and dispatch audio packets
|
||||
*/
|
||||
@@ -348,12 +383,16 @@ export class Networking extends TypedEmitter<NetworkingEvents> {
|
||||
* @param packet - The received packet
|
||||
*/
|
||||
private onWsPacket(packet: any) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (packet.op === VoiceOpcodes.Hello && this.state.code !== NetworkingStatusCode.Closed) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
||||
this.state.ws.setHeartbeatInterval(packet.d.heartbeat_interval);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
} else if (packet.op === VoiceOpcodes.Ready && this.state.code === NetworkingStatusCode.Identifying) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
const { ip, port, ssrc, modes } = packet.d;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const udp = new VoiceUDPSocket({ ip, port });
|
||||
udp.on('error', this.onChildError);
|
||||
udp.on('debug', this.onUdpDebug);
|
||||
@@ -387,19 +426,23 @@ export class Networking extends TypedEmitter<NetworkingEvents> {
|
||||
code: NetworkingStatusCode.UdpHandshaking,
|
||||
udp,
|
||||
connectionData: {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
ssrc,
|
||||
},
|
||||
};
|
||||
} else if (
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
packet.op === VoiceOpcodes.SessionDescription &&
|
||||
this.state.code === NetworkingStatusCode.SelectingProtocol
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
const { mode: encryptionMode, secret_key: secretKey } = packet.d;
|
||||
this.state = {
|
||||
...this.state,
|
||||
code: NetworkingStatusCode.Ready,
|
||||
connectionData: {
|
||||
...this.state.connectionData,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
encryptionMode,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
secretKey: new Uint8Array(secretKey),
|
||||
@@ -411,6 +454,7 @@ export class Networking extends TypedEmitter<NetworkingEvents> {
|
||||
packetsPlayed: 0,
|
||||
},
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
} else if (packet.op === VoiceOpcodes.Resumed && this.state.code === NetworkingStatusCode.Resuming) {
|
||||
this.state = {
|
||||
...this.state,
|
||||
@@ -557,38 +601,3 @@ export class Networking extends TypedEmitter<NetworkingEvents> {
|
||||
return [secretbox.methods.close(opusPacket, nonce, secretKey)];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random number that is in the range of n bits.
|
||||
*
|
||||
* @param n - The number of bits
|
||||
*/
|
||||
function randomNBit(n: number) {
|
||||
return Math.floor(Math.random() * 2 ** n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stringifies a NetworkingState.
|
||||
*
|
||||
* @param state - The state to stringify
|
||||
*/
|
||||
function stringifyState(state: NetworkingState) {
|
||||
return JSON.stringify({
|
||||
...state,
|
||||
ws: Reflect.has(state, 'ws'),
|
||||
udp: Reflect.has(state, 'udp'),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Chooses an encryption mode from a list of given options. Chooses the most preferred option.
|
||||
*
|
||||
* @param options - The available encryption options
|
||||
*/
|
||||
function chooseEncryptionMode(options: string[]): string {
|
||||
const option = options.find((option) => SUPPORTED_ENCRYPTION_MODES.includes(option));
|
||||
if (!option) {
|
||||
throw new Error(`No compatible encryption modes. Available include: ${options.join(', ')}`);
|
||||
}
|
||||
return option;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,25 @@ export interface VoiceUDPSocketEvents {
|
||||
message: (message: Buffer) => Awaited<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the response from Discord to aid with local IP discovery.
|
||||
*
|
||||
* @param message - The received message
|
||||
*/
|
||||
export function parseLocalPacket(message: Buffer): SocketConfig {
|
||||
const packet = Buffer.from(message);
|
||||
|
||||
const ip = packet.slice(8, packet.indexOf(0, 8)).toString('utf-8');
|
||||
|
||||
if (!isIPv4(ip)) {
|
||||
throw new Error('Malformed IP address');
|
||||
}
|
||||
|
||||
const port = packet.readUInt16BE(packet.length - 2);
|
||||
|
||||
return { ip, port };
|
||||
}
|
||||
|
||||
/**
|
||||
* The interval in milliseconds at which keep alive datagrams are sent.
|
||||
*/
|
||||
@@ -191,22 +210,3 @@ export class VoiceUDPSocket extends TypedEmitter<VoiceUDPSocketEvents> {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the response from Discord to aid with local IP discovery.
|
||||
*
|
||||
* @param message - The received message
|
||||
*/
|
||||
export function parseLocalPacket(message: Buffer): SocketConfig {
|
||||
const packet = Buffer.from(message);
|
||||
|
||||
const ip = packet.slice(8, packet.indexOf(0, 8)).toString('utf-8');
|
||||
|
||||
if (!isIPv4(ip)) {
|
||||
throw new Error('Malformed IP address');
|
||||
}
|
||||
|
||||
const port = packet.readUInt16BE(packet.length - 2);
|
||||
|
||||
return { ip, port };
|
||||
}
|
||||
|
||||
@@ -107,6 +107,7 @@ export class VoiceWebSocket extends TypedEmitter<VoiceWebSocketEvents> {
|
||||
|
||||
let packet: any;
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
packet = JSON.parse(event.data);
|
||||
} catch (error) {
|
||||
const e = error as Error;
|
||||
@@ -114,6 +115,7 @@ export class VoiceWebSocket extends TypedEmitter<VoiceWebSocketEvents> {
|
||||
return;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (packet.op === VoiceOpcodes.HeartbeatAck) {
|
||||
this.lastHeartbeatAck = Date.now();
|
||||
this.missedHeartbeats = 0;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
/* eslint-disable @typescript-eslint/no-empty-function */
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { createSocket as _createSocket } from 'node:dgram';
|
||||
@@ -21,7 +22,6 @@ class FakeSocket extends EventEmitter {
|
||||
}
|
||||
|
||||
// ip = 91.90.123.93, port = 54148
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
const VALID_RESPONSE = Buffer.from([
|
||||
0x0, 0x2, 0x0, 0x46, 0x0, 0x4, 0xeb, 0x23, 0x39, 0x31, 0x2e, 0x39, 0x30, 0x2e, 0x31, 0x32, 0x33, 0x2e, 0x39, 0x33,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import { VoiceOpcodes } from 'discord-api-types/voice/v4';
|
||||
import EventEmitter, { once } from 'node:events';
|
||||
import WS from 'jest-websocket-mock';
|
||||
|
||||
@@ -63,23 +63,34 @@ export class VoiceReceiver {
|
||||
* @internal
|
||||
*/
|
||||
public onWsPacket(packet: any) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (packet.op === VoiceOpcodes.ClientDisconnect && typeof packet.d?.user_id === 'string') {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
|
||||
this.ssrcMap.delete(packet.d.user_id);
|
||||
} else if (
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
packet.op === VoiceOpcodes.Speaking &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
typeof packet.d?.user_id === 'string' &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
typeof packet.d?.ssrc === 'number'
|
||||
) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
this.ssrcMap.update({ userId: packet.d.user_id, audioSSRC: packet.d.ssrc });
|
||||
} else if (
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
packet.op === VoiceOpcodes.ClientConnect &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
typeof packet.d?.user_id === 'string' &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
typeof packet.d?.audio_ssrc === 'number'
|
||||
) {
|
||||
this.ssrcMap.update({
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
userId: packet.d.user_id,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
audioSSRC: packet.d.audio_ssrc,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
videoSSRC: packet.d.video_ssrc === 0 ? undefined : packet.d.video_ssrc,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import EventEmitter, { once } from 'node:events';
|
||||
import { SSRCMap, VoiceUserData } from '../SSRCMap';
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
/* eslint-disable @typescript-eslint/dot-notation */
|
||||
import { VoiceReceiver } from '../VoiceReceiver';
|
||||
import { VoiceConnection as _VoiceConnection, VoiceConnectionStatus } from '../../VoiceConnection';
|
||||
|
||||
@@ -1,28 +1,37 @@
|
||||
interface Methods {
|
||||
open(buffer: Buffer, nonce: Buffer, secretKey: Uint8Array): Buffer | null;
|
||||
close(opusPacket: Buffer, nonce: Buffer, secretKey: Uint8Array): Buffer;
|
||||
random(bytes: number, nonce: Buffer): Buffer;
|
||||
open: (buffer: Buffer, nonce: Buffer, secretKey: Uint8Array) => Buffer | null;
|
||||
close: (opusPacket: Buffer, nonce: Buffer, secretKey: Uint8Array) => Buffer;
|
||||
random: (bytes: number, nonce: Buffer) => Buffer;
|
||||
}
|
||||
|
||||
const libs = {
|
||||
sodium: (sodium: any): Methods => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
open: sodium.api.crypto_secretbox_open_easy,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
close: sodium.api.crypto_secretbox_easy,
|
||||
random: (n: any, buffer?: Buffer) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
if (!buffer) buffer = Buffer.allocUnsafe(n);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
sodium.api.randombytes_buf(buffer);
|
||||
return buffer;
|
||||
},
|
||||
}),
|
||||
'libsodium-wrappers': (sodium: any): Methods => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
open: sodium.crypto_secretbox_open_easy,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
close: sodium.crypto_secretbox_easy,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
random: (n: any) => sodium.randombytes_buf(n),
|
||||
}),
|
||||
tweetnacl: (tweetnacl: any): Methods => ({
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
open: tweetnacl.secretbox.open,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
close: tweetnacl.secretbox,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
random: (n: any) => tweetnacl.randomBytes(n),
|
||||
}),
|
||||
} as const;
|
||||
@@ -46,6 +55,7 @@ void (async () => {
|
||||
try {
|
||||
// eslint-disable-next-line
|
||||
const lib = require(libName);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (libName === 'libsodium-wrappers' && lib.ready) await lib.ready;
|
||||
Object.assign(methods, libs[libName](lib));
|
||||
break;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import { demuxProbe } from '../demuxProbe';
|
||||
import { opus as _opus } from 'prism-media';
|
||||
import { Readable } from 'node:stream';
|
||||
@@ -9,6 +12,10 @@ jest.mock('prism-media');
|
||||
const WebmDemuxer = _opus.WebmDemuxer as unknown as jest.Mock<_opus.WebmDemuxer>;
|
||||
const OggDemuxer = _opus.OggDemuxer as unknown as jest.Mock<_opus.OggDemuxer>;
|
||||
|
||||
function nextTick() {
|
||||
return new Promise((resolve) => process.nextTick(resolve));
|
||||
}
|
||||
|
||||
async function* gen(n: number) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
yield Buffer.from([i]);
|
||||
@@ -37,10 +44,6 @@ async function collectStream(stream: Readable): Promise<Buffer> {
|
||||
return output;
|
||||
}
|
||||
|
||||
function nextTick() {
|
||||
return new Promise((resolve) => process.nextTick(resolve));
|
||||
}
|
||||
|
||||
describe('demuxProbe', () => {
|
||||
const webmWrite: jest.Mock<(buffer: Buffer) => void> = jest.fn();
|
||||
const oggWrite: jest.Mock<(buffer: Buffer) => void> = jest.fn();
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import EventEmitter from 'node:events';
|
||||
import { VoiceConnection, VoiceConnectionStatus } from '../../VoiceConnection';
|
||||
import { entersState } from '../entersState';
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
export function abortAfter(delay: number): [AbortController, AbortSignal] {
|
||||
const ac = new AbortController();
|
||||
const timeout = setTimeout(() => ac.abort(), delay);
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
||||
ac.signal.addEventListener('abort', () => clearTimeout(timeout));
|
||||
return [ac, ac.signal];
|
||||
}
|
||||
|
||||
@@ -10,17 +10,17 @@ export interface DiscordGatewayAdapterLibraryMethods {
|
||||
*
|
||||
* @param data - The inner data of the VOICE_SERVER_UPDATE payload
|
||||
*/
|
||||
onVoiceServerUpdate(data: GatewayVoiceServerUpdateDispatchData): void;
|
||||
onVoiceServerUpdate: (data: GatewayVoiceServerUpdateDispatchData) => void;
|
||||
/**
|
||||
* Call this when you receive a VOICE_STATE_UPDATE payload that is relevant to the adapter.
|
||||
*
|
||||
* @param data - The inner data of the VOICE_STATE_UPDATE payload
|
||||
*/
|
||||
onVoiceStateUpdate(data: GatewayVoiceStateUpdateDispatchData): void;
|
||||
onVoiceStateUpdate: (data: GatewayVoiceStateUpdateDispatchData) => void;
|
||||
/**
|
||||
* Call this when the adapter can no longer be used (e.g. due to a disconnect from the main gateway)
|
||||
*/
|
||||
destroy(): void;
|
||||
destroy: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,12 +34,12 @@ export interface DiscordGatewayAdapterImplementerMethods {
|
||||
*
|
||||
* @returns `false` if the payload definitely failed to send - in this case, the voice connection disconnects
|
||||
*/
|
||||
sendPayload(payload: any): boolean;
|
||||
sendPayload: (payload: any) => boolean;
|
||||
/**
|
||||
* This will be called by @discordjs/voice when the adapter can safely be destroyed as it will no
|
||||
* longer be used.
|
||||
*/
|
||||
destroy(): void;
|
||||
destroy: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,8 +56,11 @@ export function demuxProbe(
|
||||
let resolved: StreamType | undefined = undefined;
|
||||
|
||||
const finish = (type: StreamType) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
stream.off('data', onData);
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
stream.off('close', onClose);
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
stream.off('end', onClose);
|
||||
stream.pause();
|
||||
resolved = type;
|
||||
|
||||
@@ -3,6 +3,51 @@
|
||||
import { resolve, dirname } from 'node:path';
|
||||
import prism from 'prism-media';
|
||||
|
||||
/**
|
||||
* Tries to find the package.json file for a given module.
|
||||
*
|
||||
* @param dir - The directory to look in
|
||||
* @param packageName - The name of the package to look for
|
||||
* @param depth - The maximum recursion depth
|
||||
*/
|
||||
function findPackageJSON(
|
||||
dir: string,
|
||||
packageName: string,
|
||||
depth: number,
|
||||
): { name: string; version: string } | undefined {
|
||||
if (depth === 0) return undefined;
|
||||
const attemptedPath = resolve(dir, './package.json');
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const pkg = require(attemptedPath);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (pkg.name !== packageName) throw new Error('package.json does not match');
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return pkg;
|
||||
} catch (err) {
|
||||
return findPackageJSON(resolve(dir, '..'), packageName, depth - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to find the version of a dependency.
|
||||
*
|
||||
* @param name - The package to find the version of
|
||||
*/
|
||||
function version(name: string): string {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const pkg =
|
||||
name === '@discordjs/voice'
|
||||
? require('../../package.json')
|
||||
: findPackageJSON(dirname(require.resolve(name)), name, 3);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
|
||||
return pkg?.version ?? 'not found';
|
||||
} catch (err) {
|
||||
return 'not found';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a report of the dependencies used by the \@discordjs/voice module.
|
||||
* Useful for debugging.
|
||||
@@ -41,43 +86,3 @@ export function generateDependencyReport() {
|
||||
|
||||
return ['-'.repeat(50), ...report, '-'.repeat(50)].join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to find the package.json file for a given module.
|
||||
*
|
||||
* @param dir - The directory to look in
|
||||
* @param packageName - The name of the package to look for
|
||||
* @param depth - The maximum recursion depth
|
||||
*/
|
||||
function findPackageJSON(
|
||||
dir: string,
|
||||
packageName: string,
|
||||
depth: number,
|
||||
): { name: string; version: string } | undefined {
|
||||
if (depth === 0) return undefined;
|
||||
const attemptedPath = resolve(dir, './package.json');
|
||||
try {
|
||||
const pkg = require(attemptedPath);
|
||||
if (pkg.name !== packageName) throw new Error('package.json does not match');
|
||||
return pkg;
|
||||
} catch (err) {
|
||||
return findPackageJSON(resolve(dir, '..'), packageName, depth - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to find the version of a dependency.
|
||||
*
|
||||
* @param name - The package to find the version of
|
||||
*/
|
||||
function version(name: string): string {
|
||||
try {
|
||||
const pkg =
|
||||
name === '@discordjs/voice'
|
||||
? require('../../package.json')
|
||||
: findPackageJSON(dirname(require.resolve(name)), name, 3);
|
||||
return pkg?.version ?? 'not found';
|
||||
} catch (err) {
|
||||
return 'not found';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,4 @@
|
||||
{
|
||||
// Mapped from https://www.typescriptlang.org/tsconfig
|
||||
"compilerOptions": {
|
||||
// Type Checking
|
||||
"allowUnreachableCode": false,
|
||||
"allowUnusedLabels": false,
|
||||
// if true: conflicts with discord-api-types
|
||||
"exactOptionalPropertyTypes": false,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitOverride": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"strict": true,
|
||||
"useUnknownInCatchVariables": true,
|
||||
|
||||
// Modules
|
||||
"module": "CommonJS",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
|
||||
// Emit
|
||||
"declaration": true,
|
||||
"importHelpers": true,
|
||||
"importsNotUsedAsValues": "error",
|
||||
"inlineSources": true,
|
||||
"newLine": "lf",
|
||||
"noEmitHelpers": true,
|
||||
"outDir": "dist",
|
||||
"preserveConstEnums": true,
|
||||
"removeComments": false,
|
||||
"sourceMap": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
|
||||
// Language and Environment
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"lib": ["ESNext"],
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
|
||||
// Completeness
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["src/**/__tests__"]
|
||||
"extends": "../../tsconfig.json",
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user