From 5e9e23bbf21b18da5950934b84395b7cec48f1c3 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sat, 20 Feb 2021 17:09:08 +0000 Subject: [PATCH] progress --- package.json | 2 +- src/commands/new.js | 11 +++++------ src/database/index.js | 4 ++++ src/index.js | 11 ++++++++--- src/listeners/guildCreate.js | 7 +++++++ src/listeners/guildDelete.js | 7 +++++++ src/listeners/interaction.js | 4 +--- src/locales/en-GB.json | 10 +++++++++- src/modules/commands/command.js | 4 +++- src/modules/commands/manager.js | 9 +++++++-- src/modules/structures.js | 10 ++++++++++ src/structures/guild.js | 13 ++++++++++++- 12 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 src/listeners/guildCreate.js create mode 100644 src/listeners/guildDelete.js create mode 100644 src/modules/structures.js diff --git a/package.json b/package.json index 8a8c699..2edd958 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@eartharoid/discordtickets", "version": "3.0.0", "private": true, - "description": "An open-source & self-hosted Discord bot for ticket management.", + "description": "An open-source Discord bot for ticket management", "main": "src/index.js", "scripts": { "start": "node src/", diff --git a/src/commands/new.js b/src/commands/new.js index 98bfa1a..600e4ac 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -28,14 +28,13 @@ module.exports = class NewCommand extends Command { } async execute({ guild, member, channel, args}, interaction) { - console.log(args); - // console.log(channel.name); - // console.log(member.user.tag); - // console.log(guild.name); - const i18n = this.client.i18n.get(/* GET GUILD LOCALE FROM SETTINGS */); + let settings = await guild.settings; + const i18n = this.client.i18n.get(settings.locale); return new MessageEmbed() - .setTitle(i18n('bot.version', require('../../package.json').version)); + .setColor(settings.colour) + .setTitle(i18n('bot.version', require('../../package.json').version)) + .secret = true; } }; \ No newline at end of file diff --git a/src/database/index.js b/src/database/index.js index 802f192..bc19024 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -66,6 +66,10 @@ module.exports = async (log) => { primaryKey: true, allowNull: false, }, + colour: { + type: DataTypes.STRING, + defaultValue: config.defaults.colour + }, locale: { type: DataTypes.STRING, defaultValue: config.defaults.locale diff --git a/src/index.js b/src/index.js index af9535c..8a88ee6 100644 --- a/src/index.js +++ b/src/index.js @@ -16,7 +16,7 @@ * along with this program. If not, see . * * @name @eartharoid/discordtickets - * @description An open-source & self-hosted Discord bot for ticket management. + * @description An open-source Discord bot for ticket management * @copyright 2021 Isaac Saunders * @license GNU-GPLv3 */ @@ -52,8 +52,6 @@ if (!checkFile('./.env', './example.env')) { checkFile('./user/config.js', './user/example.config.js'); - - require('dotenv').config({ path: path('./.env') }); @@ -67,6 +65,7 @@ const log = new Logger({ name: 'DiscordTickets by eartharoid', debug: config.debug, logToFile: config.logs.enabled, + directory: path('./logs/'), keepFor: config.logs.keep_for, custom: { commands: { @@ -76,6 +75,10 @@ const log = new Logger({ plugins: { title: 'info', prefix: 'plugins' + }, + tickets: { + title: 'info', + prefix: 'tickets' } } }); @@ -85,6 +88,8 @@ const I18n = require('@eartharoid/i18n'); const { CommandManager } = require('./modules/commands'); const { PluginManager } = require('./modules/plugins'); +require('./modules/structures')(); // load extended structures before creating the client + const { Client, Intents diff --git a/src/listeners/guildCreate.js b/src/listeners/guildCreate.js new file mode 100644 index 0000000..22144bc --- /dev/null +++ b/src/listeners/guildCreate.js @@ -0,0 +1,7 @@ +module.exports = { + event: 'guildCreate', + execute: async (client, guild) => { + client.log.console(`Added to ${guild.name}`); + await guild.createSettings(); + } +}; \ No newline at end of file diff --git a/src/listeners/guildDelete.js b/src/listeners/guildDelete.js new file mode 100644 index 0000000..f2906e7 --- /dev/null +++ b/src/listeners/guildDelete.js @@ -0,0 +1,7 @@ +module.exports = { + event: 'guildDelete', + execute: async (client, guild) => { + client.log.console(`Removed from ${guild.name}`); + await guild.deleteSettings(); + } +}; \ No newline at end of file diff --git a/src/listeners/interaction.js b/src/listeners/interaction.js index eaa108b..97bff22 100644 --- a/src/listeners/interaction.js +++ b/src/listeners/interaction.js @@ -8,7 +8,7 @@ module.exports = { client.log.debug('Received interaction ping, responding with pong'); await client.api.interactions(interaction.id, interaction.token).callback.post({ data: { - type: 1, + type: 1, // PONG } }); break; @@ -17,7 +17,5 @@ module.exports = { break; } - - } }; \ No newline at end of file diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index c2b360d..f8aae76 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -1,5 +1,13 @@ { + "admin_only": "❌ You must be an administrator to use this command.", "bot": { "version": "DiscordTickets v%s by eartharoid" - } + }, + "cmd": { + "new": { + + } + }, + "no_perm": "❌ You do not have the permissions required to use this command:\n%s", + "support_only": "❌ You must be a member of staff to use this command." } \ No newline at end of file diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index 7bc4f48..30f1ed8 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -177,7 +177,9 @@ module.exports = class Command { data: { type: 4, flags: flags(secret), - content + data: { + content + } } }); } diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index e24068d..a133563 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -140,7 +140,7 @@ module.exports = class CommandManager { /** * Execute a command * @param {string} cmd_name - Name of the command - * @param {interaction} interaction - Command interaction + * @param {Interaction} interaction - Command interaction */ async execute(cmd_name, interaction) { if (!this.commands.has(cmd_name)) @@ -157,6 +157,11 @@ module.exports = class CommandManager { const cmd = this.commands.get(cmd_name); + let settings = await data.guild.settings; + if (!settings) + settings = await data.guild.createSettings(); + const i18n = this.client.i18n.get(settings.locale); + // if (cmd.staff_only) { // return await cmd.sendResponse(interaction, msg, true); // } @@ -165,7 +170,7 @@ module.exports = class CommandManager { && !data.member.hasPermission(cmd.permissions); if (no_perm) { let perms = cmd.permissions.map(p => `\`${p}\``).join(', '); - let msg = `❌ You do not have the permissions required to use this command:\n${perms}`; + let msg = i18n('no_perm', perms); return await cmd.sendResponse(interaction, msg, true); } diff --git a/src/modules/structures.js b/src/modules/structures.js new file mode 100644 index 0000000..5789881 --- /dev/null +++ b/src/modules/structures.js @@ -0,0 +1,10 @@ +const fs = require('fs'); +const { path } = require('../utils/fs'); + +module.exports = () => { + const files = fs.readdirSync(path('./src/structures')) + .filter(file => file.endsWith('.js')); + + for (const file of files) + require(`../structures/${file}`); +}; \ No newline at end of file diff --git a/src/structures/guild.js b/src/structures/guild.js index ec9f6b4..914b2eb 100644 --- a/src/structures/guild.js +++ b/src/structures/guild.js @@ -6,8 +6,19 @@ Structures.extend('Guild', Guild => { super(client, data); } + createSettings() { + return this.client.db.models.Guild.create({ + id: this.id + }); + } + + async deleteSettings() { + let row = await this.settings; + return await row.destroy(); + } + get settings() { - return this.client.db.Guild.findOne({ + return this.client.db.models.Guild.findOne({ where: { id: this.id }