v2 rewrite with discord.js@v12 support. Now with SQLite or MySQL storage.

Code is at least 80% less bad.
This commit is contained in:
Isaac (eartharoid)
2020-08-12 23:02:33 +01:00
parent c77f38f209
commit 8431b2d241
44 changed files with 4072 additions and 802 deletions

117
src/commands/add.js Normal file
View File

@@ -0,0 +1,117 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const Discord = require('discord.js');
const config = require('../../user/config.js');
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
name: 'add',
description: 'Add a member to a ticket channel',
usage: '<@member> [... #channel]',
aliases: ['+'],
example: 'add @member to #ticket-23',
args: true,
async execute(client, message, args, Ticket) {
const notTicket = new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **This isn\'t a ticket channel**')
.setDescription('Use this command in the ticket channel you want to add a user to, or mention the channel.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL());
let ticket;
let channel = message.mentions.channels.first();
if(!channel) {
channel = message.channel;
ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(!ticket)
return message.channel.send(notTicket);
} else {
ticket = await Ticket.findOne({ where: { channel: channel.id } });
if(!ticket) {
notTicket
.setTitle(':x: **Channel is not a ticket**')
.setDescription(`${channel} is not a ticket channel.`);
return message.channel.send(notTicket);
}
}
if(message.author.id !== ticket.get('creator') && !message.member.roles.cache.has(config.staff_role))
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **No permission**')
.setDescription(`You don't have permission to alter ${channel} as it does not belong to you and you are not staff.`)
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
let member = message.guild.member(message.mentions.users.first() || message.guild.members.cache.get(args[0]));
if(!member)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **Unknown member**')
.setDescription('Please mention a valid member.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
try {
channel.updateOverwrite(member.user, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true
});
if(channel.id !== message.channel.id)
channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(member.user.username, member.user.displayAvatarURL())
.setTitle('**Member added**')
.setDescription(`${member} has been added by ${message.author}`)
.setFooter(message.guild.name, message.guild.iconURL())
);
message.channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(member.user.username, member.user.displayAvatarURL())
.setTitle(':white_check_mark: **Member added**')
.setDescription(`${member} has been added to <#${ticket.get('channel')}>`)
.setFooter(message.guild.name, message.guild.iconURL())
);
log.info(`${message.author.tag} added a user to a ticket (#${message.channel.id})`);
} catch (error) {
log.error(error);
}
// command ends here
},
};

106
src/commands/close.js Normal file
View File

@@ -0,0 +1,106 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const Discord = require('discord.js');
const config = require('../../user/config');
module.exports = {
name: 'close',
description: 'Close a ticket; either a specified (mentioned) channel, or the channel the command is used in.',
usage: '[ticket]',
aliases: ['none'],
example: 'close #ticket-17',
args: false,
async execute(client, message, args, Ticket) {
const notTicket = new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **This isn\'t a ticket channel**')
.setDescription('Use this command in the ticket channel you want to close, or mention the channel.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL());
let ticket;
const channel = message.mentions.channels.first();
// let channel = message.guild.channels.resolve(message.mentions.channels.first()); // not necessary
if(!channel) {
ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(!ticket)
return message.channel.send(notTicket);
ticket.update({ open: false}, { where: { channel: message.channel.id } });
message.channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(`:white_check_mark: **Ticket ${ticket.id} closed**`)
.setDescription('The channel will be automatically deleted once the contents have been archived.')
.setFooter(message.guild.name, message.guild.iconURL())
);
setTimeout(() => message.channel.delete(), 5000);
} else {
ticket = await Ticket.findOne({ where: { channel: channel.id } });
if(!ticket) {
notTicket
.setTitle(':x: **Channel is not a ticket**')
.setDescription(`${channel} is not a ticket channel.`);
return message.channel.send(notTicket);
}
if(message.author.id !== ticket.get('creator') && !message.member.roles.cache.has(config.staff_role))
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **No permission**')
.setDescription(`You don't have permission to close ${channel} as it does not belong to you and you are not staff.`)
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
ticket.update({ open: false}, { where: { channel: channel.id } });
message.channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(`:white_check_mark: **Ticket ${ticket.id} closed**`)
.setDescription('The channel will be automatically deleted once the contents have been archived.')
.setFooter(message.guild.name, message.guild.iconURL())
);
setTimeout(() => channel.delete(), 5000);
}
log.info(`${message.author.tag} closed a ticket (#ticket-${ticket.get('id')})`);
if (config.logs.discord.enabled)
client.channels.cache.get(config.logs.discord.channel).send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle('Ticket closed')
.addField('Creator', `<@${ticket.get('creator')}>` , true)
.addField('Closed by', message.author, true)
.setFooter(client.user.username, client.user.avatarURL())
.setTimestamp()
);
},
};

98
src/commands/help.js Normal file
View File

@@ -0,0 +1,98 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const Discord = require('discord.js');
const config = require('../../user/config');
module.exports = {
name: 'help',
description: 'Display help menu',
usage: '[command]',
aliases: ['command', 'commands'],
example: 'help new',
args: false,
execute(client, message, args) {
const commands = Array.from(client.commands.values());
if (!args.length) {
let cmds = [];
for (let command of commands) {
if (command.hide)
continue;
if (command.permission && !message.member.hasPermission(command.permission))
continue;
let desc = command.description;
if (desc.length > 50)
desc = desc.substring(0, 50) + '...';
cmds.push(`**${config.prefix}${command.name}** **·** ${desc}`);
}
message.channel.send(
new Discord.MessageEmbed()
.setTitle('Commands')
.setColor(config.colour)
.setDescription(
`\nThe commands you have access to are listed below. Type \`${config.prefix}help [command]\` for more information about a specific command.
\n${cmds.join('\n\n')}
\nPlease contact a member of staff if you require assistance.`
)
.setFooter(message.guild.name, message.guild.iconURL())
.setTimestamp()
).catch((error) => {
log.warn('Could not send help menu');
log.error(error);
});
} else {
const name = args[0].toLowerCase();
const command = client.commands.get(name) || client.commands.find(c => c.aliases && c.aliases.includes(name));
if (!command)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setDescription(`:x: **Invalid command name** (\`${config.prefix}help\`)`)
);
const cmd = new Discord.MessageEmbed()
.setColor(config.colour)
.setTitle(command.name);
if (command.long) {
cmd.setDescription(command.long);
} else {
cmd.setDescription(command.description);
}
if (command.aliases) cmd.addField('Aliases', `\`${command.aliases.join(', ')}\``, true);
if (command.usage) cmd.addField('Usage', `\`${config.prefix}${command.name} ${command.usage}\``, false);
if (command.usage) cmd.addField('Example', `\`${config.prefix}${command.example}\``, false);
if (command.permission && !message.member.hasPermission(command.permission)) {
cmd.addField('Required Permission', `\`${command.permission}\` :exclamation: You don't have permission to use this command`, true);
} else {
cmd.addField('Required Permission', `\`${command.permission || 'none'}\``, true);
}
message.channel.send(cmd);
}
// command ends here
},
};

195
src/commands/new.js Normal file
View File

@@ -0,0 +1,195 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const Discord = require('discord.js');
const fs = require('fs');
const config = require('../../user/config');
module.exports = {
name: 'new',
description: 'Create a new support ticket',
usage: '[brief description]',
aliases: ['ticket', 'open'],
example: 'new my server won\'t start',
args: false,
async execute(client, message, args, Ticket) {
const supportRole = message.guild.roles.cache.get(config.staff_role);
if (!supportRole)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setTitle(':x: **Error**')
.setDescription(`${config.name} has not been set up correctly. Could not find a 'support team' role with the id \`${config.staff_role}\``)
.setFooter(message.guild.name, message.guild.iconURL())
);
let tickets = await Ticket.findAndCountAll({
where: {
creator: message.author.id,
open: true
},
limit: config.tickets.max
});
if (tickets.count >= config.tickets.max) {
let ticketList = [];
for (let t in tickets.rows) {
let desc = tickets.rows[t].topic.substring(0, 20);
ticketList
.push(`<#${tickets.rows[t].channel}>: \`${desc}${desc.length > 20 ? '...' : ''}\``);
}
let m = await message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(`:x: **You already have ${tickets.count} or more open tickets**`)
.setDescription(`Use \`${config.prefix}close\` to close unneeded tickets.\n\n${ticketList.join(',\n')}`)
.setFooter(message.guild.name + ' | This message will be deleted in 15 seconds', message.guild.iconURL())
);
return setTimeout(async () => {
await message.delete();
await m.delete();
}, 15000);
}
let topic = args.join(' ');
if (topic.length > 256)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **Description too long**')
.setDescription('Please limit your ticket topic to less than 256 characters. A short sentence will do.')
.setFooter(message.guild.name, message.guild.iconURL())
);
else if (topic.length < 1)
topic = 'No topic given';
let ticket = await Ticket.create({
channel: '',
creator: message.author.id,
open: true,
archived: false,
topic: topic
});
let name = 'ticket-' + ticket.get('id');
message.guild.channels.create(name, {
type: 'text',
topic: `${message.author} | ${topic}`,
parent: config.tickets.category,
permissionOverwrites: [{
id: message.guild.roles.everyone,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES']
},
{
id: message.member,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ATTACH_FILES', 'READ_MESSAGE_HISTORY']
},
{
id: supportRole,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'ATTACH_FILES', 'READ_MESSAGE_HISTORY']
}
],
reason: 'User requested a new support ticket channel'
}).then(async c => {
Ticket.update({
channel: c.id
}, {
where: {
id: ticket.id
}
});
let m = await message.channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':white_check_mark: **Ticket created**')
.setDescription(`Your ticket has been created: ${c}`)
.setFooter(client.user.username + ' | This message will be deleted in 15 seconds', client.user.avatarURL())
);
setTimeout(async () => {
await message.delete();
await m.delete();
}, 15000);
let ping;
switch (config.tickets.ping) {
case 'staff':
ping = `<@&${config.staff_role}>,\n`;
break;
case false:
ping = '';
break;
default:
ping = `@${config.tickets.ping},\n`;
}
await c.send(ping + `${message.author} has created a new ticket`);
if (config.tickets.send_img) {
const images = fs.readdirSync('user/images');
await c.send({
files: [
'user/images/' +
images[Math.floor(Math.random() * images.length)]
]
});
}
let text = config.tickets.text
.replace('{{ name }}', message.author.username)
.replace('{{ tag }}', message.author);
let w = await c.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setDescription(text)
.addField('Topic', `\`${topic}\``)
.setFooter(client.user.username, client.user.avatarURL())
);
if (config.tickets.pin)
await w.pin();
// await w.pin().then(m => m.delete()); // oopsie, this deletes the pinned message
if (config.logs.discord.enabled)
client.channels.cache.get(config.logs.discord.channel).send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle('New ticket')
.setDescription(`\`${topic}\``)
.addField('Creator', message.author, true)
.addField('Channel', c, true)
.setFooter(client.user.username, client.user.avatarURL())
.setTimestamp()
);
log.info(`${message.author.tag} created a new ticket (#${name})`);
}).catch(log.error);
},
};

117
src/commands/remove.js Normal file
View File

@@ -0,0 +1,117 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const Discord = require('discord.js');
const config = require('../../user/config.js');
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
name: 'remove',
description: 'Remove a member from ticket channel',
usage: '<@member> [... #channel]',
aliases: ['-'],
example: 'remove @member from #ticket-23',
args: true,
async execute(client, message, args, Ticket) {
const notTicket = new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **This isn\'t a ticket channel**')
.setDescription('Use this command in the ticket channel you want to remove a user from, or mention the channel.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL());
let ticket;
let channel = message.mentions.channels.first();
if(!channel) {
channel = message.channel;
ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(!ticket)
return message.channel.send(notTicket);
} else {
ticket = await Ticket.findOne({ where: { channel: channel.id } });
if(!ticket) {
notTicket
.setTitle(':x: **Channel is not a ticket**')
.setDescription(`${channel} is not a ticket channel.`);
return message.channel.send(notTicket);
}
}
if(message.author.id !== ticket.get('creator') && !message.member.roles.cache.has(config.staff_role))
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **No permission**')
.setDescription(`You don't have permission to alter ${channel} as it does not belong to you and you are not staff.`)
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
let member = message.guild.member(message.mentions.users.first() || message.guild.members.cache.get(args[0]));
if(!member)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **Unknown member**')
.setDescription('Please mention a valid member.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
try {
channel.updateOverwrite(member.user, {
VIEW_CHANNEL: false,
SEND_MESSAGES: false,
ATTACH_FILES: false,
READ_MESSAGE_HISTORY: false
});
if(channel.id !== message.channel.id)
channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(member.user.username, member.user.displayAvatarURL())
.setTitle('**Member remove**')
.setDescription(`${member} has been removed by ${message.author}`)
.setFooter(message.guild.name, message.guild.iconURL())
);
message.channel.send(
new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(member.user.username, member.user.displayAvatarURL())
.setTitle(':white_check_mark: **Member removed**')
.setDescription(`${member} has been removed from <#${ticket.get('channel')}>`)
.setFooter(message.guild.name, message.guild.iconURL())
);
log.info(`${message.author.tag} removed a user from a ticket (#${message.channel.id})`);
} catch (error) {
log.error(error);
}
// command ends here
},
};

114
src/commands/tickets.js Normal file
View File

@@ -0,0 +1,114 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const Discord = require('discord.js');
const fs = require('fs');
const config = require('../../user/config');
module.exports = {
name: 'tickets',
description: 'List your recent tickets to access transcripts / archives.',
usage: '[@member]',
aliases: ['list'],
example: '',
args: false,
async execute(client, message, args, Ticket) {
const supportRole = message.guild.roles.cache.get(config.staff_role);
if (!supportRole)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setTitle(':x: **Error**')
.setDescription(`${config.name} has not been set up correctly. Could not find a 'support team' role with the id \`${config.staff_role}\``)
.setFooter(message.guild.name, message.guild.iconURL())
);
let context;
let user = message.mentions.users.first() || message.guild.members.cache.get(args[0]);
if(!user) {
if(!message.member.roles.cache.has(config.staff_role))
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **No permission**')
.setDescription('You don\'t have permission to list others\' tickets as you are not staff.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
user = message.author;
context = 'staff';
}
context = 'self';
let openTickets = await Ticket.findAndCountAll({
where: {
creator: user.id,
open: true
}
});
let closedTickets = await Ticket.findAndCountAll({
where: {
creator: user.id,
open: false
}
});
closedTickets.rows = closedTickets.rows.slice(-10); // get most recent 10
let embed = new Discord.MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle('Your tickets')
.setFooter(message.guild.name + ' | This message will be deleted in 60 seconds', message.guild.iconURL());
if(config.transcripts.web.enabled)
embed.setDescription(`You can access all of your ticket archives on the [web portal](${config.transcripts.web.server}/${user.id}).`);
let open = [],
closed = [];
for (let t in openTickets.rows) {
let desc = openTickets.rows[t].topic.substring(0, 30);
open.push(`> <#${openTickets.rows[t].channel}>: \`${desc}${desc.length > 20 ? '...' : ''}\``);
}
for (let t in closedTickets.rows) {
let desc = closedTickets.rows[t].topic.substring(0, 30);
let transcript = '';
if(fs.existsSync(`user/transcripts/text/${closedTickets.rows[t].channel}.txt`))
transcript = `\n> Type \`${config.prefix}transcript ${closedTickets.rows[t].id}\` to download text transcript.`;
closed.push(`> #${closedTickets.rows[t].id}: \`${desc}${desc.length > 20 ? '...' : ''}\`${transcript}`);
}
let pre = context === 'self' ? 'You have' : user + ' has';
embed.addField('Open tickets', openTickets.count === 0 ? `${pre} no open tickets.` : open.join('\n\n'), false);
embed.addField('Closed tickets', closedTickets.count === 0 ? `${pre} no old tickets` : closed.join('\n\n'), false);
let m = await message.channel.send(embed);
return setTimeout(async () => {
await message.delete();
await m.delete();
}, 60000);
},
};

View File

@@ -0,0 +1,23 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const Discord = require('discord.js');
const fs = require('fs');
const config = require('../../user/config');
module.exports = {
name: 'transcript',
description: 'Download a transcript',
usage: '<ticket-id>',
aliases: ['archive', 'download'],
example: 'transcript 57',
args: false,
async execute(client, message, args, Ticket) {
/** @TODO TRY TO SEND ATTACHMENT TO DM */
}
};

17
src/events/debug.js Normal file
View File

@@ -0,0 +1,17 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
event: 'debug',
execute(client, e) {
log.debug(e);
}
};

17
src/events/error.js Normal file
View File

@@ -0,0 +1,17 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
event: 'error',
execute(client, e) {
log.error(e);
}
};

87
src/events/message.js Normal file
View File

@@ -0,0 +1,87 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const Discord = require('discord.js');
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const config = require('../../user/config');
module.exports = {
event: 'message',
async execute(client, message, Ticket) {
if (message.author.bot || message.author.id === client.user.id) return;
if (message.channel.type === 'dm') {
log.console(`Received a DM from ${message.author.tag}: ${message.cleanContent}`);
return message.channel.send(`Hello there, ${message.author.username}!
I am the support bot for **${client.guilds.cache.get(config.guild)}**.
Type \`${config.prefix}new\` on the server to create a new ticket.`);
}
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|\\${config.prefix})\\s*`);
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();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command || commandName === 'none') return;
if (command.permission && !message.member.hasPermission(command.permission)) {
log.console(`${message.author.tag} tried to use the '${command.name}' command without permission`);
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setTitle(':x: No permission')
.setDescription(`**You do not have permission to use the \`${command.name}\` command** (requires \`${command.permission}\`).`)
.setFooter(message.guild.name, message.guild.iconURL())
);
}
if (command.args && !args.length)
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.addField('Usage', `\`${config.prefix}${command.name} ${command.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${command.name}\` for more information`)
.setFooter(message.guild.name, message.guild.iconURL())
);
if (!client.cooldowns.has(command.name)) client.cooldowns.set(command.name, new Discord.Collection());
const now = Date.now();
const timestamps = client.cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || config.cooldown) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
log.console(`${message.author.tag} attempted to use the '${command.name}' command before the cooldown was over`);
return message.channel.send(
new Discord.MessageEmbed()
.setColor(config.err_colour)
.setDescription(`:x: Please wait ${timeLeft.toFixed(1)} second(s) before reusing the \`${command.name}\` command.`)
.setFooter(message.guild.name, message.guild.iconURL())
);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(client, message, args, Ticket);
log.console(`${message.author.tag} used the '${command.name}' command`);
} catch (error) {
log.warn(`An error occurred whilst executing the '${command.name}' command`);
log.error(error);
message.channel.send(`:x: An error occurred whilst executing the \`${command.name}\` command.\nThe issue has been reported.`);
}
}
};

18
src/events/rateLimit.js Normal file
View File

@@ -0,0 +1,18 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
event: 'rateLimit',
execute(client, limit) {
log.warn('Rate-limited!');
log.debug(limit);
}
};

42
src/events/ready.js Normal file
View File

@@ -0,0 +1,42 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const config = require('../../user/config');
module.exports = {
event: 'ready',
execute(client) {
log.success(`Authenticated as ${client.user.tag}`);
const updatePresence = () => {
let num = Math.floor(Math.random() * config.activities.length);
client.user.setPresence({
activity: {
name: config.activities[num] + ` | ${config.prefix}help`,
type: config.activity_types[num]
}
}).catch(log.error);
log.debug(`Updated presence: ${config.activity_types[num]} ${config.activities[num]}`);
};
updatePresence();
setInterval(() => {
updatePresence();
}, 15000);
if (client.guilds.cache.get(config.guild).member(client.user).hasPermission('ADMINISTRATOR', false))
log.success('\'ADMINISTRATOR\' permission has been granted');
else
log.warn('Bot does not have \'ADMINISTRATOR\' permission');
}
};

17
src/events/warn.js Normal file
View File

@@ -0,0 +1,17 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports = {
event: 'warn',
execute(client, e) {
log.warn(e);
}
};

99
src/index.js Normal file
View File

@@ -0,0 +1,99 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
require('dotenv').config({path: 'user/.env'});
const Discord = require('discord.js');
const fs = require('fs');
const leeks = require('leeks.js');
const client = new Discord.Client({
autoReconnect: true
});
client.events = new Discord.Collection();
client.commands = new Discord.Collection();
client.cooldowns = new Discord.Collection();
require('./utils/banner')(leeks); // big coloured text thing
const config = require('../user/config');
const Logger = require('leekslazylogger');
const log = new Logger({
name: config.name,
logToFile: config.logs.files.enabled,
maxAge: config.logs.files.keep_for,
debug: config.debug
});
log.multi(log); // required to allow other files to access the logger
require('./utils/updater')(); // check for updates
/**
* storage
*/
const { Sequelize, Model, DataTypes } = require('sequelize');
let sequelize;
if(config.storage.type === 'mysql') {
log.info('Connecting to MySQL database...');
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
dialect: 'mysql',
host: process.env.DB_HOST,
logging: log.debug
});
} else {
log.info('Using SQLite storage');
sequelize = new Sequelize({
dialect: 'sqlite',
storage: 'user/storage.db',
logging: log.debug
});
}
class Ticket extends Model {}
Ticket.init({
channel: DataTypes.STRING,
creator: DataTypes.STRING,
open: DataTypes.BOOLEAN,
topic: DataTypes.TEXT
}, {
sequelize,
modelName: 'ticket'
});
Ticket.sync();
/**
* event loader
*/
const events = fs.readdirSync('src/events').filter(file => file.endsWith('.js'));
for (const file of events) {
const event = require(`./events/${file}`);
client.events.set(event.event, event);
client.on(event.event, e => client.events.get(event.event).execute(client, e, Ticket));
log.console(log.format(`> Loaded &7${event.event}&f event`));
}
/**
* command loader
*/
const commands = fs.readdirSync('src/commands').filter(file => file.endsWith('.js'));
for (const file of commands) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
log.console(log.format(`> Loaded &7${config.prefix}${command.name}&f command`));
}
log.info(`Loaded ${events.length} events and ${commands.length} commands`);
process.on('unhandledRejection', error => {
log.warn('An error was not caught');
log.error(`Uncaught error: \n${error.stack}`);
});
client.login(process.env.TOKEN);

22
src/utils/archive.js Normal file
View File

@@ -0,0 +1,22 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
module.exports.create = (client, channel) => {
};
module.exports.addUser = (client, channel, user) => {
};
module.exports.addMessage = (client, channel, message) => {
};

31
src/utils/banner.js Normal file
View File

@@ -0,0 +1,31 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const { version, homepage } = require('../../package.json');
module.exports = (leeks) => {
console.log(leeks.colours.cyan(`
######## #### ###### ###### ####### ######## ########
## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ###### ## ## ## ######## ## ##
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ##
######## #### ###### ###### ####### ## ## ########
######## #### ###### ## ## ######## ######## ######
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ##### ###### ## ######
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
## #### ###### ## ## ######## ## ######
`));
console.log(leeks.colours.cyanBright(`DiscordTickets bot v${version} by eartharoid`));
console.log(leeks.colours.cyanBright(homepage));
console.log('\n\n');
};

31
src/utils/updater.js Normal file
View File

@@ -0,0 +1,31 @@
/**
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const fetch = require('node-fetch');
const config = require('../../user/config');
let {version} = require('../../package.json');
version = 'v' + version;
module.exports = () => {
if(!config.updater)
return;
fetch('https://api.github.com/repos/eartharoid/DiscordTickets/releases')
.then(res => res.json())
.then(json => {
const update = json[0];
if (version !== update.tag_name) {
log.notice('There is an update available for Discord Tickets');
log.info(`Download "&f${update.name}&3" from &6https://github.com/eartharoid/DiscordTickets/releases/`);
log.notice(`You currently have ${version}; The latest is ${update.tag_name}`);
}
});
};