DiscordTickets/index.js

278 lines
11 KiB
JavaScript
Raw Normal View History

2019-05-04 00:09:48 +03:00
/*
###############################################################################################
2019-05-04 18:09:05 +03:00
____ _ _____ _ _
2019-05-04 00:09:48 +03:00
| _ \ (_) ___ ___ ___ _ __ __| | |_ _| (_) ___ | | __ ___ | |_ ___
| | | | | | / __| / __| / _ \ | '__| / _` | | | | | / __| | |/ / / _ \ | __| / __|
| |_| | | | \__ \ | (__ | (_) | | | | (_| | | | | | | (__ | < | __/ | |_ \__ \
|____/ |_| |___/ \___| \___/ |_| \__,_| |_| |_| \___| |_|\_\ \___| \__| |___/
===============================================================================================
---------------------
Discord Tickets
---------------------
A bot created by Eartharoid for the Discord (TM) platform. [GNU-GPLv3.0]
> The bot manages user-created support tickets to allow your support team to provide quicker
and better assistance to your community members.
---------------------
Quick Start
---------------------
> For detailed instructions, visit the github repository and read the documentation.
> Assuming you have created discord application, edit 'config.json' to allow the bot to
function correctly.
> You will need your bot token (keep it a secret) and channel & role IDs from your server
> It is recommended that you do not change much / anything in any of the .js files unless
you know what you are doing, to prevent critical errors from occuring.
===============================================================================================
> For support, visit https://github.com/eartharoid/DiscordTickets/#readme
> My website: https://eartharoid.ml
###############################################################################################
*/
2019-05-05 23:57:51 +03:00
/**
*
* @name DiscordTickets
* @author Eartharoid <eartharoid@gmail.com>
* @license GNU-GPLv3
*
*/
2019-05-04 00:09:48 +03:00
const fs = require('fs');
const Discord = require('discord.js');
const leeks = require('leeks.js');
2019-05-05 16:31:21 +03:00
const log = require(`./handlers/logger.js`);
2019-05-04 00:09:48 +03:00
const config = require('./config.json');
const { version, homepage } = require('./package.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
2019-05-04 01:54:28 +03:00
const cooldowns = new Discord.Collection();
2019-05-04 22:16:48 +03:00
const now = Date.now();
2019-05-04 00:09:48 +03:00
const commands = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
2019-05-05 18:34:39 +03:00
console.log(leeks.colours.magentaBright(`
2019-05-04 00:09:48 +03:00
######## #### ###### ###### ####### ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ###### ## ## ## ######## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ##
######## #### ###### ###### ####### ## ## ########
######## #### ###### ## ## ######## ######## ######
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ##### ###### ## ######
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
## #### ###### ## ## ######## ## ######
2019-05-04 22:16:48 +03:00
`)); // banner appears in console
2019-05-05 18:34:39 +03:00
console.log(leeks.colours.yellow(leeks.styles.bold(`DiscordTickets v${version} - Made By Eartharoid`)));
console.log(leeks.colours.yellow(leeks.styles.bold(homepage)));
console.log('\n\n');
console.log(leeks.colours.bgGrey(leeks.colours.grey(`\n\n==========================================================================\n\n`)))
console.log('\n\n');
2019-05-06 00:08:30 +03:00
log.init('DiscordTickets (bot created by Eartharoid)')
2019-05-05 23:57:51 +03:00
// all log.* functions are logged to ./log/file.log from here onwards
2019-05-05 16:31:21 +03:00
log.info(`Starting up...`)
2019-05-04 22:16:48 +03:00
client.once('ready', () => { // after bot has logged in
2019-05-05 16:31:21 +03:00
log.info(`Initialising bot...`)
2019-05-04 00:09:48 +03:00
for (const file of commands) {
2019-05-04 01:54:28 +03:00
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
2019-05-05 16:31:21 +03:00
log.console(`> Loading '${config.prefix}${command.name}' command`);
2019-05-04 00:09:48 +03:00
}
2019-05-05 16:31:21 +03:00
log.success(`Connected to Discord API`)
log.success(`Logged in as ${client.user.tag}`)
2019-05-05 00:37:20 +03:00
client.user.setPresence({game: {name: config.playing, type: config.activityType},status: config.status})
2019-05-05 16:31:21 +03:00
// .then(log.basic)
.catch(log.error);
2019-05-04 00:09:48 +03:00
2019-05-04 01:54:28 +03:00
if (config.useEmbeds) {
2019-05-04 00:09:48 +03:00
const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
.setColor("#2ECC71")
.setDescription(":white_check_mark: **Started succesfully**")
2019-05-05 20:42:56 +03:00
.setFooter(`DiscordTickets by Eartharoid`);
2019-05-05 16:31:21 +03:00
client.channels.get(config.logChannel).send(embed)
2019-05-04 00:09:48 +03:00
} else {
client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
}
2019-05-04 01:54:28 +03:00
if (client.guilds.get(config.guildID).member(client.user).hasPermission("ADMINISTRATOR", false)) {
2019-05-05 16:31:21 +03:00
log.info(`Checking permissions...`);
2019-05-04 22:16:48 +03:00
setTimeout(function() {
2019-05-05 16:31:21 +03:00
log.success(`Required permissions have been granted\n\n`)
2019-05-04 22:16:48 +03:00
}, 1250);
2019-05-04 01:54:28 +03:00
if (config.useEmbeds) {
2019-05-04 00:09:48 +03:00
const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
.setColor("#2ECC71")
.setDescription(":white_check_mark: **Required permissions have been granted**")
2019-05-05 20:42:56 +03:00
.setFooter(`DiscordTickets by Eartharoid`);
2019-05-05 16:31:21 +03:00
client.channels.get(config.logChannel).send(embed)
2019-05-04 00:09:48 +03:00
} else {
client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
}
} else {
2019-05-05 16:31:21 +03:00
log.error(`Required permissions have not been granted`)
log.error(`Please give the bot the 'ADMINISTRATOR' permission\n\n`)
2019-05-04 01:54:28 +03:00
if (config.useEmbeds) {
2019-05-04 00:09:48 +03:00
const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
.setColor("#E74C3C")
.setDescription(":x: **Required permissions have not been granted**\nPlease give the bot the `ADMINISTRATOR` permission")
2019-05-05 20:42:56 +03:00
.setFooter(`DiscordTickets by Eartharoid`);
2019-05-04 01:54:28 +03:00
client.channels.get(config.logChannel).send({
embed
})
2019-05-04 00:09:48 +03:00
} else {
client.channels.get(config.logChannel).send(":white_check_mark: **Started succesfully**")
}
}
});
2019-05-04 18:09:05 +03:00
client.on('message', async message => {
2019-05-04 01:54:28 +03:00
// if (!message.content.startsWith(config.prefix) || message.author.bot) return;
if (message.author.bot) return;
2019-05-04 00:09:48 +03:00
if (message.channel.type === "dm") {
2019-05-04 01:54:28 +03:00
if (message.author.id === client.user.id) return;
2019-05-04 18:09:05 +03:00
// message.channel.send(`Sorry, commands can only be used on the server.`)
2019-05-04 01:54:28 +03:00
if (config.logDMs) {
if (config.useEmbeds) {
const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Ticket Log`, client.user.avatarURL)
.setTitle("DM Logger")
.addField("Username", message.author.tag, true)
.addField("Message", message.content, true)
2019-05-05 20:42:56 +03:00
.setFooter(`DiscordTickets by Eartharoid`);
2019-05-05 16:31:21 +03:00
client.channels.get(config.logChannel).send(embed)
2019-05-04 01:54:28 +03:00
} else {
client.channels.get(config.logChannel).send(`DM received from **${message.author.tag} (${message.author.id})** : \n\n\`\`\`${message.content}\`\`\``);
}
} else {
return
};
2019-05-04 00:09:48 +03:00
2019-05-04 01:54:28 +03:00
}
if (message.channel.bot) return;
2019-05-04 00:09:48 +03:00
2019-05-04 01:54:28 +03:00
// const args = message.content.slice(config.prefix.length).split(/ +/);
2019-05-04 00:09:48 +03:00
2019-05-04 18:09:05 +03:00
2019-05-04 00:09:48 +03:00
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|\\${config.prefix})\\s*`);
2019-05-04 01:54:28 +03:00
if (!prefixRegex.test(message.content)) return;
const [, matchedPrefix] = message.content.match(prefixRegex);
const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
2019-05-04 02:01:33 +03:00
// if (!client.commands.has(commandName)) return;
// const command = client.commands.get(commandName);
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
2019-05-04 00:09:48 +03:00
2019-05-04 18:09:05 +03:00
if (command.guildOnly && message.channel.type !== 'text') {
return message.channel.send(`Sorry, this command can only be used on the server.`)
}
2019-05-04 01:54:28 +03:00
if (command.args && !args.length) {
2019-05-04 14:37:39 +03:00
// let reply = `:x: **Arguments were expected but none were provided.**`;
//
// if (command.usage) {
// reply += `\n**Usage:** \`${config.prefix}${command.name} ${command.usage}\``;
// }
//
// return message.channel.send(reply);
if (config.useEmbeds) {
const embed = new Discord.RichEmbed()
.setColor("#E74C3C")
.setDescription(`\n**Usage:** \`${config.prefix}${command.name} ${command.usage}\`\nType \`${config.prefix}help ${command.name}\` for more information`)
return message.channel.send({embed})
2019-05-04 01:54:28 +03:00
2019-05-04 14:37:39 +03:00
} else {
return message.channel.send(`**Usage:** \`${config.prefix}${command.name} ${command.usage}\`\nType \`${config.prefix}help ${command.name}\` for more information`)
2019-05-04 01:54:28 +03:00
}
};
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
2019-05-04 14:20:26 +03:00
if (config.useEmbeds) {
const embed = new Discord.RichEmbed()
.setColor("#E74C3C")
.setDescription(`:x: **Please do not spam commands** (wait ${timeLeft.toFixed(1)}s)`)
return message.channel.send({embed})
} else {
return message.reply(`please do not spam commands (wait ${timeLeft.toFixed(1)}s)`);
}
2019-05-04 00:09:48 +03:00
}
2019-05-04 01:54:28 +03:00
}
2019-05-04 02:01:33 +03:00
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
2019-05-04 01:54:28 +03:00
try {
// client.commands.get(command).execute(message, args, config);
2019-05-04 02:01:33 +03:00
command.execute(message, args);
2019-05-05 20:42:56 +03:00
if(config.useEmbeds) {
const embed = new Discord.RichEmbed()
.setAuthor(`${client.user.username} / Command Log`, client.user.avatarURL)
.setTitle("Command Used")
.addField("Username", message.author, true)
.addField("Command", command.name, true)
.setFooter(`DiscordTickets`)
.setTimestamp();
client.channels.get(config.logChannel).send({embed})
} else {
client.channels.get(config.logChannel).send(`**${message.author.tag} (${message.author.id})** used the \`${command.name}\` command`);
}
log.console(`${message.author.tag} used the '${command.name}' command`)
2019-05-04 01:54:28 +03:00
} catch (error) {
2019-05-05 16:31:21 +03:00
log.error(error);
2019-05-04 01:54:28 +03:00
message.channel.send(`:x: **Oof!** An error occured whilst executing that command.\nThe issue has been reported.`);
2019-05-05 16:31:21 +03:00
log.error(`An unknown error occured whilst executing the '${command.name}' command`);
2019-05-04 01:54:28 +03:00
}
2019-05-04 00:09:48 +03:00
});
2019-05-04 19:46:53 +03:00
client.on('error', error => {
2019-05-05 16:31:21 +03:00
log.warn(`Potential error detected\n(likely Discord API connection issue)\n`);
log.error(`Client error:\n${error}`);
2019-05-04 19:46:53 +03:00
});
2019-05-05 16:31:21 +03:00
client.on('warn', (e) => log.warn(`${e}`));
if(config.debugLevel == 1){ client.on('debug', (e) => log.debug(`${e}`)) };
2019-05-04 00:09:48 +03:00
process.on('unhandledRejection', error => {
2019-05-05 16:31:21 +03:00
log.warn(`An error was not caught`);
log.error(`Uncaught error: \n${error.stack}`);
2019-05-04 00:09:48 +03:00
});
2019-05-04 16:29:49 +03:00
process.on('beforeExit', (code) => {
2019-05-05 16:31:21 +03:00
log.basic(leeks.colours.yellowBright(`Disconected from Discord API`));
log.basic(`Exiting (${code})`);
2019-05-04 00:47:21 +03:00
});
2019-05-04 00:09:48 +03:00
client.login(config.token);