#Discord Bot Invite server.js and HTML throwing errors when trying to press the button

8 messages · Page 1 of 1 (latest)

odd canyon
#
// 
<!DOCTYPE html>
<html>
<head>
    <title>Invite My Discord Bot</title>
</head>
<body>
    <button onclick="inviteBot()">Invite My Bot</button>

    <script>
        function inviteBot() {
            const serverUrl = 'http://localhost:3000/invite-bot';

            // Make a request to your local server
            fetch(serverUrl)
                .then(response => {
                    if (response.ok) {
                        alert('Bot invited successfully!');
                    } else {
                        alert('Failed to invite bot. Please try again later.');
                    }
                })
                .catch(error => {
                    console.error('Error occurred:', error);
                    alert('An error occurred while inviting the bot.');
                });
        }
    </script>
</body>
</html>
const express = require('express');
const axios = require('axios')
const app = express();

const clientId = ''; 
const token = ''; 
const guildId = ''; 

// API endpoint for bot invitation
app.get('/invite-bot', async (req, res) => {
    try {
        const response = await axios.put(`https://discord.com/api/v9/guilds/${guildId}/members/${clientId}`, {
            access_token: token
        }, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bot ${token}`
            }
        });

        if (response.status === 201) {
            res.send('Bot invited successfully!');
        } else {
            throw new Error(`Failed to invite bot: ${response.statusText}`);
        }
    } catch (error) {
        console.error('Error occurred:', error);
        res.status(500).send('An error occurred while inviting the bot.');
    }
});

// Start the server
const port = 3000;
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});

Attached is the code for the Server and the Index it is local I am just trying to get it to join my server(guild) it is for a discord.js bot
ClientId, Token, and GuildID are removed for obvious reasons the axios error code that comes back is a long "403" Code I've been troubleshooting this for about 2-3 hours now and dont know what im doing wrong
Thank you

#

^ "alert('An error occurred while inviting the bot.');" Is the error code that shows up on the html page if that helps any

#

But pretty much the goal is for it to redirect me to the "Oauth" Page to invite the bot and it does not throw me there

#

Or is there an easier way to do this im just trying to invite the bot lmao but discord has made it so complicated than the past

solemn lynx
#

On the dashboard you can generate an invite link, just paste that in your URL bar and it'll take you to the screen to invite your bot

odd canyon
#

Not too skilled with the webserver part I wish discord would of just left it how it was about a year ago lmao