Untitled diff

Created Diff never expires
0 removals
161 lines
10 additions
171 lines
// Calling the package
// Calling the package
var Discord = require('discord.js');
var Discord = require('discord.js');
var bot = new Discord.Client();
var bot = new Discord.Client();
var fs = require('fs'); // First, we need to require fs, it is packaged with node.js so no need to download anything extra. // Make sure you have this required.
var fs = require('fs'); // First, we need to require fs, it is packaged with node.js so no need to download anything extra. // Make sure you have this required.
var profanities = require('profanities') // We need to require all of our packages after we install them.
var profanities = require('profanities') // We need to require all of our packages after we install them.


// Second, lets call the file we just made using fs.
// Second, lets call the file we just made using fs.
var userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'));
var userData = JSON.parse(fs.readFileSync('Storage/userData.json', 'utf8'));
var commandsList = fs.readFileSync('Storage/commands.txt', 'utf8'); // We need to call the file, we can just copy the line above and paste it here. We need to edit it a little.
var commandsList = fs.readFileSync('Storage/commands.txt', 'utf8'); // We need to call the file, we can just copy the line above and paste it here. We need to edit it a little.
bot.commands = new Discord.Collection(); // First, we need to make a collection of all the commands for the bot.
bot.commands = new Discord.Collection(); // First, we need to make a collection of all the commands for the bot.


function loadCmds () { // First, we need to turn this into a function so we can call it whenever we want,
fs.readdir('./commands/', (err, files) => { // This reads the directory of the commands folder.
fs.readdir('./commands/', (err, files) => { // This reads the directory of the commands folder.
if(err) console.error(err); // This, sends an error message if it gets an error calling the commands,
if(err) console.error(err); // This, sends an error message if it gets an error calling the commands,


var jsfiles = files.filter(f => f.split('.').pop() === 'js'); // This checks if the file extension is 'js', or the text after the . is 'js'.
var jsfiles = files.filter(f => f.split('.').pop() === 'js'); // This checks if the file extension is 'js', or the text after the . is 'js'.
if (jsfiles.length <= 0) { return console.log('No commands found...')} // This returns & sends to the console that no commands were found in the folder.
if (jsfiles.length <= 0) { return console.log('No commands found...')} // This returns & sends to the console that no commands were found in the folder.
else { console.log(jsfiles.length + ' commands found.') } // This tells how many commands it found.
else { console.log(jsfiles.length + ' commands found.') } // This tells how many commands it found.


jsfiles.forEach((f, i) => { // This, loops through each file and runs the following code.
jsfiles.forEach((f, i) => { // This, loops through each file and runs the following code.
delete require.cache[require.resolve(`./commands/${f}`)]; // This deletes the cached file that you specify, allowing you to load something in its place.
delete require.cache[require.resolve(`./commands/${f}`)]; // This deletes the cached file that you specify, allowing you to load something in its place.
var cmds = require(`./commands/${f}`); // This gets every js file in the chosen folder.
var cmds = require(`./commands/${f}`); // This gets every js file in the chosen folder.
console.log(`Command ${f} loading...`); // This logs to the console that the command <name> is loading.
console.log(`Command ${f} loading...`); // This logs to the console that the command <name> is loading.
bot.commands.set(cmds.config.command, cmds); // This gets the name of the command, as well as the modules in the file.
bot.commands.set(cmds.config.command, cmds); // This gets the name of the command, as well as the modules in the file.
})
})




})
})

}


function userInfo(user) {
function userInfo(user) {
var finalString = ''; // This is the beginning of the final string, but we need to add things to it.
var finalString = ''; // This is the beginning of the final string, but we need to add things to it.


// Name
// Name
finalString += '**' + user.username + '**, with the **ID** of **' + user.id + '**'; // This gets the name of the user, and the ID, and adds it to the final string.
finalString += '**' + user.username + '**, with the **ID** of **' + user.id + '**'; // This gets the name of the user, and the ID, and adds it to the final string.


// Now, lets add the created At date. This doesn't look good, so lets split it and recreate the createdAt message.
// Now, lets add the created At date. This doesn't look good, so lets split it and recreate the createdAt message.
var userCreated = user.createdAt.toString().split(' ');
var userCreated = user.createdAt.toString().split(' ');
finalString += ', was **created on ' + userCreated[1] + ' ' + userCreated[2] + ', ' + userCreated[3] + '**.'
finalString += ', was **created on ' + userCreated[1] + ' ' + userCreated[2] + ', ' + userCreated[3] + '**.'


// Messages Sent
// Messages Sent
finalString += ' Since then, they have **sent ' + userData[user.id].messagesSent + ' messages** to this discord.'
finalString += ' Since then, they have **sent ' + userData[user.id].messagesSent + ' messages** to this discord.'






return finalString; // This sends what we wrote to the function call down in the script.
return finalString; // This sends what we wrote to the function call down in the script.
}
}


loadCmds(); // We also want to make sure that we call it when the script starts, so it loads.
// Listener Event: Message Received ( This will run every time a message is received)
// Listener Event: Message Received ( This will run every time a message is received)
bot.on('message', message => {
bot.on('message', message => {


// Variables
// Variables
var sender = message.author; // The person who sent the message
var sender = message.author; // The person who sent the message
var msg = message.content.toUpperCase(); // Takes the message, and makes it all uppercase
var msg = message.content.toUpperCase(); // Takes the message, and makes it all uppercase
var prefix = '>' // The text before commands, you can set this to what ever you want
var prefix = '>' // The text before commands, you can set this to what ever you want
var cont = message.content.slice(prefix.length).split(" "); // This slices off the prefix, then puts it in an array.
var cont = message.content.slice(prefix.length).split(" "); // This slices off the prefix, then puts it in an array.
var args = cont.slice(1); // This is everything after the command in an array.
var args = cont.slice(1); // This is everything after the command in an array.


if (!message.content.startsWith(prefix)) return; // This returns if the prefix of the command is not the one set.
if (!message.content.startsWith(prefix)) return; // This returns if the prefix of the command is not the one set.


var cmd = bot.commands.get(cont[0]) // This tries to grab the command that you called in chat.
var cmd = bot.commands.get(cont[0]) // This tries to grab the command that you called in chat.
if (cmd) cmd.run(bot, message, args); // This checks if it exists, and if it does it runs the command.
if (cmd) cmd.run(bot, message, args); // This checks if it exists, and if it does it runs the command.

if (msg === prefix + 'RELOAD') {
message.channel.send({embed:{description:"All Commands Reloaded"}}) // This sends an embed letting the channel know you reloaded all commands.
message.channel.send('All Commands Reloaded') // This does the same thing but not in an embed, ill show both right now so you can see the difference.
loadCmds()
}


// Profanity
// Profanity
for (x = 0; x < profanities.length; x++) { // This loops every word of the profanities list you downloaded.
for (x = 0; x < profanities.length; x++) { // This loops every word of the profanities list you downloaded.
if (message.content.toUpperCase() == profanities[x].toUpperCase()) {
if (message.content.toUpperCase() == profanities[x].toUpperCase()) {
message.channel.send('Hey! Don\'t say that!') // Tells them that they can't say that.
message.channel.send('Hey! Don\'t say that!') // Tells them that they can't say that.
message.delete(); // Deletes the message
message.delete(); // Deletes the message
return; // Stops the rest of the commands from running after they pyt profanity in their text.
return; // Stops the rest of the commands from running after they pyt profanity in their text.
}
}
}
}


// First, we need to make sure that it isn't reading a message that the bot is sending.
// First, we need to make sure that it isn't reading a message that the bot is sending.
if (sender.id === '343590789848498176') { // Checks if the ID of the sender is the same id as the bot
if (sender.id === '343590789848498176') { // Checks if the ID of the sender is the same id as the bot
return; // Cancels the rest of the Listener Event.
return; // Cancels the rest of the Listener Event.
}
}


// Be sure to delete the command from your app.js so it doesn't respond multiple times!
// Be sure to delete the command from your app.js so it doesn't respond multiple times!


// Deleting Specific Messages ( Messages that are not an ID for me )
// Deleting Specific Messages ( Messages that are not an ID for me )
if (message.channel.id === '343586693145821184') { // Checks if the message is in the specific channel
if (message.channel.id === '343586693145821184') { // Checks if the message is in the specific channel
if (isNaN(message.content)) { // Checks if the message is not a number, if it's not the following code will run
if (isNaN(message.content)) { // Checks if the message is not a number, if it's not the following code will run
//message.delete() // This deletes the message
//message.delete() // This deletes the message
//message.author.send('Please only post the number, and not any other text in this channel, thank you!') // This private messages the author that what they posted was invalid.
//message.author.send('Please only post the number, and not any other text in this channel, thank you!') // This private messages the author that what they posted was invalid.
}
}
}
}


// You can also do this for other words, in all of the channels.
// You can also do this for other words, in all of the channels.
if (msg.includes('LETTUCE')) { // Checks if the word lettuce is included in the message
if (msg.includes('LETTUCE')) { // Checks if the word lettuce is included in the message
message.delete(); // Deletes the message
message.delete(); // Deletes the message
message.author.send('The word **Lettuce** is banned, please don\'t use it!') // This private messages the author that what they posted had a banned word.
message.author.send('The word **Lettuce** is banned, please don\'t use it!') // This private messages the author that what they posted had a banned word.
}
}


// Now, lets make sure their username is there bfore writing to the file.
// Now, lets make sure their username is there bfore writing to the file.
if (!userData[sender.id]) userData[sender.id] = {
if (!userData[sender.id]) userData[sender.id] = {
messagesSent: 0
messagesSent: 0
}
}


// Now, lets increase messagesSent and write to the final file.
// Now, lets increase messagesSent and write to the final file.
userData[sender.id].messagesSent++; // This adds one to 'messagesSent', under the user.
userData[sender.id].messagesSent++; // This adds one to 'messagesSent', under the user.


// To save the file we have to write this:
// To save the file we have to write this:
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
fs.writeFile('Storage/userData.json', JSON.stringify(userData), (err) => {
if (err) console.error(err); // We just want it to log if their is an error.
if (err) console.error(err); // We just want it to log if their is an error.
});
});


// Lets delete that so we can recreate it using a better command.
// Lets delete that so we can recreate it using a better command.
if (msg.startsWith(prefix + 'USERINFO')) { // This checks if the message starts with the command, since they will be adding things to the end of it.
if (msg.startsWith(prefix + 'USERINFO')) { // This checks if the message starts with the command, since they will be adding things to the end of it.
// We should assume that if they are not adding a name to the end of the command, they want info on themselves.
// We should assume that if they are not adding a name to the end of the command, they want info on themselves.
if (msg === prefix + 'USERINFO') {
if (msg === prefix + 'USERINFO') {
message.channel.send(userInfo(sender)); // This will return the message about info on themselves. // We should make a function, so we don't have to write it multiple times.
message.channel.send(userInfo(sender)); // This will return the message about info on themselves. // We should make a function, so we don't have to write it multiple times.
}
}
}
}




});
});


// Listener Event: Bot Launched
// Listener Event: Bot Launched
bot.on('ready', () => {
bot.on('ready', () => {
console.log('Bot Launched...') // Runs when the bot is Launched
console.log('Bot Launched...') // Runs when the bot is Launched


// You can put any code you want here, it will run when you turn on your bot.
// You can put any code you want here, it will run when you turn on your bot.


// We will be going over setting 'game playing', 'status', and 'streaming'
// We will be going over setting 'game playing', 'status', and 'streaming'


// Status
// Status
bot.user.setStatus('dnd') // Your status goes here; It can be 'Online','idle','invisible', & 'dnd'
bot.user.setStatus('dnd') // Your status goes here; It can be 'Online','idle','invisible', & 'dnd'


// Game & Streaming
// Game & Streaming
bot.user.setGame('Hello!') // You can change the string to whatever you want it to say.
bot.user.setGame('Hello!') // You can change the string to whatever you want it to say.
// To set a stream, add another option like this:
// To set a stream, add another option like this:
//bot.user.setGame('Hello!', 'https://twitch.tv/truexpixels'); // It has to be a twitch stream link.
//bot.user.setGame('Hello!', 'https://twitch.tv/truexpixels'); // It has to be a twitch stream link.


});
});


// Listner Event: User joining the discord server.
// Listner Event: User joining the discord server.
bot.on('guildMemberAdd', member => {
bot.on('guildMemberAdd', member => {


console.log('User ' + member.user.username + ' has joined the server!') // Sends a message in console that someone joined the discord server.
console.log('User ' + member.user.username + ' has joined the server!') // Sends a message in console that someone joined the discord server.
console.log(member)
console.log(member)
// Now, lets add a role when they join. First, we need to get the role we want.
// Now, lets add a role when they join. First, we need to get the role we want.
var role = member.guild.roles.find('name', 'User'); // This looks for the role in the server(guild), it searches by name, meaning you can change 'User' to the role you want.
var role = member.guild.roles.find('name', 'User'); // This looks for the role in the server(guild), it searches by name, meaning you can change 'User' to the role you want.


// Secondly, we will add the role.
// Secondly, we will add the role.
member.addRole(role)
member.addRole(role)


// Sending a message to a channel when a user joins discord.
// Sending a message to a channel when a user joins discord.
member.guild.channels.get('343580441313542144').send('**' + member.user.username + '**, has joined the server!'); // The first part gets the channel, the second sends a message to that channel.
member.guild.channels.get('343580441313542144').send('**' + member.user.username + '**, has joined the server!'); // The first part gets the channel, the second sends a message to that channel.




});
});


// Now, let's make it so that when someone leaves, code runs.
// Now, let's make it so that when someone leaves, code runs.
// Listener Event: User leaving the discord server.
// Listener Event: User leaving the discord server.
bot.on('guildMemberRemove', member => {
bot.on('guildMemberRemove', member => {


// The code can simply be copied from the line you made before.
// The code can simply be copied from the line you made before.
member.guild.channels.get('343580441313542144').send('**' + member.user.username + '**, has left the server!'); // The first part gets the channel, the second sends a message to that channel.
member.guild.channels.get('343580441313542144').send('**' + member.user.username + '**, has left the server!'); // The first part gets the channel, the second sends a message to that channel.
// You can choose whatever 'Channel ID' you want, and whatever text you want it to say!
// You can choose whatever 'Channel ID' you want, and whatever text you want it to say!




});
});