Files
discord.js/typings/index.ts
2021-06-29 20:36:04 +01:00

101 lines
2.8 KiB
TypeScript

/// <reference path="index.d.ts" />
import {
Client,
Collection,
Intents,
Message,
MessageAttachment,
MessageEmbed,
Permissions,
Serialized,
ShardClientUtil,
ShardingManager,
} from 'discord.js';
const client: Client = new Client({
intents: Intents.NON_PRIVILEGED,
});
client.on('ready', () => {
console.log(`Client is logged in as ${client.user!.tag} and ready!`);
});
client.on('guildCreate', g => {
const channel = g.channels.cache.random();
if (!channel) return;
channel.setName('foo').then(updatedChannel => {
console.log(`New channel name: ${updatedChannel.name}`);
});
});
client.on('messageReactionRemoveAll', async message => {
console.log(`messageReactionRemoveAll - id: ${message.id} (${message.id.length})`);
if (message.partial) message = await message.fetch();
console.log(`messageReactionRemoveAll - content: ${message.content}`);
});
// This is to check that stuff is the right type
declare const assertIsMessage: (m: Promise<Message>) => void;
client.on('message', ({ channel }) => {
assertIsMessage(channel.send('string'));
assertIsMessage(channel.send({}));
assertIsMessage(channel.send({ embeds: [] }));
const attachment = new MessageAttachment('file.png');
const embed = new MessageEmbed();
assertIsMessage(channel.send({ files: [attachment] }));
assertIsMessage(channel.send({ embeds: [embed] }));
assertIsMessage(channel.send({ embeds: [embed], files: [attachment] }));
// @ts-expect-error
channel.send();
// @ts-expect-error
channel.send({ another: 'property' });
});
client.login('absolutely-valid-token');
// Test type transformation:
declare const assertType: <T>(value: T) => asserts value is T;
declare const serialize: <T>(value: T) => Serialized<T>;
assertType<undefined>(serialize(undefined));
assertType<null>(serialize(null));
assertType<number[]>(serialize([1, 2, 3]));
assertType<{}>(serialize(new Set([1, 2, 3])));
assertType<{}>(
serialize(
new Map([
[1, '2'],
[2, '4'],
]),
),
);
assertType<string>(serialize(new Permissions(Permissions.FLAGS.ATTACH_FILES)));
assertType<number>(serialize(new Intents(Intents.FLAGS.GUILDS)));
assertType<unknown>(
serialize(
new Collection([
[1, '2'],
[2, '4'],
]),
),
);
assertType<never>(serialize(Symbol('a')));
assertType<never>(serialize(() => {}));
assertType<never>(serialize(BigInt(42)));
// Test type return of broadcastEval:
declare const shardClientUtil: ShardClientUtil;
declare const shardingManager: ShardingManager;
assertType<Promise<number[]>>(shardingManager.broadcastEval(() => 1));
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(() => 1));
assertType<Promise<number[]>>(shardingManager.broadcastEval(async () => 1));
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(async () => 1));