【问题标题】:document.querySelector in a discord.js botdiscord.js 机器人中的 document.querySelector
【发布时间】:2022-01-26 13:06:48
【问题描述】:

我想用 discord.js 机器人连接本地站点 因此,如果我想做的事情有效,请将其在线放在服务器中。

我想在不和谐的服务器通道中发送输入的值。

js: src/bot/index.js

const { Client, Intents } = require('discord.js');
const { TOKEN } = require('./config.json');

channelid = "925049593056596009"

const client = new Client({ intents: 32767});


client.once('ready', () => {
    console.log('Ready!');
});

function test() {
    document.querySelector('.button').addEventListener('click', () => {
        channelid.send(document.querySelector('.input').value)
    })
}
test()
client.login(TOKEN);

html: src/html/application.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Applications</title>
</head>
<body>
    <script src="../bot/index.js"></script>

    <form>
        <input class="input" type="text" placeholder="say something">
        <button class="button">OK</button>
    </form>
</body>
</html>

错误:

   document.querySelector('.button').addEventListener('click', () => {
    ^

ReferenceError: document is not defined

【问题讨论】:

  • 我们可以查看您的文件吗?
  • @Leau 我是新来堆叠我如何发送图像
  • 请勿发送图片,请在代码块中附上您的代码(带文件名)。
  • 好的,我会编辑问题
  • 你不能像 Node.js 模块那样导入 Discord.js,在你的 html 文件中导入这些 webpacks github.com/discordjs/discord.js/tree/webpack 之一。

标签: javascript node.js discord.js


【解决方案1】:

从此链接将 require.js 添加到您的项目中 require.js

然后加载 discord.js 和你的 index.js 如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Applications</title>
</head>
<body>
    <script data-main="./src/discord.js" src="./src/require.js"></script>
    <script data-main="./src/index.js" src="./src/require.js"></script>

    <form>
        <input class="input" type="text" placeholder="say something">
        <button class="button">OK</button>
    </form>
</body>
</html>

【讨论】:

猜你喜欢
  • 2021-07-01
  • 2018-01-29
  • 2020-08-21
  • 2022-01-07
  • 2020-10-03
  • 2022-06-25
  • 2022-12-23
  • 2018-06-05
  • 2017-10-31
相关资源
最近更新 更多