feat: update bot status

This commit is contained in:
Yuzu 2022-07-14 07:37:27 -05:00
parent 200f503110
commit c22ea0a767
3 changed files with 93 additions and 2 deletions

View File

@ -1,8 +1,11 @@
/** /**
* Bun example * 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"; import { GatewayIntents, Session } from "@oasisjs/biscuit";
// if it didn't worked use:
// const { GatewayIntents, Session } = require("@oasisjs/biscuit");
const token = process.env.TOKEN; const token = process.env.TOKEN;
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;

View File

@ -3,6 +3,10 @@ import type { Snowflake } from "./Snowflake.ts";
// cdn endpoints // cdn endpoints
export * from "./Cdn.ts"; export * from "./Cdn.ts";
export function USER(userId: Snowflake) {
return `/users/${userId}`;
}
export function GATEWAY_BOT() { export function GATEWAY_BOT() {
return "/gateway/bot"; return "/gateway/bot";
} }

View File

@ -1,4 +1,5 @@
import type { import type {
StatusTypes,
ApplicationCommandPermissionTypes, ApplicationCommandPermissionTypes,
AtLeastOne, AtLeastOne,
DiscordApplicationCommand, DiscordApplicationCommand,
@ -8,11 +9,13 @@ import type {
GatewayBot, GatewayBot,
GatewayIntents, GatewayIntents,
Localization, Localization,
DiscordUser,
} from "../discordeno/mod.ts"; } from "../discordeno/mod.ts";
import type { DiscordGatewayPayload, Shard } from "../discordeno/mod.ts"; import type { DiscordGatewayPayload, Shard } from "../discordeno/mod.ts";
import type { Events } from "./Actions.ts"; import type { Events } from "./Actions.ts";
import type { PermissionResolvable } from "./structures/Permissions.ts"; import type { PermissionResolvable } from "./structures/Permissions.ts";
import type { Activities } from "./structures/Presence.ts";
import { Permissions } from "./structures/Permissions.ts"; import { Permissions } from "./structures/Permissions.ts";
import { Snowflake } from "./Snowflake.ts"; import { Snowflake } from "./Snowflake.ts";
@ -22,8 +25,12 @@ import {
createGatewayManager, createGatewayManager,
createRestManager, createRestManager,
getBotIdFromToken, getBotIdFromToken,
GatewayOpcodes,
StatusTypes
} from "../discordeno/mod.ts"; } from "../discordeno/mod.ts";
import User from "./structures/User.ts";
import * as Routes from "./Routes.ts"; import * as Routes from "./Routes.ts";
import * as Actions from "./Actions.ts"; import * as Actions from "./Actions.ts";
@ -102,6 +109,14 @@ export interface SessionOptions {
gateway?: GatewayOptions; gateway?: GatewayOptions;
} }
/**
* @link https://discord.com/developers/docs/topics/gateway#update-status
* */
export interface StatusUpdate {
activities: Activities[];
status: StatusTypes;
}
/** /**
* Receives a Token, connects * Receives a Token, connects
* Most of the command implementations were adapted from Discordeno (https://github.com/discordeno/discordeno) * 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); 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<User | undefined> {
const user = await this.rest.runMethod<DiscordUser>(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<DiscordApplicationCommand>( return this.rest.runMethod<DiscordApplicationCommand>(
this.rest, this.rest,
"POST", "POST",
@ -326,7 +402,15 @@ export class Session extends EventEmitter {
); );
} }
fetchCommands() {} fetchCommands(guildId?: Snowflake) {
return this.rest.runMethod<DiscordApplicationCommand[]>(
this.rest,
"GET",
guildId
? Routes.GUILD_APPLICATION_COMMANDS(this.applicationId, guildId)
: Routes.APPLICATION_COMMANDS(this.applicationId),
);
}
// deno-fmt-ignore // deno-fmt-ignore
isContextApplicationCommand(cmd: AtLeastOne<CreateContextApplicationCommand> | AtLeastOne<CreateApplicationCommand>): cmd is AtLeastOne<CreateContextApplicationCommand> { isContextApplicationCommand(cmd: AtLeastOne<CreateContextApplicationCommand> | AtLeastOne<CreateApplicationCommand>): cmd is AtLeastOne<CreateContextApplicationCommand> {