Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add @discordjs/core #8736

Merged
merged 58 commits into from
Nov 27, 2022
Merged

feat: Add @discordjs/core #8736

merged 58 commits into from
Nov 27, 2022

Conversation

suneettipirneni
Copy link
Member

@suneettipirneni suneettipirneni commented Oct 11, 2022

A new library to interact with the discord API at a lower level.

@discordjs/core is a thinly abstracted wrapper on top of the discord api. It avoids much of the pre-processing a fully-fledged library such as discord.js performs. With /core events give the raw data directly from gateway events. It also includes a rest wrapper which takes raw data and performs queries. In other words the rest client and gateway logic are decoupled from the actual returned structures giving users maximum flexibility to make their own modifications.

TODO:

  • Implement files/form data uploading
  • Add automod API

example usage:

import { REST } from '@discordjs/rest';
import { WebSocketManager } from '@discordjs/ws';
import { Gateway, GatewayDispatchEvents, GatewayIntentBits, InteractionType, MessageFlags, createClient } from '@discordjs/core';

// Create REST and web socket managers directly
const rest = new REST({ version: '10' }).setToken(token);
const ws = new WebSocketManager({
  token,
  intents: GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent,
  rest,
});

// Create a client to emit relevant events.
const client = createClient({ rest, ws });

// Listen for interactions
// Each event contains an `api` prop along with the event data that allows you to interface with the discord REST API
client.on(GatewayDispatchEvents.InteractionCreate, async ({ data: interaction, api }) => {
  if (
    !(interaction.type === InteractionType.ApplicationCommand) ||
    interaction.data.name !== 'ping'
  ) {
    return;
  }

  api.interactions.reply(interaction.id, interaction.token, { content: 'Pong!', flags: MessageFlags.Ephemeral });
});

// Listen for the ready event
client.on(GatewayDispatchEvents.Ready, () => console.log('Ready!'));

// Start the ws connection.
ws.connect();

Similar to the raw ws events, you can also keep track of shards as each event includes a shardId prop:

client.on(GatewayDispatchEvents.InteractionCreate, async ({ data: interaction, api, shardId }) => {
  ...
  api.interactions.reply(interaction.id, interaction.token, `Pong! from shard ${shardId}`);
});

Independent REST API usage

This library has REST API functions that be used without a live connection to the gateway:

// Create rest instance
const rest = new REST({ version: '10' }).setToken(token);

// Pass into api
const api = new API(rest);

// Fetch a guild using the api wrapper
const guild = await api.guilds.get('1234567891011');

Suggestions and feedback on the current API design are welcomed.

@vercel
Copy link

vercel bot commented Oct 11, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
discord-js ✅ Ready (Inspect) Visit Preview Nov 27, 2022 at 9:20PM (UTC)
1 Ignored Deployment
Name Status Preview Updated
discord-js-guide ⬜️ Ignored (Inspect) Nov 27, 2022 at 9:20PM (UTC)

@codecov
Copy link

codecov bot commented Oct 11, 2022

Codecov Report

Merging #8736 (3789b4a) into main (ec37f13) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #8736   +/-   ##
=======================================
  Coverage   85.64%   85.64%           
=======================================
  Files          96       96           
  Lines        9483     9483           
  Branches     1137     1137           
=======================================
  Hits         8122     8122           
  Misses       1319     1319           
  Partials       42       42           
Flag Coverage Δ
brokers 65.24% <ø> (ø)
builders 98.65% <ø> (ø)
collection 100.00% <ø> (ø)
proxy 81.53% <ø> (ø)
rest 91.97% <100.00%> (ø)
util 100.00% <ø> (ø)
utilities 100.00% <ø> (ø)
voice 63.70% <ø> (ø)
ws 59.83% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/rest/src/lib/RequestManager.ts 90.26% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@KhafraDev
Copy link
Contributor

seems like a mix between d.js v11 and eris

packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/thread.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
@vercel
Copy link

vercel bot commented Oct 13, 2022

Deployment failed with the following error:

Creating the Deployment Timed Out.

packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/package.json Outdated Show resolved Hide resolved
packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/emitter.ts Outdated Show resolved Hide resolved
packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/api/bot.ts Outdated Show resolved Hide resolved
packages/core/src/api/interactions.ts Outdated Show resolved Hide resolved
packages/core/src/api/interactions.ts Outdated Show resolved Hide resolved
packages/core/src/api/message.ts Outdated Show resolved Hide resolved
packages/core/src/api/thread.ts Outdated Show resolved Hide resolved
packages/core/src/api/thread.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/webhook.ts Outdated Show resolved Hide resolved
packages/core/src/api/message.ts Outdated Show resolved Hide resolved
packages/core/src/api/interactions.ts Outdated Show resolved Hide resolved
packages/core/src/api/index.ts Outdated Show resolved Hide resolved
packages/core/src/api/message.ts Outdated Show resolved Hide resolved
packages/core/src/emitter.ts Outdated Show resolved Hide resolved
packages/core/src/emitter.ts Outdated Show resolved Hide resolved
packages/core/src/emitter.ts Outdated Show resolved Hide resolved
packages/core/src/api/message.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/channel.ts Outdated Show resolved Hide resolved
Copy link
Member

@kyranet kyranet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last things before approval.

packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
Co-authored-by: Aura Román <kyradiscord@gmail.com>
@kyranet kyranet removed the blocked label Nov 26, 2022
packages/core/README.md Outdated Show resolved Hide resolved
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
packages/core/src/client.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/guild.ts Outdated Show resolved Hide resolved
packages/core/src/api/interactions.ts Outdated Show resolved Hide resolved
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
@dylhack
Copy link

dylhack commented Nov 27, 2022

Awesome! Is there an ETA when it will be published to NPM?

@iCrawl
Copy link
Member

iCrawl commented Nov 28, 2022

If everything goes well today (28.11.2022) or tomorrow (29.11.2022)

@suneettipirneni
Copy link
Member Author

It's published now: https://www.npmjs.com/package/@discordjs/core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.