This commit is contained in:
Isaac (eartharoid)
2020-08-17 22:07:05 +01:00
parent 49631320b1
commit 5ed96f33d5
17 changed files with 242 additions and 315 deletions

View File

@@ -6,85 +6,60 @@
*
*/
const Discord = require('discord.js');
const { Collection, MessageEmbed } = require('discord.js');
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const config = require('../../user/config');
const fs = require('fs');
const dtf = require('@eartharoid/dtf');
const archive = require('../utils/archive');
module.exports = {
event: 'message',
async execute(client, [message], {Ticket, Setting}) {
const guild = client.guilds.cache.get(config.guild);
if (message.author.bot || message.author.id === client.user.id) return;
if (message.channel.type === 'dm') {
if (message.channel.type === 'dm' && !message.author.bot) {
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 **${guild}**.
Type \`${config.prefix}new\` on the server to create a new ticket.`);
}
} // stop here if is DM
/**
* Ticket transcripts
* (bots currently still allowed)
*/
let ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(ticket)
archive.addMessage(client, message);
if (message.author.bot || message.author.id === client.user.id) return; // if bot, fuck off
/**
* Command handler
* (no bots / self)
*/
const regex = new RegExp(`^(<@!?${client.user.id}>|\\${config.prefix})\\s*`);
if (!regex.test(message.content)) return; // not a command
const [, prefix] = message.content.match(regex);
const args = message.content.slice(prefix.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; // not an existing command
if (message.guild.id !== guild.id)
return message.reply(`This bot can only be used within the "${guild}" server`);
/**
*
* Ticket transcripts
*
*/
let ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(ticket) {
if(config.transcripts.text.enabled) {
let path = `user/transcripts/text/${message.channel.id}.txt`;
let time = dtf('HH:mm:ss n_D MMM YY', message.createdAt),
name = message.author.tag,
msg = message.cleanContent;
let string = `[${time}] [${name}] :> ${msg}`;
fs.appendFileSync(path, string + '\n');
}
if(config.transcripts.web.enabled) {
let path = `user/transcripts/raw/${message.channel.id}.log`;
let embeds = [];
for (let embed in message.embeds)
embeds.push(message.embeds[embed].toJSON());
fs.appendFileSync(path, JSON.stringify({
id: message.id,
type: 'UNKNOWN',
author: message.author.id,
content: message.content,
// deleted: false,
time: message.createdTimestamp,
embeds: embeds,
attachments: [...message.attachments]
}) + ', \n');
}
}
/**
*
* Command handler
*
*/
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;
return message.reply(`This bot can only be used within the "${guild}" server`); // not in this server
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()
new MessageEmbed()
.setColor(config.err_colour)
.setTitle(':x: No permission')
.setDescription(`**You do not have permission to use the \`${command.name}\` command** (requires \`${command.permission}\`).`)
@@ -94,14 +69,14 @@ Type \`${config.prefix}new\` on the server to create a new ticket.`);
if (command.args && !args.length)
return message.channel.send(
new Discord.MessageEmbed()
new 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(guild.name, guild.iconURL())
);
if (!client.cooldowns.has(command.name)) client.cooldowns.set(command.name, new Discord.Collection());
if (!client.cooldowns.has(command.name)) client.cooldowns.set(command.name, new Collection());
const now = Date.now();
const timestamps = client.cooldowns.get(command.name);
@@ -114,7 +89,7 @@ Type \`${config.prefix}new\` on the server to create a new ticket.`);
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()
new MessageEmbed()
.setColor(config.err_colour)
.setDescription(`:x: Please wait ${timeLeft.toFixed(1)} second(s) before reusing the \`${command.name}\` command.`)
.setFooter(guild.name, guild.iconURL())
@@ -131,7 +106,7 @@ Type \`${config.prefix}new\` on the server to create a new ticket.`);
} 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.`);
message.channel.send(`:x: An error occurred whilst executing the \`${command.name}\` command.`);
}
}
};

View File

@@ -0,0 +1,50 @@
/**
*
* @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');
const fs = require('fs');
const dtf = require('@eartharoid/dtf');
module.exports = {
event: 'messageDelete',
async execute(client, [message], {Ticket}) {
if(!config.transcripts.web.enabled) return;
if (message.partial)
try {
await message.fetch();
} catch (err) {
log.error(err);
return;
}
let ticket = await Ticket.findOne({ where: { channel: message.channel.id } });
if(!ticket) return;
let path = `user/transcripts/raw/${message.channel.id}.log`;
let embeds = [];
for (let embed in message.embeds)
embeds.push(message.embeds[embed].toJSON());
fs.appendFileSync(path, JSON.stringify({
id: message.id,
author: message.author.id,
content: message.content, // do not use cleanContent!
time: message.createdTimestamp,
embeds: embeds,
attachments: [...message.attachments.values()],
deleted: true
}) + '\n');
}
};

View File

@@ -8,7 +8,7 @@
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const Discord = require('discord.js');
const { MessageEmbed } = require('discord.js');
const config = require('../../user/config');
const fs = require('fs');
@@ -36,7 +36,7 @@ module.exports = {
const supportRole = channel.guild.roles.cache.get(config.staff_role);
if (!supportRole)
return channel.send(
new Discord.MessageEmbed()
new 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}\``)
@@ -68,7 +68,7 @@ module.exports = {
try {
return dm.send(
new Discord.MessageEmbed()
new MessageEmbed()
.setColor(config.err_colour)
.setAuthor(u.username, u.displayAvatarURL())
.setTitle(`:x: **You already have ${tickets.count} or more open tickets**`)
@@ -80,7 +80,7 @@ module.exports = {
} catch (e) {
let m = await channel.send(
new Discord.MessageEmbed()
new MessageEmbed()
.setColor(config.err_colour)
.setAuthor(u.username, u.displayAvatarURL())
.setTitle(`:x: **You already have ${tickets.count} or more open tickets**`)
@@ -166,7 +166,7 @@ module.exports = {
let w = await c.send(
new Discord.MessageEmbed()
new MessageEmbed()
.setColor(config.colour)
.setAuthor(u.username, u.displayAvatarURL())
.setDescription(text)
@@ -180,7 +180,7 @@ module.exports = {
if (config.logs.discord.enabled)
client.channels.cache.get(config.logs.discord.channel).send(
new Discord.MessageEmbed()
new MessageEmbed()
.setColor(config.colour)
.setAuthor(u.username, u.displayAvatarURL())
.setTitle('New ticket (via panel)')

View File

@@ -14,14 +14,47 @@ const fs = require('fs');
const dtf = require('@eartharoid/dtf');
module.exports = {
event: 'messageUpdate',
async execute(client, [o, n], {Ticket, Setting}) {
/**
* CHECK IF O === N
*/
console.log(o.author.tag);
// https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-messageUpdate
// append new to raw
event: 'oUpdate',
async execute(client, [o, n], {Ticket}) {
if(!config.transcripts.web.enabled) return;
if (o.partial)
try {
await o.fetch();
} catch (err) {
log.error(err);
return;
}
if (n.partial)
try {
await o.fetch();
} catch (err) {
log.error(err);
return;
}
if(o === n) return;
let ticket = await Ticket.findOne({ where: { channel: o.channel.id } });
if(!ticket) return;
let path = `user/transcripts/raw/${o.channel.id}.log`;
let embeds = [];
for (let embed in o.embeds)
embeds.push(o.embeds[embed].toJSON());
fs.appendFileSync(path, JSON.stringify({
id: o.id,
author: o.author.id,
content: o.content, // do not use cleanContent!
time: o.createdTimestamp,
embeds: embeds,
attachments: [...o.attachments.values()],
edited: true
}) + '\n');
}
};