This commit is contained in:
Yuzu 2022-07-17 20:29:36 -05:00
parent 94852eae76
commit 94cfd9805c
19 changed files with 195 additions and 147 deletions

View File

@ -4,8 +4,8 @@
"entry": "./mod.ts",
"description": "A brand new bleeding edge non bloated Discord library",
"homepage": "https://github.com/oasisjs/biscuit",
"version": "0.1.1",
"releaseType": "patch",
"version": "0.2.0",
"releaseType": "minor",
"unstable": false,
"unlisted": false,
"files": [

View File

@ -170,7 +170,7 @@ export function WEBHOOK_MESSAGE(
webhookId: Snowflake,
token: string,
messageId: Snowflake,
options?: { threadId?: Snowflake }
options?: { threadId?: Snowflake },
): string {
let url = `/webhooks/${webhookId}/${token}/messages/${messageId}?`;
@ -369,7 +369,11 @@ export function GUILD_APPLICATION_COMMANDS(appId: Snowflake, guildId: Snowflake,
return `/applications/${appId}/guilds/${guildId}/commands`;
}
export function GUILD_APPLICATION_COMMANDS_PERMISSIONS(appId: Snowflake, guildId: Snowflake, commandId?: Snowflake): string {
export function GUILD_APPLICATION_COMMANDS_PERMISSIONS(
appId: Snowflake,
guildId: Snowflake,
commandId?: Snowflake,
): string {
if (commandId) return `/applications/${appId}/guilds/${guildId}/commands/${commandId}/permissions`;
return `/applications/${appId}/guilds/${guildId}/commands/permissions`;
}

View File

@ -203,9 +203,9 @@ export class Session extends EventEmitter {
async editProfile(nick?: string, avatarURL?: string | null): Promise<User> {
const avatar = avatarURL ? await urlToBase64(avatarURL) : avatarURL;
const user = await this.rest.runMethod<DiscordUser>(this.rest, 'PATCH', Routes.USER(), {
const user = await this.rest.runMethod<DiscordUser>(this.rest, "PATCH", Routes.USER(), {
username: nick,
avatar: avatar
avatar: avatar,
});
return new User(this, user);
}
@ -273,7 +273,10 @@ export class Session extends EventEmitter {
return new User(this, user);
}
createApplicationCommand(options: CreateApplicationCommand | CreateContextApplicationCommand, guildId?: Snowflake): Promise<DiscordApplicationCommand> {
createApplicationCommand(
options: CreateApplicationCommand | CreateContextApplicationCommand,
guildId?: Snowflake,
): Promise<DiscordApplicationCommand> {
return this.rest.runMethod<DiscordApplicationCommand>(
this.rest,
"POST",
@ -353,7 +356,10 @@ export class Session extends EventEmitter {
);
}
fetchApplicationCommandPermission(guildId: Snowflake, id: Snowflake): Promise<DiscordGuildApplicationCommandPermissions> {
fetchApplicationCommandPermission(
guildId: Snowflake,
id: Snowflake,
): Promise<DiscordGuildApplicationCommandPermissions> {
return this.rest.runMethod<DiscordGuildApplicationCommandPermissions>(
this.rest,
"GET",

View File

@ -41,7 +41,7 @@ export interface AllowedMentions {
/**
* @link https://github.com/denoland/deno_doc/blob/main/lib/types.d.ts
* channelId is optional when creating a reply, but will always be present when receiving an event/response that includes this data model.
* */
*/
export interface CreateMessageReference {
messageId: Snowflake;
channelId?: Snowflake;
@ -82,7 +82,7 @@ export type EmojiResolvable = string | {
/**
* A partial {@link User} to represent the author of a message sent by a webhook
* */
*/
export interface WebhookAuthor {
id: string;
username: string;
@ -108,7 +108,7 @@ export class Message implements Model {
users: data.mentions?.map((user) => new User(session, user)) ?? [],
roleIds: data.mention_roles ?? [],
channels: data.mention_channels?.map((channel) => ChannelFactory.from(session, channel)) ?? [],
}
};
if (!data.webhook_id) {
this.author = new User(session, data.author);
@ -335,7 +335,7 @@ export class Message implements Model {
/**
* Pins this message
* */
*/
async pin(): Promise<void> {
await this.session.rest.runMethod<undefined>(
this.session.rest,
@ -346,7 +346,7 @@ export class Message implements Model {
/**
* Unpins this message
* */
*/
async unpin(): Promise<void> {
await this.session.rest.runMethod<undefined>(
this.session.rest,
@ -487,7 +487,7 @@ export class Message implements Model {
/**
* same as Message.removeReaction but removes using a unicode emoji
* */
*/
async removeReactionEmoji(reaction: EmojiResolvable): Promise<void> {
const r = typeof reaction === "string" ? reaction : `${reaction.name}:${reaction.id}`;

View File

@ -1,7 +1,7 @@
import type { Model } from "./Base.ts";
import type { Snowflake } from "../Snowflake.ts";
import type { Session } from "../Session.ts";
import type { DiscordUser, UserFlags, PremiumTypes } from "../../discordeno/mod.ts";
import type { DiscordUser, PremiumTypes, UserFlags } from "../../discordeno/mod.ts";
import type { ImageFormat, ImageSize } from "../Util.ts";
import Util from "../Util.ts";
import * as Routes from "../Routes.ts";

View File

@ -1,10 +1,17 @@
import type { Model } from "./Base.ts";
import type { Session } from "../Session.ts";
import type { Snowflake } from "../Snowflake.ts";
import type { DiscordMessageComponents, DiscordEmbed, DiscordMessage, DiscordWebhook, FileContent, WebhookTypes } from "../../discordeno/mod.ts";
import type {
DiscordEmbed,
DiscordMessage,
DiscordMessageComponents,
DiscordWebhook,
FileContent,
WebhookTypes,
} from "../../discordeno/mod.ts";
import type { WebhookOptions } from "../Routes.ts";
import type { Attachment } from "./Attachment.ts";
import type { CreateMessage, AllowedMentions } from "./Message.ts";
import type { AllowedMentions, CreateMessage } from "./Message.ts";
import Util from "../Util.ts";
import User from "./User.ts";
import Message from "./Message.ts";
@ -12,7 +19,7 @@ import * as Routes from "../Routes.ts";
/**
* @link https://discord.com/developers/docs/resources/webhook#edit-webhook-message-jsonform-params
* */
*/
export interface EditWebhookMessage {
content?: string;
embeds?: DiscordEmbed[];
@ -60,7 +67,9 @@ export class Webhook implements Model {
guildId?: Snowflake;
user?: User;
async execute(options?: WebhookOptions & CreateMessage & { avatarUrl?: string; username?: string }): Promise<(Message | undefined)> {
async execute(
options?: WebhookOptions & CreateMessage & { avatarUrl?: string; username?: string },
): Promise<(Message | undefined)> {
if (!this.token) {
return;
}
@ -127,7 +136,10 @@ export class Webhook implements Model {
);
}
async editMessage(messageId?: Snowflake, options?: EditWebhookMessage & { threadId?: Snowflake }): Promise<Message> {
async editMessage(
messageId?: Snowflake,
options?: EditWebhookMessage & { threadId?: Snowflake },
): Promise<Message> {
if (!this.token) {
throw new Error("No token found");
}
@ -162,7 +174,7 @@ export class Webhook implements Model {
ephemeral: attachment.ephemeral,
};
}),
}
},
);
return new Message(this.session, message);

View File

@ -17,10 +17,8 @@ export class TextInput extends BaseComponent implements TextInputComponent {
this.placeholder = data.placeholder;
this.value = data.value;
this.minLength = data.min_length;
this.maxLength = data.max_length;
}
readonly session: Session;

View File

@ -23,7 +23,7 @@ import { Snowflake } from "../Snowflake.ts";
import Util from "../Util.ts";
import * as Routes from "../Routes.ts";
import WelcomeScreen from "./WelcomeScreen.ts";
import { GuildChannel, ThreadChannel, ReturnThreadsArchive } from "./channels.ts";
import { GuildChannel, ReturnThreadsArchive, ThreadChannel } from "./channels.ts";
import ThreadMember from "./ThreadMember.ts";
import Member from "./Member.ts";
import Role from "./Role.ts";
@ -320,7 +320,7 @@ export class Guild extends BaseGuild implements Model {
this.vefificationLevel = data.verification_level;
this.defaultMessageNotificationLevel = data.default_message_notifications;
this.explicitContentFilterLevel = data.explicit_content_filter;
this.premiumTier = data.premium_tier
this.premiumTier = data.premium_tier;
this.members = new Map(
data.members?.map((member) => [data.id, new Member(session, { ...member, user: member.user! }, data.id)]),
@ -628,7 +628,7 @@ export class Guild extends BaseGuild implements Model {
return result.pruned;
}
async getActiveThreads(): Promise<Omit<ReturnThreadsArchive, 'hasMore'>> {
async getActiveThreads(): Promise<Omit<ReturnThreadsArchive, "hasMore">> {
const { threads, members } = await this.session.rest.runMethod<DiscordListActiveThreads>(
this.session.rest,
"GET",

View File

@ -1,10 +1,6 @@
import type { Model } from "../Base.ts";
import type { Session } from "../../Session.ts";
import type {
DiscordInteraction,
DiscordMessage,
DiscordMessageComponents,
} from "../../../discordeno/mod.ts";
import type { DiscordInteraction, DiscordMessage, DiscordMessageComponents } from "../../../discordeno/mod.ts";
import type CommandInteraction from "./CommandInteraction.ts";
import type PingInteraction from "./PingInteraction.ts";
import type ComponentInteraction from "./ComponentInteraction.ts";
@ -13,7 +9,7 @@ import type AutoCompleteInteraction from "./AutoCompleteInteraction.ts";
import type { CreateMessage } from "../Message.ts";
import type { MessageFlags } from "../../Util.ts";
import type { EditWebhookMessage } from "../Webhook.ts";
import { InteractionTypes, InteractionResponseTypes } from "../../../discordeno/mod.ts";
import { InteractionResponseTypes, InteractionTypes } from "../../../discordeno/mod.ts";
import { Snowflake } from "../../Snowflake.ts";
import User from "../User.ts";
import Member from "../Member.ts";
@ -22,7 +18,6 @@ import Permsisions from "../Permissions.ts";
import Webhook from "../Webhook.ts";
import * as Routes from "../../Routes.ts";
/**
* @link https://discord.com/developers/docs/interactions/slash-commands#interaction-response
*/
@ -157,7 +152,7 @@ export abstract class BaseInteraction implements Model {
};
}),
message_id: options.messageId,
}
},
);
if (!message || !options.messageId) {
@ -184,7 +179,7 @@ export abstract class BaseInteraction implements Model {
token: this.token,
},
messageId,
options
options,
);
return message;
@ -194,10 +189,10 @@ export abstract class BaseInteraction implements Model {
await Webhook.prototype.deleteMessage.call(
{
id: this.session.applicationId,
token: this.token
token: this.token,
},
messageId,
options
options,
);
}
@ -208,7 +203,7 @@ export abstract class BaseInteraction implements Model {
token: this.token,
},
messageId,
options
options,
);
return message;
@ -219,7 +214,9 @@ export abstract class BaseInteraction implements Model {
// deno-fmt-ignore
async respond(resp: InteractionResponse): Promise<Message | undefined>;
async respond(resp: { with: InteractionApplicationCommandCallbackData }): Promise<Message | undefined>;
async respond(resp: InteractionResponse | { with: InteractionApplicationCommandCallbackData }): Promise<Message | undefined> {
async respond(
resp: InteractionResponse | { with: InteractionApplicationCommandCallbackData },
): Promise<Message | undefined> {
const options = "with" in resp ? resp.with : resp.data;
const type = "type" in resp ? resp.type : InteractionResponseTypes.ChannelMessageWithSource;

View File

@ -86,7 +86,7 @@ export class CommandInteractionOptionResolver {
properties: Array<keyof CommandInteractionOption>,
required: boolean,
): CommandInteractionOption | void {
const option: (CommandInteractionOption | undefined) = this.get(name, required);
const option: CommandInteractionOption | undefined = this.get(name, required);
if (!option) {
return;
@ -106,7 +106,7 @@ export class CommandInteractionOptionResolver {
get(name: string | number, required: true): CommandInteractionOption;
get(name: string | number, required: boolean): CommandInteractionOption | undefined;
get(name: string | number, required?: boolean) {
const option: (CommandInteractionOption | undefined) = this.hoistedOptions.find((o) =>
const option: CommandInteractionOption | undefined = this.hoistedOptions.find((o) =>
typeof name === "number" ? o.name === name.toString() : o.name === name
);
@ -125,7 +125,12 @@ export class CommandInteractionOptionResolver {
getString(name: string | number, required: true): string;
getString(name: string | number, required?: boolean): string | undefined;
getString(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.String, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.String,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -134,7 +139,12 @@ export class CommandInteractionOptionResolver {
getNumber(name: string | number, required: true): number;
getNumber(name: string | number, required?: boolean): number | undefined;
getNumber(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Number, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Number,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -143,7 +153,12 @@ export class CommandInteractionOptionResolver {
getInteger(name: string | number, required: true): number;
getInteger(name: string | number, required?: boolean): number | undefined;
getInteger(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Integer, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Integer,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -152,7 +167,12 @@ export class CommandInteractionOptionResolver {
getBoolean(name: string | number, required: true): boolean;
getBoolean(name: string | number, required?: boolean): boolean | undefined;
getBoolean(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Boolean, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Boolean,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -161,7 +181,9 @@ export class CommandInteractionOptionResolver {
getUser(name: string | number, required: true): bigint;
getUser(name: string | number, required?: boolean): bigint | undefined;
getUser(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.User, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(name, ApplicationCommandOptionTypes.User, [
"Otherwise",
], required);
return option?.Otherwise ?? undefined;
}
@ -170,7 +192,12 @@ export class CommandInteractionOptionResolver {
getChannel(name: string | number, required: true): bigint;
getChannel(name: string | number, required?: boolean): bigint | undefined;
getChannel(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Channel, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Channel,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -179,7 +206,12 @@ export class CommandInteractionOptionResolver {
getMentionable(name: string | number, required: true): string;
getMentionable(name: string | number, required?: boolean): string | undefined;
getMentionable(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Mentionable, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Mentionable,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
@ -188,7 +220,9 @@ export class CommandInteractionOptionResolver {
getRole(name: string | number, required: true): bigint;
getRole(name: string | number, required?: boolean): bigint | undefined;
getRole(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Role, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(name, ApplicationCommandOptionTypes.Role, [
"Otherwise",
], required);
return option?.Otherwise ?? undefined;
}
@ -197,14 +231,19 @@ export class CommandInteractionOptionResolver {
getAttachment(name: string | number, required: true): string;
getAttachment(name: string | number, required?: boolean): string | undefined;
getAttachment(name: string | number, required = false) {
const option: (CommandInteractionOption | void) = this.getTypedOption(name, ApplicationCommandOptionTypes.Attachment, ["Otherwise"], required);
const option: CommandInteractionOption | void = this.getTypedOption(
name,
ApplicationCommandOptionTypes.Attachment,
["Otherwise"],
required,
);
return option?.Otherwise ?? undefined;
}
/** searches for the focused option */
getFocused(full = false): string | number | bigint | boolean | undefined | CommandInteractionOption {
const focusedOption: (CommandInteractionOption | void) = this.hoistedOptions.find((option) => option.focused);
const focusedOption: CommandInteractionOption | void = this.hoistedOptions.find((option) => option.focused);
if (!focusedOption) {
throw new TypeError("No option found");

View File

@ -2,7 +2,7 @@ import type { Model } from "../Base.ts";
import type { Snowflake } from "../../Snowflake.ts";
import type { Session } from "../../Session.ts";
import type { DiscordInteraction, InteractionTypes } from "../../../discordeno/mod.ts";
import { MessageComponentTypes, InteractionResponseTypes } from "../../../discordeno/mod.ts";
import { InteractionResponseTypes, MessageComponentTypes } from "../../../discordeno/mod.ts";
import BaseInteraction from "./BaseInteraction.ts";
import Message from "../Message.ts";

View File

@ -12,14 +12,14 @@ import ModalSubmitInteraction from "./ModalSubmitInteraction.ts";
/**
* @link https://discord.com/developers/docs/interactions/receiving-and-responding#message-interaction-object-message-interaction-structure
* */
*/
export interface MessageInteraction {
/** id of the interaction */
id: Snowflake;
/** type of interaction */
type: InteractionTypes
type: InteractionTypes;
/** name of the application command, including subcommands and subcommand groups */
name: string
name: string;
/** user who invoked the interaction */
user: User;
/** member who invoked the interaction in the guild */
@ -49,7 +49,11 @@ export class InteractionFactory {
}
}
static fromMessage(session: Session, interaction: DiscordMessageInteraction, _guildId?: Snowflake): MessageInteraction {
static fromMessage(
session: Session,
interaction: DiscordMessageInteraction,
_guildId?: Snowflake,
): MessageInteraction {
const obj = {
id: interaction.id,
type: interaction.type,

View File

@ -1,4 +1,3 @@
import type { Session, Snowflake } from "./deps.ts";
export class Collection<V> extends Map<Snowflake, V> {

View File

@ -1,10 +1,4 @@
import type {
ChannelInGuild,
ChannelWithMessagesInGuild,
ChannelTypes,
DiscordChannel,
Snowflake,
} from "./deps.ts";
import type { ChannelInGuild, ChannelTypes, ChannelWithMessagesInGuild, DiscordChannel, Snowflake } from "./deps.ts";
import type { CachedMessage } from "./messages.ts";
import type { CachedGuild } from "./guilds.ts";
import type { SessionCache } from "./mod.ts";
@ -30,9 +24,12 @@ export interface CachedDMChannel extends DMChannel {
export function channelBootstrapper(cache: SessionCache, channel: DiscordChannel) {
if (!channel.guild_id) {
cache.dms.set(channel.id, Object.assign(new DMChannel(cache.session, channel), {
cache.dms.set(
channel.id,
Object.assign(new DMChannel(cache.session, channel), {
messages: new Collection<CachedMessage>(cache.session),
}))
}),
);
return;
}
@ -47,7 +44,7 @@ export function channelBootstrapper(cache: SessionCache, channel: DiscordChannel
guildId: channel.guild_id!,
get guild(): CachedGuild {
return cache.guilds.get(this.guildId)!;
}
},
},
),
);
@ -60,11 +57,10 @@ export function channelBootstrapper(cache: SessionCache, channel: DiscordChannel
guildId: channel.guild_id!,
get guild(): CachedGuild {
return cache.guilds.get(this.guildId)!;
}
}
},
},
),
);
}
});
}

View File

@ -1,7 +1,4 @@
import type {
DiscordGuild,
DiscordMemberWithUser,
} from "./deps.ts";
import type { DiscordGuild, DiscordMemberWithUser } from "./deps.ts";
import type { SessionCache } from "./mod.ts";
import type { CachedMember } from "./members.ts";
import type { CachedUser } from "./users.ts";

View File

@ -1,11 +1,11 @@
import type {
DiscordEmoji,
DiscordMessage,
DiscordMemberWithUser,
DiscordMessage,
DiscordMessageReactionAdd,
DiscordMessageReactionRemove,
DiscordMessageReactionRemoveAll,
Snowflake
Snowflake,
} from "./deps.ts";
import type { CachedUser } from "./users.ts";
import type { SessionCache } from "./mod.ts";

View File

@ -7,11 +7,7 @@ import { memberBootstrapper } from "./members.ts";
import { userBootstrapper } from "./users.ts";
import { channelBootstrapper } from "./channels.ts";
import { guildBootstrapper } from "./guilds.ts";
import {
messageBootstrapper,
reactionBootstrapper,
reactionBootstrapperDeletions
} from "./messages.ts";
import { messageBootstrapper, reactionBootstrapper, reactionBootstrapperDeletions } from "./messages.ts";
export const cache_sym = Symbol("@cache");