使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS

Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
本文介绍了使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我刚刚在 Replit 上启动了 Discord.JS.它遵循 FreeCodeCamp 关于 https://www 的教程.freecodecamp.org/news/create-a-discord-bot-with-javascript-nodejs/

I have just started Discord.JS on Replit. It followed FreeCodeCamp's Tutorial on https://www.freecodecamp.org/news/create-a-discord-bot-with-javascript-nodejs/

但是当我运行代码时它告诉我运行:

But when I runned the code it telled me to run :

const Discord = require("discord.js")
const client = new Discord.Client()

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

client.on("message", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
})

client.login(process.env.TOKEN)

它告诉这个错误:

Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discor
Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/index.js
- /home/runner/Omega-Flowey-JS-Test/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js:3:22)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19

谁能告诉我什么错误?我是 Discord.JS 的新手,所以我不知道有什么错误.

Can someone tell me whats error? I'm very new at Discord.JS so I don't know anything that is error.

推荐答案

Discord.js v13 需要 Node.js v16.6.Replit 仅直接支持 Node.js v12.16.1(在撰写本文时).

Discord.js v13 requires Node.js v16.6. Replit only directly supports Node.js v12.16.1 (at the time of writing).

使用旧版本的 Node.js 时,您可能会看到诸如

When using an older version of Node.js, you may see errors such as

  • 错误:找不到模块'node:events'
  • ReferenceError: AbortController 未定义
  • SyntaxError: Unexpected token '?'

但是,您可以使用 node npm 包安装更新的 Node.js 二进制文件.

However, you can install a newer Node.js binary using the node npm package.

这是一个最小设置:

package.json

{
  "name": "node.js-v16-on-replit-demo",
  "version": "0.0.1",
  "dependencies": {
    "discord.js": "^13.2.0",
    "node": "^16.10.0"
  }
}

.replit

run = "npx node ."

index.js

const Discord = require("discord.js")

// your code...

你可以试试这个demo repl.

如果你喜欢,你可以定义一个 start npm 脚本:

If you like, you can define a start npm script:

package.json

{
  // ...
  "scripts": {
    "start": "node ."
  }
}

.replit

run = 'npm start'

还有 网上有大量关于 Node 的资源.js v16 在 Replit 上.

There are also plenty of resources online about Node.js v16 on Replit.

这篇关于使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)