- What's your exact discord.js
npm list discord.jsand nodenode -vversion? - 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!
#How can I manage the client object for making calls?
76 messages · Page 1 of 1 (latest)
Add a caching mechanism and ur pretty much set
In TS, I’d use TSyringe as a DI container
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
Helps to 🦆 sometimes
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...);
})
That depends what that openClient function does exactly (and if you‘re using esm or commonjs)
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
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?
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
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.
ah yes, so how can I ensure that event is emitted before I start doing things with the client in express?
Should just use a webhook here instead of channel id tbh
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
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
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
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?
Depends on the actual use cases
one use case I can think of is that I think discordjs embeds can handle video, but webhook embeds cannot
I’ve only used d.js with express when I’m running the bot on the same host as the site
You can’t send video embeds
Except indirectly by sending links
Yes using links is fine, but I don't think it's possible with webhook embeds
but pretty sure it is with discordjs embeds
Not sure I follow what you mean about permissions
webhooks have perms to send messages
and that's it right
at least native webhooks
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
here is an example, if I include this link in a webhook embed.. the video is not embedded
but if I plain text it
Aren’t you supposed to send it in the content?
what I was saying is, is it not possible to have an embedded video in the embedded part of the message?
That’s correct
You can only send a link in the content that causes Discord to add a video embed
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
Webhooks are nice because you can move them to any channel in the guild without changing the code
that's true and I am drawn towards it because of it's simplicity for this
Are you running the bot alongside the webserver?
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
Qjuh mentioned it earlier
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?
Create a new promise and resolve once ready has emitted
Or you mean waiting for the message event?
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)
})
Just create a new promise and resolve it once ready emits
I prefer to just initialize client before express to avoid this issue
can you show me an example of how you would do this
Promise
The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
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
The doc tells you how to use the Promise constructor
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
You have to turn the event emitting into a Promise
And then you can await the promise you created
Popular Topics: Collectors - Message collectors