OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to handle listeners in Cloud Function | Discord.js

  • Thread starter Thread starter yolo
  • Start date Start date
Y

yolo

Guest
I'm using Discord.js to create a bot with Firebase's Cloud Functions. Users access functions from the application client and operate the Discord Bot.

In the code below, discord_test is called and a client.on listener is added. This will return the response after client.login() is called.

Code:
exports.discord_test = functions.https.onCall(async (data, context) => {
    console.log("1");
    client.on("ready", async () => {
        console.log("3");
        const snap = await db.collection(colName).doc(docName).get().catch((e) => {
            console.error(e);
        });
        console.log("4");
    });

    console.log("2");
    await client.login(token.token);
});

As you can see, I have set up a log that outputs 1 to 4. It is assumed that numbers 1 to 4 are called in order.

Run this multiple times and the result will look like this:

1st time:​

Function execution started

Callable request verification passed

1

2

Function execution took 2220 ms, finished with status code: 200

3

4 wasn't called.

2nd time:​

Function execution started

Callable request verification passed

1

2

Function execution took 1689 ms, finished with status code: 200

3 and 4 weren't called.

The timing when I execute firebase deploy --only functions (About 10 minutes after the 2nd time)​

3rd time:​

Function execution started

Callable request verification passed

1

2

Function execution took 2100 ms, finished with status code: 200

No matter how long I wait after this, 3 and 4 will never be called.



From this result, we can see the function written in the second argument of client.on() will be suddenly blocked at some times and suddenly loaded at other times.

Of course, I understand that this is the listener, so there is nothing strange about the timing being off.

So, what I would like to ask is how much time the Cloud Function retains for this listener, and why the processing is interrupted midway through.

It's very strange that client.on() is loaded when I deploy.

<p>I'm using Discord.js to create a bot with Firebase's Cloud Functions. Users access functions from the application client and operate the Discord Bot.</p>
<p>In the code below, <code>discord_test</code> is called and a <code>client.on</code> listener is added. This will return the response after <code>client.login()</code> is called.</p>
<pre><code>exports.discord_test = functions.https.onCall(async (data, context) => {
console.log("1");
client.on("ready", async () => {
console.log("3");
const snap = await db.collection(colName).doc(docName).get().catch((e) => {
console.error(e);
});
console.log("4");
});

console.log("2");
await client.login(token.token);
});
</code></pre>
<p>As you can see, I have set up a log that outputs 1 to 4. It is assumed that numbers 1 to 4 are called in order.</p>
<p>Run this multiple times and the result will look like this:</p>
<h3>1st time:</h3>
<blockquote>
<p>Function execution started</p>
<p>Callable request verification passed</p>
<p>1</p>
<p>2</p>
<p>Function execution took 2220 ms, finished with status code: 200</p>
<p>3</p>
</blockquote>
<p><code>4</code> wasn't called.</p>
<h3>2nd time:</h3>
<blockquote>
<p>Function execution started</p>
<p>Callable request verification passed</p>
<p>1</p>
<p>2</p>
<p>Function execution took 1689 ms, finished with status code: 200</p>
</blockquote>
<p><code>3</code> and <code>4</code> weren't called.</p>
<h3>The timing when I execute <code>firebase deploy --only functions</code> (About 10 minutes after the 2nd time)</h3>
<blockquote>
<p>3</p>
<p>4</p>
</blockquote>
<h3>3rd time:</h3>
<blockquote>
<p>Function execution started</p>
<p>Callable request verification passed</p>
<p>1</p>
<p>2</p>
<p>Function execution took 2100 ms, finished with status code: 200</p>
</blockquote>
<p>No matter how long I wait after this, <code>3</code> and <code>4</code> will never be called.</p>
<hr />
<p>From this result, we can see the function written in the second argument of <code>client.on()</code> will be suddenly blocked at some times and suddenly loaded at other times.</p>
<p>Of course, I understand that this is the listener, so there is nothing strange about the timing being off.</p>
<p>So, what I would like to ask is how much time the Cloud Function retains for this listener, and why the processing is interrupted midway through.</p>
<p>It's very strange that <code>client.on()</code> is loaded when I deploy.</p>
 

Latest posts

A
Replies
0
Views
1
Aarif Hussain A Nassar
A
F
Replies
0
Views
1
Fahmi Nur Fachrurozi
F
Top