1

我正在尝试在我的不和谐机器人中实现 js 代码。因此,我在网上获取了一些代码来尝试它是否正常工作,现在,我正在尝试像这样使用 queryselector:

if (msg.content === "!GuessNumber"){
    let randomNumber = Math.floor(Math.random() * 100) + 1;
      const guesses = document.querySelector('.guesses');
      const lastResult = document.querySelector('.lastResult');
      const lowOrHi = document.querySelector('.lowOrHi');
      const guessSubmit = document.querySelector('.guessSubmit');
      const guessField = document.querySelector('.guessField');
      let guessCount = 1;
      let resetButton;

但我有这个错误

ReferenceError: document is not defined
    at Client.<anonymous> (/Users/zep/Documents/GitHub/Bot-discord/my-bot/index.js:18:23)
    at Client.emit (events.js:311:20)
    at MessageCreateAction.handle (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:435:22)
    at WebSocketShard.onMessage (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:311:20)
    at Receiver.receiverOnMessage (/Users/zep/Documents/GitHub/Bot-discord/my-bot/node_modules/ws/lib/websocket.js:801:20)
[nodemon] app crashed - waiting for file changes before starting...

有人可以帮我调试一下并使用https://github.com/mdn/learning-area/blob/master/javascript/introduction-to-js-1/first-splash/number-guessing-game 中的代码。我的机器人的html ?我来试试它是如何工作的,谢谢

4

1 回答 1

0

好吧,你不能使用

      const guesses = document.querySelector('.guesses');
      const lastResult = document.querySelector('.lastResult');
      const lowOrHi = document.querySelector('.lowOrHi');
      const guessSubmit = document.querySelector('.guessSubmit');
      const guessField = document.querySelector('.guessField');

因为“文档”可以在支持 HTML 的 javascript 文件中使用。

您可以在https://discord.js.org上搜索它的替代品,因为它可能会有所帮助。我也会检查自己。

我实际上无法理解您到底想在那里做什么,您是想让用户(执行命令的人)猜测数字,还是希望机器人通过机器人猜测用户的数量.

于 2020-04-08T14:43:56.897 回答