• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

PW Verification Discord Bot

Status
Not open for further replies.
Banned
Banned
Joined
Jul 5, 2014
Messages
88
Reaction score
76
as the name suggests, this is a discord bot that can verify your users and give them a specific role afterwards.

THINGS TO NOTE: the code is pretty much basic, I would suggest that you expand upon it for your personal use.

This bot uses nodejs, you need the discord.js package to use this. (Assuming you already know that by looking at the code )

1701470774037 - PW Verification Discord Bot - RaGEZONE Forums


Code:
const { Client, Intents } = require('discord.js');
const client = new Client({
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS],
});

// Database connection
const mysql = require('mysql');
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'pw',
});

db.connect((err) => {
  if (err) {
    console.error('Error connecting to the database:', err);
    throw err;
  }
  console.log('Connected to the database.');
});

client.on('ready', async () => {
  console.log(`Bot is ready as: ${client.user.tag}`);

  // Define a new slash command
  const data = {
    name: 'verify',
    description: 'Used to Verify your username to get access to the server.',
    options: [
      {
        name: 'username',
        description: 'The username to search for',
        type: 'STRING',
        required: true,
      },
    ],
  };

  // Register the slash command for a specific guild
  const guild = client.guilds.cache.get('serverID');
  await guild.commands.create(data);
});

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;

  const { commandName, options, guild } = interaction;

  if (commandName === 'verify') {
    try {
      // Get the username from user input
      const username = options.getString('username');

      if (!username) {
        interaction.reply('Please provide a username.');
        return;
      }

      // Update the SQL query to search for the username in the "name" column
      let sql = 'SELECT * FROM users WHERE name = ?';
      const results = await query(sql, [username]);

      if (results.length > 0) {
        // Get the member associated with the interaction
        const member = guild.members.cache.get(interaction.user.id);

        // Get the role to assign (replace 'YourRoleID' with your actual role ID)
        const roleToAssign = guild.roles.cache.get('role ID');

        // Check if the role exists and the member is not already assigned the role
        if (roleToAssign && !member.roles.cache.has(roleToAssign.id)) {
          // Add the role to the member
          await member.roles.add(roleToAssign);
          interaction.reply('You are verified! The role has been assigned.');
        } else {
          interaction.reply('You are already verified or the role does not exist.');
        }
      } else {
        interaction.reply('Username not found in the database.');
      }
    } catch (err) {
      console.error('Error executing database query:', err);
      interaction.reply('An error occurred while processing your request.');
    }
  }
});

async function query(sql, values) {
  return new Promise((resolve, reject) => {
    db.query(sql, values, (err, results) => {
      if (err) {
        reject(err);
      } else {
        resolve(results);
      }
    });
  });
}

client.login('clientID');
 

Attachments

You must be registered for see attachments list
Last edited:
Status
Not open for further replies.
Back
Top