简体   繁体   中英

document.querySelector in a discord.js bot

I want to connect a local site with a discord.js bot so if the thing I want to do works put it online in a server.

I want to sent in a discord server channel the value of an input.

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>

Error:

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

ReferenceError: document is not defined

Add require.js to your project from this link require.js

Then load discord.js and your index.js as following

 <,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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM