Skip to content

Commit a04fc04

Browse files
authored
add support for discord.js v13 (#85)
* rename files - blapi.ts -> main.ts - bttps.ts -> requests.ts - bump minor version - adjust type to support both discordjs v12 and v13 clients passed (for now that only encompasses the shardID to shardId change) * fix type errors with logger passing
1 parent 7393161 commit a04fc04

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
"typescript",
5555
"typed"
5656
],
57-
"main": "dist/blapi.js",
58-
"types": "dist/blapi.d.ts",
57+
"main": "dist/main.js",
58+
"types": "dist/main.d.ts",
5959
"license": "MIT",
6060
"name": "blapi",
6161
"repository": "botblock/BLAPI",
@@ -66,7 +66,7 @@
6666
"files": [
6767
"dist/"
6868
],
69-
"version": "2.1.0",
69+
"version": "2.2.0",
7070
"dependencies": {
7171
"centra": "^2.4.2"
7272
}

src/blapi.ts renamed to src/main.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Response } from 'centra';
2-
import { get, post, UserLogger } from './bttps';
2+
import { get, post, UserLogger } from './requests';
33
import fallbackData from './fallbackListData';
44
import legacyIdsFallbackData from './legacyIdsFallbackData';
55

@@ -51,7 +51,13 @@ type DiscordJSClientFallback = {
5151
))
5252
| null;
5353
guilds: {
54-
cache: Collection<string, { shardID: number; [k: string]: any }>;
54+
cache: Collection<
55+
string,
56+
(
57+
| { shardID: number; shardId: undefined }
58+
| { shardId: number; shardID: undefined }
59+
) & { [k: string]: any }
60+
>;
5561
};
5662
ws: {
5763
shards: Collection<number, { id: number; [k: string]: any }>;
@@ -60,17 +66,14 @@ type DiscordJSClientFallback = {
6066
[k: string]: any;
6167
};
6268

63-
64-
type LogOptions = boolean | { extended?: boolean, logger?: UserLogger }
65-
69+
type LogOptions = boolean | { extended?: boolean; logger?: UserLogger };
6670

6771
let listData = fallbackData as listDataType;
6872
let legacyIds = legacyIdsFallbackData as legacyIdDataType;
6973
const listAge = new Date();
7074
let extendedLogging = false;
7175
let useBotblockAPI = true;
7276

73-
7477
/**
7578
* the userLogger variable will later be defined with the
7679
* logger supplied by the user if they supplied any
@@ -79,12 +82,17 @@ let useBotblockAPI = true;
7982
let userLogger: UserLogger | undefined;
8083

8184
const log = {
82-
info: (msg: string) => (userLogger ? userLogger.info(`BLAPI: ${msg}`) : console.info(`[INFO] BLAPI: ${msg}`)),
83-
warn: (msg: string) => (userLogger ? userLogger.warn(`BLAPI: ${msg}`) : console.warn(`[WARN] BLAPI: ${msg}`)),
84-
error: (err: any) => (userLogger ? userLogger.error(`BLAPI: ${err}`) : console.error(`[ERROR] BLAPI ${err}`)),
85+
info: (msg: string) => (userLogger
86+
? userLogger.info(`BLAPI: ${msg}`)
87+
: console.info(`[INFO] BLAPI: ${msg}`)),
88+
warn: (msg: string) => (userLogger
89+
? userLogger.warn(`BLAPI: ${msg}`)
90+
: console.warn(`[WARN] BLAPI: ${msg}`)),
91+
error: (err: any) => (userLogger
92+
? userLogger.error(`BLAPI: ${err}`)
93+
: console.error(`[ERROR] BLAPI ${err}`)),
8594
};
8695

87-
8896
function convertLegacyIds(apiKeys: apiKeysObject) {
8997
const newApiKeys: apiKeysObject = { ...apiKeys };
9098
Object.entries(legacyIds).forEach(([list, newlist]) => {
@@ -191,7 +199,9 @@ async function postToAllLists(
191199
sendObj[list.api_shards] = shards;
192200
}
193201

194-
posts.push(post(apiPath, apiKeys[listname], sendObj, extendedLogging, log));
202+
posts.push(
203+
post(apiPath, apiKeys[listname], sendObj, extendedLogging, log),
204+
);
195205
}
196206
});
197207

@@ -249,14 +259,16 @@ async function handleInternal(
249259
// Get array of shards, loosing collection typings make this somewhat ugly
250260
shards = client.ws.shards.map(
251261
(s: { id: number }) => client.guilds.cache.filter(
252-
(g: { shardID: number }) => g.shardID === s.id,
262+
(
263+
g:
264+
| { shardID: number; shardId: undefined }
265+
| { shardId: number; shardID: undefined },
266+
) => g.shardID === s.id || g.shardId === s.id,
253267
).size,
254268
) as Array<number>;
255269
if (shards.length !== client.ws.shards.size) {
256270
// If not all shards are up yet, we skip this run of handleInternal
257-
log.info(
258-
"Not all shards are up yet, so we're skipping this run.",
259-
);
271+
log.info("Not all shards are up yet, so we're skipping this run.");
260272
return;
261273
}
262274
server_count = shards.reduce(
@@ -338,9 +350,9 @@ export function handle(
338350
/**
339351
* For when you don't use discord.js or just want to post to manual times
340352
* @param guildCount Integer value of guilds your bot is serving
341-
* @param botID Snowflake of the ID the user your bot is using
353+
* @param botId Snowflake of the ID the user your bot is using
342354
* @param apiKeys A JSON object formatted like: {"botlist name":"API Keys for that list", etc.}
343-
* @param shardID (optional) The shard ID, which will be used to identify the
355+
* @param shardId (optional) The shard ID, which will be used to identify the
344356
* shards valid for posting
345357
* (and for super efficient posting with BLAPIs own distributer when not using botBlock)
346358
* @param shardCount (optional) The number of shards the bot has, which is posted to the lists
@@ -422,13 +434,31 @@ export async function manualPost(
422434
export function setLogging(logOptions: LogOptions): void {
423435
// we are setting extendedLogging to the passed in logOptions
424436
// so users can disable extended logging later on
425-
if (typeof logOptions === 'boolean') extendedLogging = logOptions;
426-
if (typeof logOptions === 'object' && Object.prototype.hasOwnProperty.call(logOptions, 'extended') && typeof logOptions.extended === 'boolean') extendedLogging = logOptions.extended;
437+
if (typeof logOptions === 'boolean') {
438+
extendedLogging = logOptions;
439+
}
440+
if (
441+
typeof logOptions === 'object'
442+
&& Object.prototype.hasOwnProperty.call(logOptions, 'extended')
443+
&& typeof logOptions.extended === 'boolean'
444+
) {
445+
extendedLogging = logOptions.extended;
446+
}
427447
// no logger supplied by user
428-
if (!Object.prototype.hasOwnProperty.call(logOptions, 'logger')) return;
448+
if (!Object.prototype.hasOwnProperty.call(logOptions, 'logger')) {
449+
return;
450+
}
451+
const { logger } = logOptions as any; // we checked that it exists beforehand
429452
// making sure the logger supplied by the user has our required log levels (info, warn, error)
430-
// @ts-ignore
431-
if ((typeof logOptions.logger.info !== 'function') || (typeof logOptions.logger.warn !== 'function') || (typeof logOptions.logger.error !== 'function')) throw new Error('Your supplied logger does not seem to expose the log levels BLAPI needs to work. Make sure your logger offers the following methods: info() warn() error()');
453+
if (
454+
typeof logger.info !== 'function'
455+
|| typeof logger.warn !== 'function'
456+
|| typeof logger.error !== 'function'
457+
) {
458+
throw new Error(
459+
'Your supplied logger does not seem to expose the log levels BLAPI needs to work. Make sure your logger offers the following methods: info() warn() error()',
460+
);
461+
}
432462
// @ts-ignore
433463
userLogger = logOptions.logger;
434464
}

src/bttps.ts renamed to src/requests.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import c from 'centra';
22

3-
// defining the userlogger type there again
4-
// so we dont have to import it from ./blapi
53
export type UserLogger = {
64
info: (msg: string) => void,
75
warn: (msg: string) => void,

0 commit comments

Comments
 (0)