Survey responses!

and some small changes/fixes
This commit is contained in:
Isaac
2021-05-19 15:24:02 +01:00
parent 053fcdb4b8
commit 20ac8bff73
9 changed files with 340 additions and 40 deletions

View File

@@ -82,7 +82,7 @@ module.exports = class BlacklistCommand extends Command {
const member_or_role = is_role ? 'role' : 'member';
const index = settings.blacklist.findIndex(element => element === id);
const new_blacklist = [ ...settings.blacklist ];
const new_blacklist = [...settings.blacklist];
if (index === -1) {
new_blacklist.push(id);

View File

@@ -0,0 +1,114 @@
{
"type": "object",
"properties": {
"categories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"claiming": {
"type": "boolean"
},
"image": {
"type": [
"string",
"null"
]
},
"max_per_member": {
"type": "number"
},
"name": {
"type": "string"
},
"name_format": {
"type": "string"
},
"opening_message": {
"type": "string"
},
"opening_questions": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"ping": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"require_topic": {
"type": "boolean"
},
"roles": {
"type": "array",
"items": {
"type": "string"
}
},
"survey": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"opening_message",
"roles"
]
}
},
"colour": {
"type": "string"
},
"command_prefix": {
"type": "string"
},
"error_colour": {
"type": "string"
},
"footer": {
"type": "string"
},
"locale": {
"type": "string"
},
"log_messages": {
"type": "boolean"
},
"success_colour": {
"type": "string"
},
"surveys": {
"type": "object"
},
"tags": {
"type": "object"
}
},
"required": [
"categories",
"colour",
"command_prefix",
"error_colour",
"footer",
"locale",
"log_messages",
"success_colour",
"surveys",
"tags"
]
}

View File

@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<title>{{survey}} Survey Responses | Discord Tickets</title>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css'>
<link rel='stylesheet' href='https://jenil.github.io/bulmaswatch/darkly/bulmaswatch.min.css'>
</head>
<body>
<section class='section'>
<container class='container box has-text-centered'>
<div class='content'>
<h1>{{survey}} survey responses</h1>
</div>
<div class='level'>
<div class='level-item has-text-centered'>
<div class='box'>
<p class='title'>{{count.responses}}</p>
<p class='heading'>Responses</p>
</div>
</div>
<div class='level-item has-text-centered'>
<div class='box'>
<p class='title'>{{count.users}}</p>
<p class='heading'>Users</p>
</div>
</div>
</div>
<div class='table-container'>
<table class='table is-bordered is-striped is-hoverable is-fullwidth'>
<thead>
<tr>
{{#columns}}
<th>{{.}}</th>
{{/columns}}
</tr>
</thead>
<tbody>
{{#responses}}
<tr>
{{#.}}
<td>{{.}}</td>
{{/.}}
</tr>
{{/responses}}
</tbody>
<tfoot>
<tr>
{{#columns}}
<th>{{.}}</th>
{{/columns}}
</tr>
</tfoot>
</table>
</div>
</container>
</section>
</body>
</html>

View File

@@ -19,8 +19,7 @@ module.exports = class SettingsCommand extends Command {
permissions: ['MANAGE_GUILD']
});
this.schema = require('../settings.schema.json');
this.schema = require('./extra/settings.schema.json');
this.v = new Validator();
}
@@ -33,7 +32,7 @@ module.exports = class SettingsCommand extends Command {
const settings = await message.guild.settings;
const i18n = this.client.i18n.getLocale(settings.locale);
const attachments = [ ...message.attachments.values() ];
const attachments = [...message.attachments.values()];
if (attachments.length >= 1) {
@@ -207,7 +206,7 @@ module.exports = class SettingsCommand extends Command {
`Settings for ${message.guild.name}.json`
);
message.channel.send({
return await message.channel.send({
files: [attachment]
});
}

112
src/commands/survey.js Normal file
View File

@@ -0,0 +1,112 @@
const Command = require('../modules/commands/command');
// eslint-disable-next-line no-unused-vars
const { MessageAttachment, MessageEmbed, Message } = require('discord.js');
const fsp = require('fs').promises;
const { path } = require('../utils/fs');
const mustache = require('mustache');
module.exports = class SurveyCommand extends Command {
constructor(client) {
const i18n = client.i18n.getLocale(client.config.locale);
super(client, {
internal: true,
name: i18n('commands.survey.name'),
description: i18n('commands.survey.description'),
aliases: [
i18n('commands.survey.aliases.surveys')
],
process_args: false,
args: [
{
name: i18n('commands.survey.args.survey.name'),
description: i18n('commands.survey.args.survey.description'),
example: i18n('commands.survey.args.survey.example'),
required: false,
}
],
staff_only: true
});
}
/**
* @param {Message} message
* @param {string} args
* @returns {Promise<void|any>}
*/
async execute(message, args) {
const settings = await message.guild.settings;
const i18n = this.client.i18n.getLocale(settings.locale);
const survey = await this.client.db.models.Survey.findOne({
where: {
name: args,
guild: message.guild.id
}
});
if (survey) {
const { rows: responses, count } = await this.client.db.models.SurveyResponse.findAndCountAll({
where: {
survey: survey.id
}
});
const users = new Set();
for (const i in responses) {
const ticket = await this.client.db.models.Ticket.findOne({
where: {
id: responses[i].ticket
}
});
users.add(ticket.creator);
const answers = responses[i].answers.map(a => this.client.cryptr.decrypt(a));
answers.unshift(ticket.number);
responses[i] = answers;
}
let template = await fsp.readFile(path('./src/commands/extra/survey.template.html'), {
encoding: 'utf8'
});
template = template.replace(/\n|\t/, '');
survey.questions.unshift('Ticket #');
const html = mustache.render(template, {
survey: survey.name.charAt(0).toUpperCase() + survey.name.slice(1),
count: {
responses: count,
users: users.size
},
columns: survey.questions,
responses
});
const attachment = new MessageAttachment(
Buffer.from(html),
`${survey.name}.html`
);
return await message.channel.send({
files: [attachment]
});
} else {
const surveys = await this.client.db.models.Survey.findAll({
where: {
guild: message.guild.id
}
});
const list = surveys.map(s => ` **\`${s.name}\`**`);
return await message.channel.send(
new MessageEmbed()
.setColor(settings.colour)
.setTitle(i18n('commands.survey.response.list.title'))
.setDescription(list.join('\n'))
.setFooter(settings.footer, message.guild.iconURL())
);
}
}
};