with endpoint

mail@pastecode.io avatar
unknown
javascript
11 days ago
2.4 kB
1
Indexable
Never
const { Client, GatewayIntentBits } = require('discord.js');
const express = require('express');  // Import Express
const bodyParser = require('body-parser');
const axios = require('axios');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildMembers
  ]
});

// Discord bot variables
const GUILD_ID = '705123157438234734';
const ROLE_PARTNER = '705129728234881086';
const ROLE_AFFILIATE = '705130154904518756';
const ROLE_CREATOR = '705225579959418931';

// Set up Express app
const app = express();
app.use(bodyParser.json());  // Middleware to parse JSON

// API endpoint for role assignment
app.post('/assign_role', async (req, res) => {
  const { broadcaster_type, discord_user_id } = req.body;

  console.log(`Received request: ${JSON.stringify(req.body)}`); // Debugging output

  try {
    const guild = client.guilds.cache.get(GUILD_ID);
    if (!guild) {
      return res.status(404).json({ status: 'error', message: 'Guild not found' });
    }

    const member = await guild.members.fetch(discord_user_id);
    if (!member) {
      return res.status(404).json({ status: 'error', message: 'Member not found' });
    }

    let roleToAssign;
    if (broadcaster_type === 'partner') {
      roleToAssign = guild.roles.cache.get(ROLE_PARTNER);
    } else if (broadcaster_type === 'affiliate') {
      roleToAssign = guild.roles.cache.get(ROLE_AFFILIATE);
    } else {
      roleToAssign = guild.roles.cache.get(ROLE_CREATOR);
    }

    if (!roleToAssign) {
      return res.status(404).json({ status: 'error', message: 'Role not found' });
    }

    if (roleToAssign.position >= guild.me.roles.highest.position) {
      return res.status(403).json({ status: 'error', message: 'Bot cannot assign this role' });
    }

    await member.roles.add(roleToAssign);
    return res.json({ status: 'success', message: 'Role assigned successfully' });
  } catch (error) {
    console.error(`Error assigning role: ${error}`);
    return res.status(500).json({ status: 'error', message: error.message });
  }
});

// Start Express server
app.listen(5002, () => {
  console.log('API server running on port 5002');
});

// Discord bot setup
client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

client.login('YOUR_DISCORD_BOT_TOKEN');
Leave a Comment