js查询选择器



我正试图在我的discord bot中实现一个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为我的机器人?我想试试它的工作原理,谢谢

好吧,你不能使用

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');

因为"Document"可以在HTML支持的javascript文件中使用。

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

事实上,我无法理解你到底想在那里做什么,你想让用户(执行命令的人(猜测数字,还是想让机器人通过机器人猜测用户的数字。

最新更新