@confis/discordapiwrapper
Version:
A fast and lightweight discord api wrapper.
594 lines (593 loc) • 16.2 kB
TypeScript
import { Manager } from "./internal/Manager";
import { ActionRowBuilder, EmbedBuilder } from "./structure/Builders";
import { Channel } from "./structure/Channel";
import { Collector, ModalCollector } from "./structure/Collector";
import { Guild } from "./structure/Guild";
import { Interaction, ModalInteraction } from "./structure/Interactions";
import { Member } from "./structure/Member";
import { Message, WebhookMessage } from "./structure/Message";
import { Role } from "./structure/Role";
import { SlashCommandBuilder } from "./structure/SlashCommandBuilder";
import { User } from "./structure/User";
import { Rest } from "./internal/Rest";
type PRESENCES = "online" | "dnd" | "invisible" | "idle";
export type Snowflake = string;
/**
* Guild Member Event Object
*/
export interface APIGuildMemberEvent extends APIMember {
guild_id: string;
}
/** Enum for types of webhook */
export declare enum WebhookTypes {
INCOMING = 1,
CHANNEL_FOLLOWER = 2,
APPLICATION = 3
}
/** Enum for types of interaction contexts */
export declare enum InteractionContextTypes {
GUILD = 0,
BOT_DM = 1,
PRIVATE_CHANNEL = 2
}
/** Enum for types of interaction integration */
export declare enum InteractionIntegrationTypes {
GUILD_INSTALL = 0,
USER_INSTALL = 1
}
/** Enum for types of message */
export declare enum APIMessageTypes {
DEFAULT = 0,
RECIPIENT_ADD = 1,
RECIPIENT_REMOVE = 2,
CALL = 3,
CHANNEL_NAME_CHANGE = 4,
CHANNEL_ICON_CHANGE = 5,
CHANNEL_PINNED_MESSAGE = 6,
USER_JOIN = 7,
GUILD_BOOST = 8,
GUILD_BOOST_TIER_1 = 9,
GUILD_BOOST_TIER_2 = 10,
GUILD_BOOST_TIER_3 = 11,
CHANNEL_FOLLOW_ADD = 12,
GUILD_DISCOVERY_DISQUALIFIED = 14,
GUILD_DISCOVERY_REQUALIFIED = 15,
GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16,
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17,
THREAD_CREATED = 18,
REPLY = 19,
CHAT_INPUT_COMMAND = 20,
THREAD_STARTER_MESSAGE = 21,
GUILD_INVITE_REMINDER = 22,
CONTEXT_MENU_COMMAND = 23,
AUTO_MODERATION_ACTION = 24,
ROLE_SUBSCRIPTION_PURCHASE = 25,
INTERACTION_PREMIUM_UPSELL = 26,
STAGE_START = 27,
STAGE_END = 28,
STAGE_SPEAKER = 29,
STAGE_TOPIC = 31,
GUILD_APPLICATION_PREMIUM_SUBSCRIPTION = 32,
GUILD_INCIDENT_ALERT_MODE_ENABLED = 36,
GUILD_INCIDENT_ALERT_MODE_DISABLED = 37,
GUILD_INCIDENT_REPORT_RAID = 38,
GUILD_INCIDENT_REPORT_FALSE_ALARM = 39,
PURCHASE_NOTIFICATION = 44,
POLL_RESULT = 46
}
/** Webhook Object */
export interface APIWebhookObject extends BaseData {
type: WebhookTypes;
guild_id?: string | null;
channel_id: string | null;
user?: APIUser;
name: string | null;
avatar: string | null;
token?: string;
application_id: string | null;
url?: string;
}
export interface AvatarDecoration {
asset: string;
sku_id: Snowflake;
}
interface CommandData extends BaseData {
name: string;
description: string;
guild_id?: string;
}
/** User Object */
export interface APIUser extends BaseData {
username: string;
discriminator: string;
global_name: string | null;
avatar: string | null;
bot?: boolean;
system?: boolean;
avatar_decoration_data?: AvatarDecoration | null;
}
/** Member Object */
export interface APIMember {
user: APIUser;
nick?: string | null;
avatar?: string | null;
roles: APIRole[];
joined_at: string;
deaf: boolean;
mute: boolean;
flags: number;
permissions: string;
}
/** Message Attachment Object */
export interface APIMessageAttachment extends BaseData {
filename: string;
size: number;
url: string;
proxy_url: string;
content_type?: string;
}
/** Embed Object */
export interface APIEmbed {
title?: string;
description?: string;
url?: string;
color?: number;
footer?: {
text: string;
icon_url?: string;
proxy_icon_url?: string;
};
image?: {
url: string;
proxy_url?: string;
height?: number;
width: number;
};
thumbnail?: {
url: string;
proxy_url?: string;
height?: number;
width: number;
};
author?: {
name: string;
url?: string;
icon_url?: string;
proxy_icon_url?: string;
};
fields?: {
name: string;
value: string;
inline?: boolean;
}[];
}
/** Message Object */
export interface APIMessage extends BaseData {
channel_id: string;
author: APIUser;
content: string;
timestamp: string;
edited_timestamp: string;
tts: boolean;
mention_everyone: boolean;
mentions: APIUser[];
mention_roles: APIRole[];
pinned: boolean;
type: number;
referenced_message?: APIMessage;
guild_id: string;
attachments: APIMessageAttachment[];
embeds: APIEmbed[];
message_snapshots?: APIMessageSnapshot[];
}
export interface APIMessageSnapshotPartial extends Pick<APIMessage, "type" | "content" | "embeds" | "attachments" | "timestamp" | "edited_timestamp" | "mentions" | "mention_roles"> {
}
export interface APIMessageSnapshot {
message: APIMessageSnapshotPartial;
}
/** Webhook Message Object */
export interface APIWebhookMessage extends Omit<APIMessage, "author"> {
webhook_id?: string;
author: JSONCache;
}
/** Role Object */
export interface APIRole extends BaseData {
name: string;
color: number;
hoist: boolean;
icon?: string | null;
unicode_emoji?: string | null;
position: number;
permissions: string[];
managed: boolean;
mentionable: boolean;
flags: number;
}
/** Application Command Interaction Data Options */
export interface APIApplicationCommandOptionsData {
name: string;
type: ApplicationCommandOptionTypes;
value?: string | number | boolean;
options?: APIApplicationCommandOptionsData[];
focused?: boolean;
}
/** Client Events */
export interface ClientEvents {
ready: [];
messageCreate: [message: Message];
messageUpdate: [message: Message];
messageDelete: [
messageData: {
id: string;
channel_id: string;
}
];
guildCreate: [guild: Guild];
guildDelete: [guild: Guild];
memberUpdate: [oldMember: Member, newMember: Member];
interactionCreate: [interaction: Interaction];
resume: [];
roleCreate: [role: Role];
roleUpdate: [oldRole: Role, newRole: Role, guild: Guild];
roleDelete: [role: Role];
channelCreate: [channel: Channel];
channelDelete: [channel: Channel];
channelUpdate: [oldChannel: Channel, newChannel: Channel];
userUpdate: [oldUser: User, newUser: User];
webhookMessageCreate: [message: WebhookMessage];
}
/** Emoji Object */
export interface APIEmoji {
id: string;
name: string;
roles?: string[];
user?: User;
require_colons?: boolean;
managed?: boolean;
animated?: boolean;
available?: boolean;
}
/**
* Partial Emoji object, this object is used for when only the ID, name, and animated status are needed.
*/
export type PartialEmoji = Omit<APIEmoji, "available" | "animated" | "managed" | "require_colons" | "user" | "roles">;
/**
* Base data object for all objects.
*/
export interface BaseData {
id: string;
[key: string]: any;
}
/**
* Enum for types of application commands
*/
export declare enum ApplicationCommandTypes {
CHAT_INPUT = 1,
USER = 2,
MESSAGE = 3
}
/**
* Enum for types of interactions
*/
export declare enum InteractionTypes {
PING = 1,
APPLICATION_COMMAND = 2,
MESSAGE_COMPONENT = 3,
APPLICATION_COMMAND_AUTOCOMPLETE = 4,
MODAL_SUBMIT = 5
}
/**
* Enum for types of components
*/
export declare enum ComponentTypes {
ACTION_ROW = 1,
BUTTON = 2,
STRING_SELECT = 3,
TEXT_INPUT = 4
}
/**
* Enum for types of button styles
*/
export declare enum ButtonStyles {
PRIMARY = 1,
SECONDARY = 2,
SUCCESS = 3,
DANGER = 4,
LINK = 5
}
/**
* Enum for types of activity
*/
export declare enum ActivityTypes {
GAME = 0,
STREAMING = 1,
LISTENING = 2,
WATCHING = 3,
CUSTOM = 4,
COMPETING = 5
}
/**
* Enum for types of application command options
*/
export declare enum ApplicationCommandOptionTypes {
SUB_COMMAND = 1,
SUB_COMMAND_GROUP = 2,
STRING = 3,
INTEGER = 4,
BOOLEAN = 5,
USER = 6,
CHANNEL = 7,
ROLE = 8,
MENTIONABLE = 9,
NUMBER = 10,
ATTACHMENT = 11
}
/**
* Enum for types of channels
*/
export declare enum ChannelTypes {
TEXT = 0,
DM = 1,
CATEGORY = 4
}
export declare enum OverwriteObjectTypes {
ROLE = 0,
MEMBER = 1
}
/**
* File content object
*/
export interface FileContent {
name: string;
buffer: Buffer;
}
/**
* Options for sending a message
*/
export interface ContentOptions {
content?: string;
embeds?: EmbedBuilder[];
components?: ActionRowBuilder<any>[];
ephemeral?: boolean;
file?: FileContent | FileContent[];
poll?: PollRequestObject;
}
/**
* Options for sending a message to a webhook
*/
export interface WebhookContentOptions extends ContentOptions {
username?: string;
avatarURL?: string;
}
/**
* Poll media object, this object is used for the question and answers of a poll.
*/
export interface PollMediaObject {
text: string;
emoji?: PartialEmoji;
}
/**
* Poll request object, this object is used for creating a poll.
*/
export interface PollRequestObject {
question: PollMediaObject;
answers: {
answer_id: number;
poll_media: PollMediaObject;
}[];
duration: number;
allow_multiselect: boolean;
}
/**
* JSON Cache object, this object is used for caching data from the discord API.
*/
export interface JSONCache {
[x: string]: unknown;
}
/** Enum of types of messages */
export declare enum MessageTypes {
DEFAULT = 0,
USER_JOIN = 7,
REPLY = 19
}
/** Bot Intents */
export declare enum Intents {
GUILDS = 1,
GUILD_MEMBERS = 2,
GUILD_BANS = 4,
GUILD_EMOJIS_AND_STICKERS = 8,
GUILD_INTEGRATIONS = 16,
GUILD_WEBHOOKS = 32,
GUILD_INVITES = 64,
GUILD_VOICE_STATES = 128,
GUILD_PRESENCES = 256,
GUILD_MESSAGES = 512,
GUILD_MESSAGE_REACTIONS = 1024,
GUILD_MESSAGE_TYPING = 2048,
DIRECT_MESSAGES = 4096,
DIRECT_MESSAGE_REACTIONS = 8192,
DIRECT_MESSAGE_TYPING = 16384,
MESSAGE_CONTENT = 32768,
GUILD_SCHEDULED_EVENTS = 65536,
ALL = 131071
}
/**
* Discord Client
*/
export declare class Client {
#private;
shards: number;
readonly users: Manager<User>;
readonly guilds: Manager<Guild>;
readonly channels: Manager<Channel>;
readonly roles: Manager<Role>;
readonly rest: Rest;
collectors: Collector[];
modalCollectors: ModalCollector[];
logger: {
info: (...args: any[]) => any;
error: (...args: any[]) => any;
};
readonly baseURL = "https://discord.com/api/v10/";
private readonly eventHandlers;
get commands(): CommandData[];
/**
* The user object for the bot.
* @returns {User} The bot user object.
*/
get user(): User;
/**
* The bot token used to authenticate with the API.
* @returns {string} The bot token.
*/
get token(): string;
/**
* The ping of the bot, this is the time it takes for the bot to receive a heartbeat ack from discord.
* @returns {number} The ping of the bot in milliseconds.
*/
get ping(): number;
/**
* Sends a JSON payload to the discord gateway, if the socket is not connected it will cache the payload and send it when the socket is connected.
* @param json The JSON payload to send.
*/
private sendToSocket;
/**
* Pings discord every "ms" milliseconds
* @param ms Milliseconds
* @returns Interval
*/
private _heartbeat;
/**
* Sets the default logger for the client, this will be used for all logging done by the client.
* @param logger The logger object to use, this must have an "info" and "error" method.
*/
setDefaultLogger(logger: {
info: (...args: any[]) => any;
error: (...args: any[]) => any;
}): void;
/**
* Create a new Discord client
* @param token The bot token to use
* @param options Options for the client
*/
constructor(token: string, options?: {
cacheAllUsers?: boolean;
intents?: Intents[];
shards?: "auto" | number;
});
/**
* Define the payload for the websocket connection, this will be used to connect to the discord gateway.
*/
private _definePayload;
/**
* Register shards for the bot
*/
private _registerShards;
/**
* The number of milliseconds the bot has been up for since Epoch
*/
get uptime(): number;
/**
* Register a channel from the Discord API.
* @param channelID The ID of the channel to register.
*/
registerChannelFromAPI(channelID: string): Promise<void>;
/**
* Emit an event, this will call all listeners for the event.
* @param event Event to emit
* @param args Arguments to pass to the event listeners
*/
private emit;
/**
* Listen for an event
* @param event Event to listen for
* @param callback Event callback
*/
on<K extends keyof ClientEvents>(event: K, callback: (...args: ClientEvents[K]) => any): this;
/**
* Listen for an event once
* @param event Event to listen for
* @param callback Event callback
*/
once<K extends keyof ClientEvents>(event: K, callback: (...args: ClientEvents[K]) => any): this;
/**
* Get the headers for a request
* @param contentType The content type of the request. Defaults to `application/json`
* @returns The headers for the request
*/
getHeaders(contentType?: string): {
Authorization: string;
"Content-Type": string;
};
/**
* Close websocket and remove all its listeners
*/
disconnect(): void;
/**
* Get guild commands from discord API
* @param guild The ID of the guild or a {@link Guild} object
* @returns Guild commands
*/
getGuildCommands(guild: string | Guild): Promise<any>;
/**
* Bulk overwrite guild commands
* @param guild The ID of the guild or a {@link Guild} object
* @param commands Array of {@link SlashCommandBuilder}
*/
setGuildCommands(guild: string | Guild, commands: SlashCommandBuilder[]): Promise<void>;
/**
* Get the global commands for the application from the discord API
* @returns Global commands
*/
getCommands(): Promise<any>;
/**
* Bulk overwrite glboal commands
* @param commands Array of {@link SlashCommandBuilder}
*/
setGlobalCommands(...commands: SlashCommandBuilder[]): Promise<void>;
/**
* Update bot presence
* @param data Presence data
*/
editStatus(data: PRESENCES | {
activity?: {
name: string;
type: ActivityTypes;
};
status: PRESENCES;
}): void;
/**
* Fetch a message from the discord API
* @param channelID The id of the channel the message is in
* @param messageID The id of the message
* @returns A {@link Message} object
*/
getMessage(channelID: string, messageID: string): Promise<Message>;
/**
* Get cached channel
* @param channelID The id of the channel
* @returns A {@link Channel} object
*/
getChannel(channelID: string): Channel;
/**
* Delete channels from the cache
* @param id The ID of the channel or an array of channel IDs to delete
*/
private _deleteChannels;
private _deleteRoles;
/**
* Connect to discord websocket
*/
connect(): Promise<void>;
/**
* Creates a collector for modal submissions
* @param customID The custom ID of the modal to collect
* @param options Collector options
* @returns A modal collector object
*/
createModalCollector(customID: string, options?: {
timeout?: number;
filter?: (i: ModalInteraction) => boolean;
}): ModalCollector;
}
export {};