#How can I manage the client object for making calls?

76 messages · Page 1 of 1 (latest)

jagged hullBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
cunning marsh
#

Add a caching mechanism and ur pretty much set

#

In TS, I’d use TSyringe as a DI container

lean quest
#

Sorry I deleted this question because I realised I should be doing event driven programming on the express side to make calls to discordjs, but I was probably just overthinking it and I guess there is nothing inherently wrong with DI

cunning marsh
#

Helps to 🦆 sometimes

lean quest
#

I guess this is more of a design / js thing but

app.listen(PORT, async () => {
    let client = await openClient();
    
})
app.get('/receivedEvent', (req, res) => {
    client.sendMessage(req.body...);
})
#

is it valid to instantiate the client in the outer scope of these two functions?

#

i.e.

const client = await openClient();
app.listen(PORT, async () => {
    
})
app.get('/', (req, res) => {
    sendMessage(client, req.body...);
})
serene fog
#

That depends what that openClient function does exactly (and if you‘re using esm or commonjs)

lean quest
#

openClient is logging in and returning the client

const openClient = async () => {
    const { Client, Events, GatewayIntentBits } = require('discord.js');
    const { ECCE_BOT_TOKEN, CHANNEL_ID } = process.env;

    console.log(ECCE_BOT_TOKEN);

    const client = new Client({ intents: [
            GatewayIntentBits.Guilds,
            GatewayIntentBits.GuildMessages
        ] });

    client.once(Events.ClientReady, readyClient => {
        console.log(`Ready! Logged in as ${readyClient.user.tag}`);
    });

    // Log in to Discord with your client's token
    client.login(ECCE_BOT_TOKEN);
    return client;
}
#

and actually I can't have an await before the OpenClient call in this case which kind of breaks things

serene fog
#

Then you should be fine doing that outside. And why can’t you await? Do you not use esm?

#

But your openClient doesn‘t need to be async, does it?

lean quest
#

that's true, I think that will solve the problem of having it being called outside a function

#
const discordClient = openClient();
sendMessage(discordClient, "123");
const sendMessage = (client, msg) => {
    const channel = client.channels.cache.get(process.env.CHANNEL_ID).send("123");
}

TypeError: Cannot read properties of undefined (reading 'send')
I'm guessing that the client isn't resolving in time?

#

openClient isn't async anymore but I guess the login function is

serene fog
#

Yes, but even if you awaited that it wouldn’t mean the ready event emitted yet. Which is the thing you‘d really need to wait for.

lean quest
#

ah yes, so how can I ensure that event is emitted before I start doing things with the client in express?

cunning marsh
#

Should just use a webhook here instead of channel id tbh

serene fog
#

If all you need the client for is that channel send then yeah… or at least use @discordjs/rest if you don’t actually need the gateway connection

lean quest
#

my use case is that I'm gonna send use the instagram api to send events to my api which will then trigger a message send to a discord channel

cunning marsh
#

You can send messages via webhooks or the rest package if you don’t like webhook

#

D.js is more useful for the events and caching mechanism

lean quest
#

I think considering I might want to add more functionality in the future that maybe a webhook would only solve this problem

#

whereas a full application could handle all?

cunning marsh
#

Depends on the actual use cases

lean quest
#

one use case I can think of is that I think discordjs embeds can handle video, but webhook embeds cannot

cunning marsh
#

I’ve only used d.js with express when I’m running the bot on the same host as the site

cunning marsh
#

Except indirectly by sending links

lean quest
#

Yes using links is fine, but I don't think it's possible with webhook embeds

#

but pretty sure it is with discordjs embeds

cunning marsh
#

It is possible

#

You just need the everyone role permissions set properly

lean quest
#

Not sure I follow what you mean about permissions

#

webhooks have perms to send messages

#

and that's it right

#

at least native webhooks

cunning marsh
#

But it still inherits other permissions from the everyone role

#

Assuming it’s a user-created webhook

#

Namely, the use external emojis and embed links permissions on the everyone role still affects webhooks

lean quest
#

here is an example, if I include this link in a webhook embed.. the video is not embedded

#

but if I plain text it

cunning marsh
#

Aren’t you supposed to send it in the content?

lean quest
cunning marsh
#

Yea

#

The latter sends the link in the content, which is the correct way to do it

lean quest
#

what I was saying is, is it not possible to have an embedded video in the embedded part of the message?

cunning marsh
#

That’s correct

#

You can only send a link in the content that causes Discord to add a video embed

lean quest
#

okay okay that's fine

#

but back to the thing you're saying, webhook vs a full app

#

of course a webhook would solve this task, but I feel like it's better to encompass everything into one bot

cunning marsh
#

Webhooks are nice because you can move them to any channel in the guild without changing the code

lean quest
#

that's true and I am drawn towards it because of it's simplicity for this

cunning marsh
lean quest
#

yes, I'm hoping I'll be able to hit my expressjs server with the instagram api events and then trigger discord events

#

i.e. send a message

#

I can do that with webhooks but now that I've failed to get the bot to send a message as a full app I'm really wanting to know why/how

cunning marsh
#

Qjuh mentioned it earlier

lean quest
#

Yes the client isn't ready when I make the call to send a message to a channel

#

but considering I'm wanting the express events to determine when I send a message, how can I be sure the client event has emitted?

cunning marsh
#

Create a new promise and resolve once ready has emitted

#

Or you mean waiting for the message event?

lucid grottoBOT
#

guide Popular Topics: Collectors - Message collectors
read more

lean quest
#

I mean how can I ensure the discord client is ready before everything else, that it resolves so that messages can be sent

const discordClient = openClient();
app.get('/', (req, res) => {
    sendMessage(discordClient, req.body)
})
cunning marsh
#

Just create a new promise and resolve it once ready emits

#

I prefer to just initialize client before express to avoid this issue

lean quest
lucid grottoBOT
#

mdn Promise
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

lean quest
#

I know how to wait for a promise to resolve, but I don't know how to wait for some custom event, in this case Events.ClientReady

cunning marsh
lean quest
#

it does but I don't see how I can recognise in the express app when that event is emitted

#

like that event is separate to the promise resolving

cunning marsh
#

And then you can await the promise you created