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 */
}
};