From cdaf1d39cd53d74ac7594e715702d9e0c2f3b19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20Olausson=20Holten=C3=A4s?= Date: Sun, 6 Mar 2022 09:54:11 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Version=202.2.0=20released?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.json.example | 3 +- deploy-commands.js | 38 ++++++------- package.json | 2 +- src/commands/credits/addons/set.js | 38 +++++++++++++ src/commands/credits/addons/transfer.js | 72 +++++++++++++++++++++++++ src/commands/credits/index.js | 59 +++++++++++++++----- src/events/messageCreate.js | 2 +- src/index.js | 1 + 8 files changed, 182 insertions(+), 33 deletions(-) create mode 100644 src/commands/credits/addons/set.js create mode 100644 src/commands/credits/addons/transfer.js diff --git a/config.json.example b/config.json.example index 748cd41..aff4be3 100644 --- a/config.json.example +++ b/config.json.example @@ -9,7 +9,8 @@ "token": "required", "timeout": "5000", "rate": "1", - "minimumLength": "5" + "minimumLength": "5", + "excludedChannels": ["channel_id", "channel_id"] }, "colors": { "success": "0x22bb33", "error": "0xbb2124", "wait": "0xf0ad4e" }, "mongodb": { diff --git a/deploy-commands.js b/deploy-commands.js index 19a30b8..45cd34f 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -1,22 +1,24 @@ -__basedir = __dirname; -__config = require(`${__basedir}/config.json`); -const fs = require('node:fs'); -const { REST } = require('@discordjs/rest'); -const { Routes } = require('discord-api-types/v9'); +// __basedir = __dirname; +// __config = require(`${__basedir}/config.json`); +module.exports = async () => { + const fs = require('node:fs'); + const { REST } = require('@discordjs/rest'); + const { Routes } = require('discord-api-types/v9'); -const commands = []; -const commandFiles = fs.readdirSync(`${__basedir}/src/commands`); + const commands = []; + const commandFiles = fs.readdirSync(`${__basedir}/commands`); -for (const file of commandFiles) { - const command = require(`${__basedir}/src/commands/${file}`); - commands.push(command.data.toJSON()); -} + for (const file of commandFiles) { + const command = require(`${__basedir}/commands/${file}`); + commands.push(command.data.toJSON()); + } -const rest = new REST({ version: '9' }).setToken(__config.bot.token); + const rest = new REST({ version: '9' }).setToken(__config.bot.token); -rest - .put(Routes.applicationGuildCommands(__config.bot.clientId, __config.bot.guildId), { - body: commands, - }) - .then(() => console.log('Successfully registered application commands.')) - .catch(console.error); + rest + .put(Routes.applicationGuildCommands(__config.bot.clientId, __config.bot.guildId), { + body: commands, + }) + .then(() => console.log('Successfully registered application commands.')) + .catch(console.error); +}; diff --git a/package.json b/package.json index ab8d429..bc3463d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xyter", - "version": "2.1.0", + "version": "2.2.0", "description": "Earn credits while chatting! And more", "main": "src/index.js", "scripts": { diff --git a/src/commands/credits/addons/set.js b/src/commands/credits/addons/set.js new file mode 100644 index 0000000..b0ebd27 --- /dev/null +++ b/src/commands/credits/addons/set.js @@ -0,0 +1,38 @@ +const { Permissions } = require('discord.js'); + +const credits = require(`${__basedir}/helpers/database/models/creditSchema`); +const logger = require(`${__basedir}/handlers/logger`); +const creditNoun = require(`${__basedir}/helpers/creditNoun`); + +module.exports = async (interaction) => { + if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) { + const embed = { + title: 'Set', + description: 'You need to have permission to manage this guild (MANAGE_GUILD)', + color: 0xbb2124, + timestamp: new Date(), + footer: { iconURL: __config.footer.icon, text: __config.footer.text }, + }; + return await interaction.editReply({ embeds: [embed], ephemeral: true }); + } + const user = await interaction.options.getUser('user'); + const amount = await interaction.options.getInteger('amount'); + + const toUser = await credits.findOne({ userId: user.id }); + toUser.balance = amount; + await toUser.save(); + + const embed = { + title: 'Set', + description: `You set ${creditNoun(amount)} on ${user}.`, + color: 0x22bb33, + timestamp: new Date(), + footer: { iconURL: __config.footer.icon, text: __config.footer.text }, + }; + await logger.debug( + `Administrator: ${interaction.user.username} set ${ + amount <= 1 ? `${amount} credit` : `${amount} credits` + } on ${user.username}` + ); + return await interaction.editReply({ embeds: [embed], ephemeral: true }); +}; diff --git a/src/commands/credits/addons/transfer.js b/src/commands/credits/addons/transfer.js new file mode 100644 index 0000000..62edfa1 --- /dev/null +++ b/src/commands/credits/addons/transfer.js @@ -0,0 +1,72 @@ +const { Permissions } = require('discord.js'); + +const credits = require(`${__basedir}/helpers/database/models/creditSchema`); +const logger = require(`${__basedir}/handlers/logger`); +const saveUser = require(`${__basedir}/helpers/saveUser`); +const creditNoun = require(`${__basedir}/helpers/creditNoun`); + +module.exports = async (interaction) => { + try { + if (!interaction.member.permissions.has(Permissions.FLAGS.MANAGE_GUILD)) { + const embed = { + title: 'Transfer failed', + description: 'You need to have permission to manage this guild (MANAGE_GUILD)', + }; + return await interaction.editReply({ embeds: [embed], ephemeral: true }); + } + + const from = await interaction.options.getUser('from'); + const to = await interaction.options.getUser('to'); + const amount = await interaction.options.getInteger('amount'); + const data = await credits.findOne({ userId: from.id }); + + if (amount <= 0) { + const embed = { + title: 'Transfer failed', + description: "You can't transfer zero or below.", + color: 0xbb2124, + timestamp: new Date(), + footer: { iconURL: __config.footer.icon, text: __config.footer.text }, + }; + return await interaction.editReply({ embeds: [embed], ephemeral: true }); + } else { + if (data.balance < amount) { + const embed = { + title: 'Transfer', + description: `${from.username} has insufficient credits. ${from.username} balance is ${data.balance}`, + color: 0xbb2124, + timestamp: new Date(), + footer: { iconURL: __config.footer.icon, text: __config.footer.text }, + }; + return await interaction.editReply({ embeds: [embed], ephemeral: true }); + } else { + const fromUser = await credits.findOne({ userId: from.id }); + const toUser = await credits.findOne({ userId: to.id }); + + console.log(fromUser, toUser, amount); + + fromUser.balance -= amount; + toUser.balance += amount; + + await saveUser(fromUser, toUser); + + const embed = { + title: 'Transfer', + description: `You sent ${creditNoun(amount)} from ${from} to ${to}.`, + color: 0x22bb33, + fields: [ + { name: `${from.username} Balance`, value: `${fromUser.balance}`, inline: true }, + { name: `${to.username} Balance`, value: `${toUser.balance}`, inline: true }, + ], + timestamp: new Date(), + footer: { iconURL: __config.footer.icon, text: __config.footer.text }, + }; + + await logger.debug(`Gift sent from: ${interaction.user.username} to: ${to.username}`); + return await interaction.editReply({ embeds: [embed], ephemeral: true }); + } + } + } catch { + async (err) => await logger.error(err); + } +}; diff --git a/src/commands/credits/index.js b/src/commands/credits/index.js index 0fa9b20..f6b797d 100644 --- a/src/commands/credits/index.js +++ b/src/commands/credits/index.js @@ -7,6 +7,8 @@ const give = require('./addons/give.js'); const redeem = require('./addons/redeem.js'); const take = require('./addons/take.js'); const top = require('./addons/top.js'); +const transfer = require('./addons/transfer.js'); +const set = require('./addons/set.js'); module.exports = { permissions: new Permissions([ @@ -21,7 +23,7 @@ module.exports = { .addSubcommand((subcommand) => subcommand .setName('give') - .setDescription('Give credits to a user.') + .setDescription('Give credits to a user. (ADMIN)') .addUserOption((option) => option.setName('user').setDescription('The user you want to pay.').setRequired(true) ) @@ -32,7 +34,7 @@ module.exports = { .addSubcommand((subcommand) => subcommand .setName('take') - .setDescription('Take credits from a user.') + .setDescription('Take credits from a user. (ADMIN)') .addUserOption((option) => option .setName('user') @@ -75,25 +77,58 @@ module.exports = { ) .addSubcommand((subcommand) => subcommand.setName('top').setDescription('Check the top balance.') + ) + .addSubcommand((subcommand) => + subcommand + .setName('transfer') + .setDescription('Transfer credits from a user to another user. (ADMIN)') + .addUserOption((option) => + option + .setName('from') + .setDescription('The user you want to take credits from.') + .setRequired(true) + ) + .addUserOption((option) => + option + .setName('to') + .setDescription('The user you want to give credits to.') + .setRequired(true) + ) + .addIntegerOption((option) => + option.setName('amount').setDescription('The amount you will transfer.').setRequired(true) + ) + ) + .addSubcommand((subcommand) => + subcommand + .setName('set') + .setDescription('Set credits on a user. (ADMIN)') + .addUserOption((option) => + option + .setName('user') + .setDescription('The user you want to set credits on.') + .setRequired(true) + ) + .addIntegerOption((option) => + option.setName('amount').setDescription('The amount you will set.').setRequired(true) + ) ), async execute(interaction) { if (interaction.options.getSubcommand() === 'balance') { await balance(interaction); - } - else if (interaction.options.getSubcommand() === 'gift') { + } else if (interaction.options.getSubcommand() === 'gift') { await gift(interaction); - } - else if (interaction.options.getSubcommand() === 'give') { + } else if (interaction.options.getSubcommand() === 'give') { await give(interaction); - } - else if (interaction.options.getSubcommand() === 'redeem') { + } else if (interaction.options.getSubcommand() === 'redeem') { await redeem(interaction); - } - else if (interaction.options.getSubcommand() === 'take') { + } else if (interaction.options.getSubcommand() === 'take') { await take(interaction); - } - else if (interaction.options.getSubcommand() === 'top') { + } else if (interaction.options.getSubcommand() === 'top') { await top(interaction); + } else if (interaction.options.getSubcommand() === 'transfer') { + await transfer(interaction); + } else if (interaction.options.getSubcommand() === 'set') { + await set(interaction); } }, }; diff --git a/src/events/messageCreate.js b/src/events/messageCreate.js index a4a5fcf..cfe80c1 100644 --- a/src/events/messageCreate.js +++ b/src/events/messageCreate.js @@ -9,7 +9,7 @@ module.exports = { async execute(message) { if (message.author.bot) return; if (message.content.length < __config.credits.minimumLength) return; - + if (__config.credits.excludedChannels.includes(message.channel.id)) return; if (!talkedRecently.has(message.author.id)) { await credits .findOneAndUpdate( diff --git a/src/index.js b/src/index.js index 3c39a68..b01ed1e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ __basedir = __dirname; __config = require(`${__basedir}/../config.json`); +require('../deploy-commands')(); require('./helpers/database')(); const fs = require('node:fs');