diff --git a/examples/bun.js b/examples/bun.js index 833120c..0dea8f8 100644 --- a/examples/bun.js +++ b/examples/bun.js @@ -1,8 +1,11 @@ /** * Bun example + * this example should work on most systems, but if it doesn't just clone the library and import everything from mod.ts */ import { GatewayIntents, Session } from "@oasisjs/biscuit"; +// if it didn't worked use: +// const { GatewayIntents, Session } = require("@oasisjs/biscuit"); const token = process.env.TOKEN; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; diff --git a/packages/biscuit/Routes.ts b/packages/biscuit/Routes.ts index c1e6ecd..353ee04 100644 --- a/packages/biscuit/Routes.ts +++ b/packages/biscuit/Routes.ts @@ -3,6 +3,10 @@ import type { Snowflake } from "./Snowflake.ts"; // cdn endpoints export * from "./Cdn.ts"; +export function USER(userId: Snowflake) { + return `/users/${userId}`; +} + export function GATEWAY_BOT() { return "/gateway/bot"; } diff --git a/packages/biscuit/Session.ts b/packages/biscuit/Session.ts index c3a84ed..847acb7 100644 --- a/packages/biscuit/Session.ts +++ b/packages/biscuit/Session.ts @@ -1,4 +1,5 @@ import type { + StatusTypes, ApplicationCommandPermissionTypes, AtLeastOne, DiscordApplicationCommand, @@ -8,11 +9,13 @@ import type { GatewayBot, GatewayIntents, Localization, + DiscordUser, } from "../discordeno/mod.ts"; import type { DiscordGatewayPayload, Shard } from "../discordeno/mod.ts"; import type { Events } from "./Actions.ts"; import type { PermissionResolvable } from "./structures/Permissions.ts"; +import type { Activities } from "./structures/Presence.ts"; import { Permissions } from "./structures/Permissions.ts"; import { Snowflake } from "./Snowflake.ts"; @@ -22,8 +25,12 @@ import { createGatewayManager, createRestManager, getBotIdFromToken, + GatewayOpcodes, + StatusTypes } from "../discordeno/mod.ts"; +import User from "./structures/User.ts"; + import * as Routes from "./Routes.ts"; import * as Actions from "./Actions.ts"; @@ -102,6 +109,14 @@ export interface SessionOptions { gateway?: GatewayOptions; } +/** + * @link https://discord.com/developers/docs/topics/gateway#update-status + * */ +export interface StatusUpdate { + activities: Activities[]; + status: StatusTypes; +} + /** * Receives a Token, connects * Most of the command implementations were adapted from Discordeno (https://github.com/discordeno/discordeno) @@ -187,7 +202,68 @@ export class Session extends EventEmitter { return super.emit(event, ...params); } - createApplicationCommand(options: CreateApplicationCommand | CreateContextApplicationCommand, guildId: Snowflake) { + /** + * Edit bot's status + * tip: execute this on the ready event if possible + * @example + * for (const { id } of session.gateway.manager.shards) { + * session.editStatus(id, data); + * } + * */ + editStatus(shardId: number, status: StatusUpdate): void { + const shard = this.gateway.manager.shards.get(shardId); + + if (!shard) { + throw new Error(`Unknown shard ${shardId}`); + } + + shard.send({ + op: GatewayOpcodes.PresenceUpdate, + d: { + status: status.status, + since: null, + afk: false, + activities: status.activities.map((activity) => { + return { + name: activity.name, + type: activity.type, + url: activity.url, + created_at: activity.createdAt, + timestamps: activity.timestamps, + application_id: this.applicationId, + details: activity.details, + state: activity.state, + emoji: activity.emoji || { + name: activity.emoji!.name, + id: activity.emoji!.id, + animated: activity.emoji!.animated + }, + party: activity.party, + assets: activity.assets ? { + large_image: activity.assets.largeImage, + large_text: activity.assets.largeText, + small_image: activity.assets.smallImage, + small_text: activity.assets.smallText, + } : undefined, + secrets: activity.secrets, + instance: activity.instance, + flags: activity.flags, + buttons: activity.buttons, + }; + }), + }, + }); + } + + async fetchUser(id: Snowflake): Promise { + const user = await this.rest.runMethod(this.rest, "GET", Routes.USER(id)); + + if (!user.id) return; + + return new User(this, user); + } + + createApplicationCommand(options: CreateApplicationCommand | CreateContextApplicationCommand, guildId?: Snowflake) { return this.rest.runMethod( this.rest, "POST", @@ -326,7 +402,15 @@ export class Session extends EventEmitter { ); } - fetchCommands() {} + fetchCommands(guildId?: Snowflake) { + return this.rest.runMethod( + this.rest, + "GET", + guildId + ? Routes.GUILD_APPLICATION_COMMANDS(this.applicationId, guildId) + : Routes.APPLICATION_COMMANDS(this.applicationId), + ); + } // deno-fmt-ignore isContextApplicationCommand(cmd: AtLeastOne | AtLeastOne): cmd is AtLeastOne {