Created
October 13, 2018 01:46
-
-
Save littletsu/8698b128825f46717957e10f98237012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require Packages -- You will need to install these | |
const Discord = require('discord.js'); | |
const client = new Discord.Client(); // This uses the discord.js package to setup a client | |
// Constant Variables | |
const prefix = '_'; // This can be changed to anything you want | |
const ownerID = '144645791145918464'; // This will be used in future videos | |
// First, we need to create an object containing the serverStats info | |
const serverStats = { | |
guildID: '', | |
totalUsersID: '', | |
memberCountID: '', | |
botCount: '' | |
}; // This is so we can edit the channels, using the ID | |
// Listener Events | |
client.on('message', message => { | |
// This is run whenever a new message is created in a channel the bot has access to view | |
// Variables | |
let args = message.content.slice(prefix.length).trim().split(' '); | |
let cmd = args.shift().toLowerCase(); | |
// Now, we have two variables. cmd contains the command following the prefix | |
// args contains everything following that, split into an array by spaces | |
// Return Statemets | |
if (message.author.bot) return; // This will ignore all bots | |
if (!message.content.startsWith(prefix)) return; // This will return if the message doesn't start with your prefix | |
// Command Handler | |
try { | |
// Bonus: Auto-Reload (You should move this into it's own command) | |
delete require.cache[require.resolve(`./commands/${cmd}.js`)]; | |
// Options | |
let ops = { | |
ownerID: ownerID | |
} | |
let commandFile = require(`./commands/${cmd}.js`); // This will require a file in the commands folder | |
commandFile.run(client, message, args, ops); // This will pass three variables into the file | |
// We can create a basic ping-pong command for starters | |
} catch (e) { // This will catch any errors, either within the code or if the command doesn't exist | |
console.log(e.stack); | |
} | |
}); | |
// Ready Event -- This will run whenever the turns on | |
client.on('ready', () => console.log('Launched!')); | |
// Next, we need to create 2 listener events, one for a member leaving the server, and one for the member joining a server | |
client.on('guildMemberAdd', member => { | |
// We also want to return if the member's guild isn't the same as the one with serverStats | |
if(member.guild.id !== serverStats.guildID) return; | |
// Now, we want to update the voiceChannel names | |
client.channels.get(serverStats.totalUsersID).setName(`Total Users: ${member.guild.memberCount}`); | |
client.channels.get(serverStats.memberCountID).setName(`Member Count: ${member.guild.members.filter(m => !m.user.bot).size}`); | |
client.channels.get(serverStats.botCount).setName(`Bot Count: ${member.guild.members.filter(m => m.user.bot).size}`); | |
}); | |
client.on('guildMemberRemove', member => { | |
if(member.guild.id !== serverStats.guildID) return; | |
// We also want the same thing to happen when a member leaves the guild | |
client.channels.get(serverStats.totalUsersID).setName(`Total Users:${member.guild.memberCount}`); | |
client.channels.get(serverStats.memberCountID).setName(`Member Count: ${member.guild.members.filter(m => !m.user.bot).size}`); | |
client.channels.get(serverStats.botCount).setName(`Bot Count: ${member.guild.members.filter(m => m.user.bot).size}`); | |
}); // Now, we can test it! | |
// Discord Login -- Remember to use your own token, and to NEVER leak it | |
client.login(process.env.TOKEN); // This will be your token instead of process.env.TOKEN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment