【问题标题】:How do you solve this mineflayer problem?你如何解决这个地雷问题?
【发布时间】:2020-11-30 11:05:27
【问题描述】:

所以我一直在尝试为我的世界中的机器人编写代码,但是在执行命令时:

node index.js

然后发生这种情况:

 internal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'C:\Users\monst\Downloads\Serverchat\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

这是代码:

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

let prefix = ".";
let bot = mineflayer.createBot({
    version: "1.12.2",
    host: "example.org",
    username: "email",
    password: "password",
})

client.on("ready", async => {
    console.log("Bot Online")
})

bot.on("login", async => {
    console.log("Ingame Bot Online")
    bot.chat("Online!")
})

bot.on("message", message => {
    let channel = client.channels.cache.get(742454971450261557)
    if (!channel) return; 
    channel.send(`${message}`)
})

client.login("token")

如果有人能帮忙,那对我来说就意味着整个世界!

【问题讨论】:

  • A) 这个文件叫什么? B) 你是如何运行它的?
  • 还值得注意的是,像742454971450261557 这样的数字太大而无法正常表示,并且可能会被四舍五入。你需要像742454971450261557n一样使用BigInt
  • @tadman 这个文件叫做 index.js。
  • @tadman 哦,这些大数字被用作不和谐频道的 id。如果它没有被使用,并且如果我在最后放置一个 n,它就不起作用。
  • @TobiasNielsen Discord.js 期望您将 ID 作为字符串而不是数字提供,这正是 tadman 上面解释的原因。

标签: node.js discord.js


【解决方案1】:

第一件事:

重置您的令牌!

您的机器人令牌就像用户名和密码合二为一。任何获得它的人都可以采取任何行动,就好像他们是你的机器人一样,包括通过它交谈并删除频道和类似的东西。我通过编辑从您的问题中对其进行了编辑,但我强烈建议您立即访问 Discord 门户并重置它。


除此之外,主要问题:

错误写得很清楚。 Error: Cannot find module 'C:\Users\monst\Downloads\Serverchat\index.js'。这意味着您在该文件夹中没有名为 index.js 的文件。看到它也是一个不和谐的机器人,也许你的意思是node bot.js?如果不是,您的代码可能位于另一个工作目录中。在命令提示符下使用cd 将您的工作目录更改为您放置机器人代码的位置和node_modules


另一件事:JavaScript 不能将雪花表示为原始数字 - 精度损失会导致最后几个数字略有变化。因此,discord 库(包括 discord.js)要求您将 ID 作为字符串传递,如下所示:

let channel = client.channels.cache.get("742454971450261557")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2011-06-07
    相关资源
    最近更新 更多