Clearing up confusion around Importing node_modules

Hi there, I believe I need some help around importing node_modules, and I thought maybe the community could help shed some light on what seems to be fairly puzzling.

I am attempting to build a discord bot and would like to use ReScript. However, In the first few moments of trying to use ReScript I found myself stumped when I tried to import a library from the node_modules folder. Please keep in mind that this question may seem ridiculous-but it probably has to do with the fact that I am new to JS and do not use React.
The library in question is called “discord.js”. Note that this contains a period in the name.
I looked at the documentation for Importing & Exporting, but it says that it doesn’t need import statements and that you simply would write something like Student.message. That’s fine but discord.js as a name contains that period, which essentially led me down what felt like a pretty overwhelming rabbit hole, when all I wanted to type was const { Client, Collection, GatewayIntentBits} = require("discord.js")

Is there an easy way to do this? I currently have the following lines

@module("discord.js") external client: Js.Dict.t<string> = "Client"
@module("discord.js") external collection: Js.Dict.t<string> = "Collection"
@module("discord.js") external gatewayIntentBits: Js.Dict.t<string> = "GatewayIntentBits"

But I am pretty sure this isn’t what I want either. So is there any documentation that I should be looking at to help me use the modules from this library that I need?

I know I am way to far out of my depth now as the next two lines I would like to write are

let cur_client = client({ intents: gatewayIntentBits.Guilds });
cur_client.commands = new collection();

And I have no idea how

1 Like

Hi @Jerome , The following bindings might help you.

Playground link also shows the generated javascript. The Syntax Lookup will be helpful to quickly lookup what these annotations are.

module Discord = {
  type guilds
  type client
  type clientOptions = {intents: guilds}
  type gatewayIntentBits = {@as("Guilds") guilds: guilds}
  type collection

  @module("discord.js") external client: clientOptions => client = "Client"

  @new @module("discord.js")
  external createCollection: unit => collection = "Collection"

  @module("discord.js")
  external gatewayIntentBits: gatewayIntentBits = "GatewayIntentBits"

  @set external setCommands: (client, collection) => unit = "commands"
}

let discordClient = Discord.client({
  intents: Discord.gatewayIntentBits.guilds,
})

discordClient->Discord.setCommands(Discord.createCollection())

2 Likes

Thank you for your help. I believe I understand this now a lot better, much appreciated.

I have a discord bot running in prod written in 100% rescript that might help to look at.

I extracted a lot of the bindings I used into an npm package. It is in no way finished, but it may help you along the way or write your own bindings.

1 Like