mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
refactor(.github,README,gateway,plugins/fileloader,tests,types)!: make intent calculation manual
This changes the calculation of intents to be manual to the dev. This is to improve overall consistency of our code base, also it is not a big drawback for users since intents are usually done once and then never (seldom) touched again.
This commit is contained in:
@@ -22,9 +22,7 @@ const bot = createBot({
|
|||||||
events: {
|
events: {
|
||||||
// ADD EVENTS NEEDED TO SHOW THE BUG HERE
|
// ADD EVENTS NEEDED TO SHOW THE BUG HERE
|
||||||
},
|
},
|
||||||
intents: [
|
intents: 0, // ADD INTENTS NEEDED HERE FOR YOUR TEST IF NECESSARY
|
||||||
// ADD INTENTS NEEDED HERE FOR YOUR TEST
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await startBot(bot);
|
await startBot(bot);
|
||||||
|
|||||||
@@ -100,12 +100,12 @@ Have your cache setup in any way you like. Redis, PGSQL or any cache layer you w
|
|||||||
Here is a minimal example to get started with:
|
Here is a minimal example to get started with:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { createBot, startBot } from "https://deno.land/x/discordeno@13.0.0-rc18/mod.ts";
|
import { createBot, Intents, startBot } from "https://deno.land/x/discordeno@13.0.0-rc18/mod.ts";
|
||||||
import { enableCachePlugin, enableCacheSweepers } from "https://deno.land/x/discordeno_cache_plugin@0.0.18/mod.ts";
|
import { enableCachePlugin, enableCacheSweepers } from "https://deno.land/x/discordeno_cache_plugin@0.0.18/mod.ts";
|
||||||
|
|
||||||
const baseBot = createBot({
|
const baseBot = createBot({
|
||||||
token: Deno.env.get("DISCORD_TOKEN"),
|
token: Deno.env.get("DISCORD_TOKEN"),
|
||||||
intents: ["Guilds", "GuildMessages"],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
botId: Deno.env.get("BOT_ID"),
|
botId: Deno.env.get("BOT_ID"),
|
||||||
events: {
|
events: {
|
||||||
ready() {
|
ready() {
|
||||||
|
|||||||
@@ -145,10 +145,7 @@ export function createBot(options: CreateBotOptions): Bot {
|
|||||||
applicationId: options.applicationId || options.botId,
|
applicationId: options.applicationId || options.botId,
|
||||||
token: removeTokenPrefix(options.token),
|
token: removeTokenPrefix(options.token),
|
||||||
events: createEventHandlers(options.events ?? {}),
|
events: createEventHandlers(options.events ?? {}),
|
||||||
intents: (options.intents ?? []).reduce(
|
intents: options.intents,
|
||||||
(bits, next) => (bits |= GatewayIntents[next]),
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
botGatewayData: options.botGatewayData,
|
botGatewayData: options.botGatewayData,
|
||||||
activeGuildIds: new Set<bigint>(),
|
activeGuildIds: new Set<bigint>(),
|
||||||
constants: createBotConstants(),
|
constants: createBotConstants(),
|
||||||
@@ -318,7 +315,7 @@ export interface CreateBotOptions {
|
|||||||
applicationId?: bigint;
|
applicationId?: bigint;
|
||||||
secretKey?: string;
|
secretKey?: string;
|
||||||
events?: Partial<EventHandlers>;
|
events?: Partial<EventHandlers>;
|
||||||
intents?: (keyof typeof GatewayIntents)[];
|
intents?: GatewayIntents;
|
||||||
botGatewayData?: GetGatewayBot;
|
botGatewayData?: GetGatewayBot;
|
||||||
rest?: Omit<CreateRestManagerOptions, "token">;
|
rest?: Omit<CreateRestManagerOptions, "token">;
|
||||||
handleDiscordPayload?: GatewayManager["handleDiscordPayload"];
|
handleDiscordPayload?: GatewayManager["handleDiscordPayload"];
|
||||||
|
|||||||
+1
-1
@@ -57,7 +57,7 @@ startGateway({
|
|||||||
/** Whether or not to use compression for gateway payloads. */
|
/** Whether or not to use compression for gateway payloads. */
|
||||||
compress: true,
|
compress: true,
|
||||||
/** The intents you would like to enable. */
|
/** The intents you would like to enable. */
|
||||||
intents: ["GUILDS", "GUILD_MESSAGES"],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
/** The max amount of shards used for identifying. This can be useful for zero-downtime updates or resharding. */
|
/** The max amount of shards used for identifying. This can be useful for zero-downtime updates or resharding. */
|
||||||
maxShards: 885,
|
maxShards: 885,
|
||||||
/** The first shard Id for this group of shards. */
|
/** The first shard Id for this group of shards. */
|
||||||
|
|||||||
@@ -59,10 +59,7 @@ export function createGatewayManager(
|
|||||||
$os: options.$os ?? "linux",
|
$os: options.$os ?? "linux",
|
||||||
$browser: options.$browser ?? "Discordeno",
|
$browser: options.$browser ?? "Discordeno",
|
||||||
$device: options.$device ?? "Discordeno",
|
$device: options.$device ?? "Discordeno",
|
||||||
intents:
|
intents: options.intents ?? 0,
|
||||||
(Array.isArray(options.intents)
|
|
||||||
? options.intents.reduce((bits, next) => (bits |= GatewayIntents[next]), 0)
|
|
||||||
: options.intents) ?? 0,
|
|
||||||
shard: options.shard ?? [0, options.shardsRecommended ?? 1],
|
shard: options.shard ?? [0, options.shardsRecommended ?? 1],
|
||||||
presence: options.presence,
|
presence: options.presence,
|
||||||
urlWSS: options.urlWSS ?? "wss://gateway.discord.gg/?v=9&encoding=json",
|
urlWSS: options.urlWSS ?? "wss://gateway.discord.gg/?v=9&encoding=json",
|
||||||
@@ -131,7 +128,7 @@ export interface GatewayManager {
|
|||||||
$os: string;
|
$os: string;
|
||||||
$browser: string;
|
$browser: string;
|
||||||
$device: string;
|
$device: string;
|
||||||
intents: number | (keyof typeof GatewayIntents)[];
|
intents: GatewayIntents;
|
||||||
shard: [number, number];
|
shard: [number, number];
|
||||||
presence?: Omit<StatusUpdate, "afk" | "since">;
|
presence?: Omit<StatusUpdate, "afk" | "since">;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ This plugin leverages the ability to write files, and then import them.
|
|||||||
## Code Example
|
## Code Example
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { createBot, enableFileLoaderPlugin, startBot } from "./deps.ts"; // Import discordeno and this plugin.
|
import { createBot, enableFileLoaderPlugin, Intents, startBot } from "./deps.ts"; // Import discordeno and this plugin.
|
||||||
|
|
||||||
console.log("Starting Up the Bot, this might take awhile...");
|
console.log("Starting Up the Bot, this might take awhile...");
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ const bot = enableFileLoaderPlugin(
|
|||||||
createBot({
|
createBot({
|
||||||
token: "", // Your bot's token
|
token: "", // Your bot's token
|
||||||
botId: 0n, // Your bot's "Application Id",
|
botId: 0n, // Your bot's "Application Id",
|
||||||
intents: [],
|
intents: Intents.Guilds,
|
||||||
events: {
|
events: {
|
||||||
ready() {
|
ready() {
|
||||||
console.log("Bot Ready");
|
console.log("Bot Ready");
|
||||||
|
|||||||
@@ -25,14 +25,14 @@ Create a file path like `src/bot/mod.ts`.
|
|||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { DISCORD_TOKEN } from "../../configs.ts";
|
import { DISCORD_TOKEN } from "../../configs.ts";
|
||||||
import { Collection, createBot } from "../../deps.ts";
|
import { Collection, createBot, Intents } from "../../deps.ts";
|
||||||
import { psql } from "./cache/mod.ts";
|
import { psql } from "./cache/mod.ts";
|
||||||
|
|
||||||
export const bot = createBot({
|
export const bot = createBot({
|
||||||
token: DISCORD_TOKEN,
|
token: DISCORD_TOKEN,
|
||||||
botId: 270010330782892032n,
|
botId: 270010330782892032n,
|
||||||
// applicationId: 270010330782892032,
|
// applicationId: 270010330782892032,
|
||||||
intents: ["Guilds", "GuildMessages"],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
events: {
|
events: {
|
||||||
messageCreate: function (bot, message) {
|
messageCreate: function (bot, message) {
|
||||||
console.log("message arrived");
|
console.log("message arrived");
|
||||||
@@ -126,7 +126,8 @@ Alright that was a lot of code. Now let's break it down little by little.
|
|||||||
developers have mentioned that this behavior is not documented and not supposed to be relied on to remain stable. Due
|
developers have mentioned that this behavior is not documented and not supposed to be relied on to remain stable. Due
|
||||||
to these reasons, we chose to just require the bot id be passed here.
|
to these reasons, we chose to just require the bot id be passed here.
|
||||||
- `applicationId` is an optional choice if your bot is old and has a unique id different from it's bot id.
|
- `applicationId` is an optional choice if your bot is old and has a unique id different from it's bot id.
|
||||||
- `intents`: Provide the intents you like using strings or a number. String form supports autocomplete and type safety.
|
- `intents`: Provide the intents you like using a bitwise OR operation (eg. `Intents.Guilds | Intents.GuildsMessages`).
|
||||||
|
String form supports autocomplete and type safety.
|
||||||
- `events`: These are your event handler functions. When a MESSAGE_CREATE event arrives from Discord it will be
|
- `events`: These are your event handler functions. When a MESSAGE_CREATE event arrives from Discord it will be
|
||||||
processed here. We will set up the routing to run these functions later in the guide but for now you can see how to
|
processed here. We will set up the routing to run these functions later in the guide but for now you can see how to
|
||||||
set it up. Note, you can create these functions in separate files and just import them here as you wish.
|
set it up. Note, you can create these functions in separate files and just import them here as you wish.
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ With this info, we can now create our gateway manager.
|
|||||||
const gateway = createGatewayManager({
|
const gateway = createGatewayManager({
|
||||||
secretKey: EVENT_HANDLER_SECRET_KEY,
|
secretKey: EVENT_HANDLER_SECRET_KEY,
|
||||||
token: DISCORD_TOKEN,
|
token: DISCORD_TOKEN,
|
||||||
intents: ["GuildMessages", "Guilds"],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
shardsRecommended: result.shards,
|
shardsRecommended: result.shards,
|
||||||
sessionStartLimitTotal: result.sessionStartLimit.total,
|
sessionStartLimitTotal: result.sessionStartLimit.total,
|
||||||
sessionStartLimitRemaining: result.sessionStartLimit.remaining,
|
sessionStartLimitRemaining: result.sessionStartLimit.remaining,
|
||||||
|
|||||||
@@ -45,11 +45,11 @@ Starting with Discordeno is very simple, you can start from scratch without any
|
|||||||
of code into a new TypeScript file:
|
of code into a new TypeScript file:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { startBot } from "https://deno.land/x/discordeno/mod.ts";
|
import { Intents, startBot } from "https://deno.land/x/discordeno/mod.ts";
|
||||||
|
|
||||||
startBot({
|
startBot({
|
||||||
token: "BOT TOKEN",
|
token: "BOT TOKEN",
|
||||||
intents: ["GUILDS", "GUILD_MESSAGES"],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
eventHandlers: {
|
eventHandlers: {
|
||||||
ready() {
|
ready() {
|
||||||
console.log("Successfully connected to gateway");
|
console.log("Successfully connected to gateway");
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ startBot({
|
|||||||
token: configs.token,
|
token: configs.token,
|
||||||
// Pick the intents you wish to have for your bot.
|
// Pick the intents you wish to have for your bot.
|
||||||
// For instance, to work with guild message reactions, you will have to pass the Intents.GUILD_MESSAGE_REACTIONS intent to the array.
|
// For instance, to work with guild message reactions, you will have to pass the Intents.GUILD_MESSAGE_REACTIONS intent to the array.
|
||||||
intents: [Intents.GUILDS, Intents.GUILD_MESSAGES],
|
intents: Intents.Guilds | Intents.GuildMessages,
|
||||||
// These are all your event handler functions. Imported from the events folder
|
// These are all your event handler functions. Imported from the events folder
|
||||||
eventHandlers: botCache.eventHandlers,
|
eventHandlers: botCache.eventHandlers,
|
||||||
});
|
});
|
||||||
|
|||||||
+10
-11
@@ -26,6 +26,7 @@ import { categoryChildrenTest } from "./helpers/channels/categoryChannels.ts";
|
|||||||
import { deleteChannelOverwriteTests } from "./helpers/channels/deleteChannelOverwrite.ts";
|
import { deleteChannelOverwriteTests } from "./helpers/channels/deleteChannelOverwrite.ts";
|
||||||
import { editChannelTests } from "./helpers/channels/editChannel.ts";
|
import { editChannelTests } from "./helpers/channels/editChannel.ts";
|
||||||
import { CACHED_COMMUNITY_GUILD_ID, sanitizeMode } from "./constants.ts";
|
import { CACHED_COMMUNITY_GUILD_ID, sanitizeMode } from "./constants.ts";
|
||||||
|
import { Intents } from "../types/shared.ts";
|
||||||
|
|
||||||
console.log("[Tests] Starting test preparation");
|
console.log("[Tests] Starting test preparation");
|
||||||
dotenv({ export: true, path: `${Deno.cwd()}/.env` });
|
dotenv({ export: true, path: `${Deno.cwd()}/.env` });
|
||||||
@@ -45,17 +46,15 @@ const baseBot = createBot({
|
|||||||
},
|
},
|
||||||
// debug: console.log,
|
// debug: console.log,
|
||||||
}),
|
}),
|
||||||
intents: [
|
intents: Intents.Guilds |
|
||||||
"Guilds",
|
Intents.GuildEmojis |
|
||||||
"GuildEmojis",
|
Intents.GuildMessages |
|
||||||
"GuildMessages",
|
Intents.GuildMessageReactions |
|
||||||
"GuildMessageReactions",
|
Intents.GuildBans |
|
||||||
"GuildBans",
|
Intents.GuildMembers |
|
||||||
"GuildMembers",
|
Intents.GuildScheduledEvents |
|
||||||
"GuildScheduledEvents",
|
Intents.GuildVoiceStates |
|
||||||
"GuildVoiceStates",
|
Intents.GuildPresences,
|
||||||
"GuildPresences",
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const bot = enableCachePlugin(baseBot);
|
export const bot = enableCachePlugin(baseBot);
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ export function loadBot() {
|
|||||||
const botId = BigInt(atob(token.split(".")[0]));
|
const botId = BigInt(atob(token.split(".")[0]));
|
||||||
const bot = createBot({
|
const bot = createBot({
|
||||||
events: {},
|
events: {},
|
||||||
intents: [],
|
intents: 0,
|
||||||
botId,
|
botId,
|
||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1151,6 +1151,12 @@ export enum GatewayIntents {
|
|||||||
GuildScheduledEvents = (1 << 16),
|
GuildScheduledEvents = (1 << 16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ALIASES JUST FOR BETTER UX IN THIS CASE
|
||||||
|
|
||||||
|
/** https://discord.com/developers/docs/topics/gateway#list-of-intents */
|
||||||
|
export const Intents = GatewayIntents;
|
||||||
|
export type Intents = GatewayIntents;
|
||||||
|
|
||||||
/** https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionresponsetype */
|
/** https://discord.com/developers/docs/interactions/slash-commands#interaction-response-interactionresponsetype */
|
||||||
export enum InteractionResponseTypes {
|
export enum InteractionResponseTypes {
|
||||||
/** ACK a `Ping` */
|
/** ACK a `Ping` */
|
||||||
|
|||||||
Reference in New Issue
Block a user