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

npm:discord.js hangs #17391

Closed
ghaerdi opened this issue Jan 13, 2023 · 10 comments
Closed

npm:discord.js hangs #17391

ghaerdi opened this issue Jan 13, 2023 · 10 comments
Labels
bug Something isn't working correctly needs investigation requires further investigation before determining if it is an issue or not node compat

Comments

@ghaerdi
Copy link

ghaerdi commented Jan 13, 2023

Summary

I found a bug by trying to create a discord bot in Deno using the discord.js package from npm.
I ran the following code (replacing the empty string in the line 3):

import { Client, GatewayIntentBits, Events } from "npm:discord.js";

const TOKEN = "" // replace empty string with a discord bot's token;

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

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

client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === "ping") {
    await interaction.reply("Pong!");
  }
});

client.login(TOKEN);

I got the following error in repl.it:

error: Uncaught TypeError: Deno.createHttpClient is not a function
    return Deno.createHttpClient({ caCerts });
                ^
    at HttpsClientRequest._createCustomClient (https://deno.land/std@0.167.0/node/https.ts:84:17)
    at async HttpsClientRequest._final (https://deno.land/std@0.167.0/node/http.ts:136:20)

But if I run the same code in my machine it doesn't output any error but it also doesn't initialize the bot, it just freeze in the last line.

The code should initialize the bot by changing bot status to online in discord, output in console "Logged in as {bot name and tag}" and if someone write "/ping" on a discord server (where is the bot) the bot should response with "pong". In node it runs as I described.

Specs

Repl.it

Deno version: 1.28.3

My Machine

Deno version: 1.29.2
OS: Arch Linux on WSL

@bartlomieju
Copy link
Member

What is the command that you use to run this program?

@ghaerdi
Copy link
Author

ghaerdi commented Jan 13, 2023

What is the command that you use to run this program?

Here's my deno.jsonc file:

{
  "tasks": {
    "dev": "deno run --allow-read --allow-env --allow-net --watch main.ts",
    "start": "deno run --allow-read --allow-env --allow-net --no-check main.ts"
  }
}

I tried with both tasks deno task dev and deno task start, also with deno run -A main.ts and in repl.it I used the default command when pressing the Start button with and without the --no-check flag: deno run --lock=lock.json --lock-write --allow-all --no-check index.ts.
I ran the same repl.it command in my machine and the output is the same: no errors and the bot doesn't run.
@bartlomieju

@bartlomieju
Copy link
Member

Does the problem go away if you add --unstable flag? Deno.createHttpClient() is an unstable API and is not available without that flag.

@ghaerdi
Copy link
Author

ghaerdi commented Jan 13, 2023

Nothing happens, both in repl.it and in my machine, the returned promise from method client.login keeps in pending status.

@bartlomieju
Copy link
Member

Nothing happens, both in repl.it and in my machine, the returned promise from method client.login keeps in pending status.

But you don't get error about missing Deno.createHttpClient() anymore?

@ghaerdi
Copy link
Author

ghaerdi commented Jan 13, 2023

You're right, no errors. But the bot doesn't initialize, in node it does.

@bartlomieju bartlomieju changed the title [Bug] npm:discord.js - Deno.createHttpClient is not a function npm:discord.js hangs Jan 13, 2023
@bartlomieju bartlomieju added bug Something isn't working correctly needs investigation requires further investigation before determining if it is an issue or not node compat labels Jan 13, 2023
@zookatron
Copy link

@ghaerdi You want to listen for debug events to see more details about what's going wrong, see the example below:

import { Client, Events, GatewayIntentBits } from "npm:discord.js";

const TOKEN = ""; // replace empty string with a discord bot's token;

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

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

client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === "ping") {
    await interaction.reply("Pong!");
  }
});

client.on(Events.Debug, (message) => console.log("Debug:", message)); // Listen for debug messages

client.login(TOKEN);

@bartlomieju I investigated this issue and determined that this "hanging" is simply due to a persistent connection error that causes discord.js to retry the connection over and over forever. The connection error is due to the fact that the https deno compatibility shim does not properly support opening websocket connections, I've created an issue about this here: #17846

@evantill
Copy link

Thank you for the debug display

client.on(Events.Debug, (message) => console.log("Debug:", message)); // Listen for debug messages

I was fighting against this "hanging" without understanding what was going on.

@LiberaTeMetuMortis
Copy link

Is there any updates about it?

@bartlomieju
Copy link
Member

With #19412 merged this is now supported. Will be released in v1.34.3 later this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly needs investigation requires further investigation before determining if it is an issue or not node compat
Projects
None yet
Development

No branches or pull requests

5 participants