Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Defining Variables and Loggin into the Client/Bot */
- const Discord = require("discord.js");
- const fs = require("fs");
- const client = new Discord.Client( {
- partials: ['USER', 'CHANNEL', 'GUILD_MEMBER', 'REACTION', 'MESSAGE'] // partials so I can include role reactions in this same bot.
- });
- const config = require("./config.json");
- // We also need to make sure we're attaching the config to the CLIENT so it's accessible everywhere!
- client.config = config;
- /* Adding Events & Commands to bot */
- /* Adding Events to bot */
- fs.readdir("./events/", (err, files) => {
- if (err) return console.error(err);
- files.forEach(file => {
- const event = require(`./events/${file}`);
- let eventName = file.split(".")[0];
- client.on(eventName, event.bind(null, client));
- });
- });
- /* Reading Commands Files for usage */
- client.commands = new Discord.Collection();
- const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
- for (const file of commandFiles) {
- console.log(file.slice(0,-3));
- const command = require(`./commands/${file.slice(0,-3)}`)
- client.commands.set(command.name, command);
- }
- /* Database Stuffs */
- let connection;
- const guildCommandPrefixes = new Map(); // Creates a new map for per-server prefixes
- /* Displays that the bot was logged in and loads the per-server prefixes */
- client.on('ready', () => {
- const Guilds = client.guilds.cache.map(guild => guild.id);
- console.log('Guilds: ' + Guilds);
- console.log(client.user.tag + " has logged in.");
- client.guilds.cache.forEach (guild => {
- connection.query (
- `SELECT cmdPrefix FROM GuildConfigurable WHERE guildId = '${guild.id}'`
- ).then(result => {
- guildCommandPrefixes.set(guild.id, result[0][0].cmdPrefix);
- }).catch(err => console.log(err))
- });
- // Sends embed when bot is reloaded
- const onEmbed = new Discord.MessageEmbed() // WORKS!
- .setColor('#000000')
- .setFooter(`The bot is now online. :) If you have issues please ping cute.as.ducks#8061 (Erin).`);
- let channel = client.channels.cache.find(c => c.id === '776192612671553578');
- channel.send(onEmbed);
- // Sets Bot's Status
- client.user.setPresence({
- status: "dnd",
- activity: {
- name: "++help",
- type: "PLAYING"
- }
- });
- });
- /* Adds Guilds to SQL table if there isn't already a row for the guild */
- client.on('guildCreate', async (guild) => {
- try {
- await connection.query(
- `INSERT INTO Guilds VALUES('${guild.id}', '${guild.ownerID}')`
- );
- await connection.query(
- `INSERT INTO GuildConfigurable (guildId) VALUES ('${guild.id}')`
- );
- } catch(err) {
- console.log(err);
- }
- });
- /* commands for per-server prefix */
- client.on('message', async (message) => {
- if(message.author.bot) return;
- const prefix = guildCommandPrefixes.get(message.guild.id);
- if(message.content.toLowerCase().startsWith(prefix + 'ping')) {
- message.channel.send(`Pong! You triggered this command with the prefix: ${prefix}`);
- } else if(message.content.toLowerCase().startsWith(prefix + 'changeprefix')) {
- if(message.member.id === message.guild.ownerID) {
- const [ cmdName, newPrefix ] = message.content.split(" ");
- if(newPrefix) {
- try {
- await connection.query(
- `UPDATE GuildConfigurable SET cmdPrefix = '${newPrefix}' WHERE guildId = '${message.guild.id}'`
- );
- guildCommandPrefixes.set(message.guild.id, newPrefix);
- message.channel.send(`Updated guild prefix to ${newPrefix}`);
- } catch(err) {
- console.log(err);
- message.channel.send(`Failed to update guild prefix.`);
- }
- } else {
- message.channel.send('❗ Please tell me what prefix you would like, I can\'t update the prefix unless you tell me. You should format this command like so: ' + `\`[originalprefix]changeprefix [newprefix]\``);
- }
- } else {
- message.channel.send('❌ You do not have permission to use this command. Only guild owners can change this command.')
- }
- }
- });
- /* Async function to login to database and the bot itself */
- (async () => {
- connection = await require('../database/db.js');
- await client.login(config.token);
- })();
Add Comment
Please, Sign In to add comment