const Discord = require('discord.js'); const bot = new Discord.Client({ ws: { intents: ['GUILD_MEMBERS']}}); const fs = require('fs'); const util = require('util'); const mysql = require('mysql2'); const { addConsoleHandler } = require('selenium-webdriver/lib/logging'); let connection; require('dotenv').config(); const guildCommandPrefixes = new Map(); ///Bot is online sent to command prompt bot.on('ready', () =>{ console.log('this bot in online!'); bot.guilds.cache.forEach(guild =>{ connection.query( `SELECT cmdPrefix FROM ServerConfig WHERE serverId = '${guild.id}'` ).then(result =>{ guildCommandPrefixes.set(guild.id, result[0][0]?.cmdPrefix); }).catch(err => console.log(err)); }); }); ///Check for if a guild owner gained or lost the paid role bot.on('guildMemberUpdate', async (oldMember, newMember) => { let test1 = oldMember.roles; console.log(test1); let test2 = newMember.roles.cache.has(r => r.id === `${[process.env.PAID_ROLE]}`); console.log(test2); if (test1 !== test2) { console.log("role change"); } else { return; } } ); /// adding a guild to the database upon invite bot.on('guildCreate', async (guild) => { // Guild the user needs to have the role in let myGuild = await bot.guilds.fetch(process.env.BOT_GUILD); try { /// insert serverid and serverownerid into servers db await connection.query( `INSERT INTO Servers (serverId, serverOwnerId, paidRole) VALUES ('${guild.id}', '${guild.ownerID}', NULL)` ); /// insert server id into serverconfig db await connection.query( `INSERT INTO ServerConfig (serverId) VALUES ('${guild.id}')` ); }catch(err){ console.log(err); // find default channel let defaultChannel = ""; guild.channels.cache.forEach((channel) => { if(channel.type == "text" && defaultChannel == "") { if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) { defaultChannel = channel; console.log(defaultChannel); } } }); // Member object of the user in guildA try{ let guildOwner = await myGuild.members.fetch(guild.ownerID); if (!guildOwner) return console.log(`Oops, ${guild.owner} is not a member of your server.`); }catch(error) { return console.log(`Oops, ${guild.owner} is not a member of your server.`), defaultChannel.send('Please kick the bot, have the guild owner join this discord https://discord.gg/tDTjBAaCAn, Then you can reinvite the bot and you will be properly added to the database and can use the bot! dont forget to check out the premium features while your there if you decide you want more features from Gate Bot!'); } //Check if they have the role let guildOwner = await myGuild.members.fetch(guild.ownerID); let ownerHasPaidRole = guildOwner.roles.cache.has(process.env.PAID_ROLE); if (ownerHasPaidRole){ console.log(`Woohoo, ${guildOwner} has the required role`); await connection.query( `UPDATE Servers SET paidRole = '1' WHERE serverId = ${guild.id}` ); } else { await connection.query( `UPDATE Servers SET paidRole = '0' WHERE serverId = ${guild.id}` ); } }}); ///Token for discord bot (async () => { connection = await require('./database/db.js'); await bot.login(process.env.BOT_TOKEN); })(); ///allowing the script to see files from the commands folder bot.commands = new Discord.Collection(); const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js')); for(const file of commandFiles){ const command = require(`./commands/${file}`); bot.commands.set(command.name, command); } ///Message Handler bot.on('message', async (message) => { const prefix = guildCommandPrefixes.get(message.guild.id); console.log('caught message'); if(!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const command = args.shift().toLowerCase(); ///basic ping pong test command if(command === 'help'){ bot.commands.get('help').execute(message); } ///basic ping pong test command else if(command === 'ping'){ bot.commands.get('ping').execute(message); } ///change the servers prefix else if(command === 'changeprefix'){ bot.commands.get('changeprefix').execute(message, connection, prefix, guildCommandPrefixes); } ///arguments test else if (command === 'argsinfo'){ bot.commands.get('argsinfo').execute(message, command, args) } ///role update command else if (command === 'checkrole'){ bot.commands.get('checkrole').execute(message, connection, bot) } ///message command list else if (command === 'autoungate'){ bot.commands.get('autoungate').execute(message, args, Discord); }});