From 8417a1ba57c408c874d61e7d1306481b5e1005e1 Mon Sep 17 00:00:00 2001 From: WatDuhHekBro <44940783+WatDuhHekBro@users.noreply.github.com> Date: Wed, 4 Nov 2020 02:04:07 -0600 Subject: [PATCH] Split up the money command into several parts --- package.json | 3 +- prettier.config.js | 1 + src/commands/fun/eco.ts | 35 +++ src/commands/fun/subcommands/eco-core.ts | 180 ++++++++++++ .../fun/subcommands/eco-shop-items.ts | 0 src/commands/fun/subcommands/eco-shop.ts | 20 ++ src/commands/fun/subcommands/eco-utils.ts | 80 ++++++ src/commands/money.ts | 260 ------------------ 8 files changed, 318 insertions(+), 261 deletions(-) create mode 100644 src/commands/fun/eco.ts create mode 100644 src/commands/fun/subcommands/eco-core.ts create mode 100644 src/commands/fun/subcommands/eco-shop-items.ts create mode 100644 src/commands/fun/subcommands/eco-shop.ts create mode 100644 src/commands/fun/subcommands/eco-utils.ts delete mode 100644 src/commands/money.ts diff --git a/package.json b/package.json index bfc3fac..16d6760 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "start": "node dist/index.js", "once": "tsc && npm start", "dev": "tsc-watch --onSuccess \"node dist/index.js dev\"", - "test": "mocha --require ts-node/register --extension ts --recursive" + "test": "mocha --require ts-node/register --extension ts --recursive", + "format": "prettier --write **/*" }, "keywords": [ "discord.js", diff --git a/prettier.config.js b/prettier.config.js index 7584823..4cbaf1d 100644 --- a/prettier.config.js +++ b/prettier.config.js @@ -10,4 +10,5 @@ module.exports = { bracketSpacing: true, jsxBracketSameLine: true, arrowParens: 'always', + endOfLine: 'auto', }; diff --git a/src/commands/fun/eco.ts b/src/commands/fun/eco.ts new file mode 100644 index 0000000..ceab224 --- /dev/null +++ b/src/commands/fun/eco.ts @@ -0,0 +1,35 @@ +import Command from '../../core/command'; +import { isAuthorized, getMoneyEmbed } from './subcommands/eco-utils'; +import { DailyCommand, PayCommand, GuildCommand } from './subcommands/eco-core'; +import { BuyCommand, ShopCommand } from './subcommands/eco-shop'; + +export default new Command({ + description: 'Economy command for Monika.', + + async run({ guild, channel, author }) { + if (isAuthorized(guild, channel)) channel.send(getMoneyEmbed(author)); + }, + subcommands: { + daily: DailyCommand, + pay: PayCommand, + guild: GuildCommand, + buy: BuyCommand, + shop: ShopCommand, + }, + user: new Command({ + description: + 'See how much money someone else has by using their user ID or pinging them.', + async run({ guild, channel, args }) { + if (isAuthorized(guild, channel)) channel.send(getMoneyEmbed(args[0])); + }, + }), + any: new Command({ + description: 'See how much money someone else has by using their username.', + async run({ guild, channel, args, callMemberByUsername, message }) { + if (isAuthorized(guild, channel)) + callMemberByUsername(message, args.join(' '), (member) => { + channel.send(getMoneyEmbed(member.user)); + }); + }, + }), +}); diff --git a/src/commands/fun/subcommands/eco-core.ts b/src/commands/fun/subcommands/eco-core.ts new file mode 100644 index 0000000..e2c8c5c --- /dev/null +++ b/src/commands/fun/subcommands/eco-core.ts @@ -0,0 +1,180 @@ +import Command from '../../../core/command'; +import $ from '../../../core/lib'; +import { Storage } from '../../../core/structures'; +import { isAuthorized, getMoneyEmbed, getSendEmbed } from './eco-utils'; + +export const DailyCommand = new Command({ + description: + 'Pick up your daily Mons. The cooldown is per user and every 22 hours to allow for some leeway.', + async run({ author, channel, guild }) { + if (isAuthorized(guild, channel)) { + const user = Storage.getUser(author.id); + const now = Date.now(); + + if (now - user.lastReceived >= 79200000) { + user.money++; + user.lastReceived = now; + Storage.save(); + channel.send({ + embed: { + title: 'Daily Reward', + description: 'You received 1 Mon!', + color: 0xf1c40f, + }, + }); + } else + channel.send({ + embed: { + title: 'Daily Reward', + description: `It's too soon to pick up your daily credits. You have about ${( + (user.lastReceived + 79200000 - now) / + 3600000 + ).toFixed(1)} hours to go.`, + color: 0xf1c40f, + }, + }); + } + }, +}); + +export const GuildCommand = new Command({ + description: 'See the richest players.', + async run({ guild, channel, client }) { + if (isAuthorized(guild, channel)) { + const users = Storage.users; + const ids = Object.keys(users); + ids.sort((a, b) => users[b].money - users[a].money); + const fields = []; + + for (let i = 0, limit = Math.min(10, ids.length); i < limit; i++) { + const id = ids[i]; + const user = await client.users.fetch(id); + + fields.push({ + name: `#${i + 1}. ${user.username}#${user.discriminator}`, + value: $(users[id].money).pluralise('credit', 's'), + }); + } + + channel.send({ + embed: { + title: 'Top 10 Richest Players', + color: '#ffff00', + fields: fields, + }, + }); + } + }, +}); + +export const PayCommand = new Command({ + description: 'Send money to someone.', + usage: ' ', + run: 'Who are you sending this money to?', + user: new Command({ + run: "You need to enter an amount you're sending!", + number: new Command({ + async run({ args, author, channel, guild }): Promise { + if (isAuthorized(guild, channel)) { + const amount = Math.floor(args[1]); + const sender = Storage.getUser(author.id); + const target = args[0]; + const receiver = Storage.getUser(target.id); + + if (amount <= 0) + return channel.send('You must send at least one Mon!'); + else if (sender.money < amount) + return channel.send( + "You don't have enough Mons for that.", + getMoneyEmbed(author), + ); + else if (target.id === author.id) + return channel.send("You can't send Mons to yourself!"); + else if (target.bot && process.argv[2] !== 'dev') + return channel.send("You can't send Mons to a bot!"); + + sender.money -= amount; + receiver.money += amount; + Storage.save(); + return channel.send(getSendEmbed(author, target, amount)); + } + }, + }), + }), + number: new Command({ + run: 'You must use the format `money send `!', + }), + any: new Command({ + async run({ args, author, channel, guild, prompt }) { + if (isAuthorized(guild, channel)) { + const last = args.pop(); + + if (!/\d+/g.test(last) && args.length === 0) + return channel.send("You need to enter an amount you're sending!"); + + const amount = Math.floor(last); + const sender = Storage.getUser(author.id); + + if (amount <= 0) + return channel.send('You must send at least one credit!'); + else if (sender.money < amount) + return channel.send( + "You don't have enough money to do that!", + getMoneyEmbed(author), + ); + else if (!guild) + return channel.send( + 'You have to use this in a server if you want to send money with a username!', + ); + + const username = args.join(' '); + const member = ( + await guild.members.fetch({ + query: username, + limit: 1, + }) + ).first(); + + if (!member) + return channel.send( + `Couldn't find a user by the name of \`${username}\`! If you want to send money to someone in a different server, you have to use their user ID!`, + ); + else if (member.user.id === author.id) + return channel.send("You can't send money to yourself!"); + else if (member.user.bot && process.argv[2] !== 'dev') + return channel.send("You can't send money to a bot!"); + + const target = member.user; + + return prompt( + await channel.send( + `Are you sure you want to send ${$(amount).pluralise( + 'credit', + 's', + )} to this person?\n*(This message will automatically be deleted after 10 seconds.)*`, + { + embed: { + color: '#ffff00', + author: { + name: `${target.username}#${target.discriminator}`, + icon_url: target.displayAvatarURL({ + format: 'png', + dynamic: true, + }), + }, + }, + }, + ), + author.id, + () => { + const receiver = Storage.getUser(target.id); + sender.money -= amount; + receiver.money += amount; + Storage.save(); + channel.send(getSendEmbed(author, target, amount)); + }, + ); + } + }, + }), +}); diff --git a/src/commands/fun/subcommands/eco-shop-items.ts b/src/commands/fun/subcommands/eco-shop-items.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/fun/subcommands/eco-shop.ts b/src/commands/fun/subcommands/eco-shop.ts new file mode 100644 index 0000000..7bd4bd0 --- /dev/null +++ b/src/commands/fun/subcommands/eco-shop.ts @@ -0,0 +1,20 @@ +import Command from '../../../core/command'; +import $ from '../../../core/lib'; +import { Storage } from '../../../core/structures'; +import { isAuthorized, getMoneyEmbed, getSendEmbed } from './eco-utils'; + +export const BuyCommand = new Command({ + description: '', + async run({ guild, channel }) { + if (isAuthorized(guild, channel)) { + } + }, +}); + +export const ShopCommand = new Command({ + description: '', + async run({ guild, channel }) { + if (isAuthorized(guild, channel)) { + } + }, +}); diff --git a/src/commands/fun/subcommands/eco-utils.ts b/src/commands/fun/subcommands/eco-utils.ts new file mode 100644 index 0000000..7b5bafd --- /dev/null +++ b/src/commands/fun/subcommands/eco-utils.ts @@ -0,0 +1,80 @@ +import $ from '../../../core/lib'; +import { Storage } from '../../../core/structures'; +import { User, Guild, TextChannel, DMChannel, NewsChannel } from 'discord.js'; + +export function getMoneyEmbed(user: User): object { + const profile = Storage.getUser(user.id); + + return { + embed: { + color: 0xffff00, + author: { + name: user.username, + icon_url: user.displayAvatarURL({ + format: 'png', + dynamic: true, + }), + }, + fields: [ + { + name: 'Balance', + value: $(profile.money).pluralise('credit', 's'), + }, + ], + }, + }; +} + +export function getSendEmbed( + sender: User, + receiver: User, + amount: number, +): object { + return { + embed: { + color: 0xffff00, + author: { + name: sender.username, + icon_url: sender.displayAvatarURL({ + format: 'png', + dynamic: true, + }), + }, + title: 'Transaction', + description: `${sender.toString()} has sent ${$(amount).pluralise( + 'credit', + 's', + )} to ${receiver.toString()}!`, + fields: [ + { + name: `Sender: ${sender.username}#${sender.discriminator}`, + value: $(Storage.getUser(sender.id).money).pluralise('credit', 's'), + }, + { + name: `Receiver: ${receiver.username}#${receiver.discriminator}`, + value: $(Storage.getUser(receiver.id).money).pluralise('credit', 's'), + }, + ], + footer: { + text: receiver.username, + icon_url: receiver.displayAvatarURL({ + format: 'png', + dynamic: true, + }), + }, + }, + }; +} + +export function isAuthorized( + guild: Guild | null, + channel: TextChannel | DMChannel | NewsChannel, +): boolean { + if (guild?.id !== '637512823676600330') return true; + else { + channel.send( + "Sorry, this command can only be used in Monika's emote server.", + ); + return false; + } +} diff --git a/src/commands/money.ts b/src/commands/money.ts deleted file mode 100644 index 7abffb9..0000000 --- a/src/commands/money.ts +++ /dev/null @@ -1,260 +0,0 @@ -import Command from '../core/command'; -import $, { CommonLibrary } from '../core/lib'; -import { Storage } from '../core/structures'; -import { User } from 'discord.js'; - -export function getMoneyEmbed(user: User): object { - const profile = Storage.getUser(user.id); - - return { - embed: { - color: 0xffff00, - author: { - name: user.username, - icon_url: user.displayAvatarURL({ - format: 'png', - dynamic: true, - }), - }, - fields: [ - { - name: 'Balance', - value: $(profile.money).pluralise('credit', 's'), - }, - ], - }, - }; -} - -function getSendEmbed(sender: User, receiver: User, amount: number): object { - return { - embed: { - color: 0xffff00, - author: { - name: sender.username, - icon_url: sender.displayAvatarURL({ - format: 'png', - dynamic: true, - }), - }, - title: 'Transaction', - description: `${sender.toString()} has sent ${$(amount).pluralise( - 'credit', - 's', - )} to ${receiver.toString()}!`, - fields: [ - { - name: `Sender: ${sender.username}#${sender.discriminator}`, - value: $(Storage.getUser(sender.id).money).pluralise('credit', 's'), - }, - { - name: `Receiver: ${receiver.username}#${receiver.discriminator}`, - value: $(Storage.getUser(receiver.id).money).pluralise('credit', 's'), - }, - ], - footer: { - text: receiver.username, - icon_url: receiver.displayAvatarURL({ - format: 'png', - dynamic: true, - }), - }, - }, - }; -} - -export default new Command({ - description: - 'See how much money you have. Also provides other commands related to money.', - async run($: CommonLibrary): Promise { - $.channel.send(getMoneyEmbed($.author)); - }, - subcommands: { - get: new Command({ - description: - 'Pick up your daily credits. The cooldown is per user and every 22 hours to allow for some leeway.', - async run($: CommonLibrary): Promise { - const user = Storage.getUser($.author.id); - const now = Date.now(); - - if (user.lastReceived === -1) { - user.money = 100; - user.lastReceived = now; - Storage.save(); - $.channel.send( - "Here's 100 credits to get started, the price of a sandwich in Rookie Harbor.", - getMoneyEmbed($.author), - ); - } else if (now - user.lastReceived >= 79200000) { - user.money += 25; - user.lastReceived = now; - Storage.save(); - $.channel.send( - "Here's your daily 25 credits.", - getMoneyEmbed($.author), - ); - } else - $.channel.send( - `It's too soon to pick up your daily credits. You have about ${( - (user.lastReceived + 79200000 - now) / - 3600000 - ).toFixed(1)} hours to go.`, - ); - }, - }), - send: new Command({ - description: 'Send money to someone.', - usage: ' ', - run: 'Who are you sending this money to?', - user: new Command({ - run: "You need to enter an amount you're sending!", - number: new Command({ - async run($: CommonLibrary): Promise { - const amount = Math.floor($.args[1]); - const author = $.author; - const sender = Storage.getUser(author.id); - const target = $.args[0]; - const receiver = Storage.getUser(target.id); - - if (amount <= 0) - return $.channel.send('You must send at least one credit!'); - else if (sender.money < amount) - return $.channel.send( - "You don't have enough money to do that!", - getMoneyEmbed(author), - ); - else if (target.id === author.id) - return $.channel.send("You can't send money to yourself!"); - else if (target.bot && process.argv[2] !== 'dev') - return $.channel.send("You can't send money to a bot!"); - - sender.money -= amount; - receiver.money += amount; - Storage.save(); - $.channel.send(getSendEmbed(author, target, amount)); - }, - }), - }), - number: new Command({ - run: 'You must use the format `money send `!', - }), - any: new Command({ - async run($: CommonLibrary): Promise { - const last = $.args.pop(); - - if (!/\d+/g.test(last) && $.args.length === 0) - return $.channel.send( - "You need to enter an amount you're sending!", - ); - - const amount = Math.floor(last); - const author = $.author; - const sender = Storage.getUser(author.id); - - if (amount <= 0) - return $.channel.send('You must send at least one credit!'); - else if (sender.money < amount) - return $.channel.send( - "You don't have enough money to do that!", - getMoneyEmbed(author), - ); - else if (!$.guild) - return $.channel.send( - 'You have to use this in a server if you want to send money with a username!', - ); - - const username = $.args.join(' '); - const member = ( - await $.guild.members.fetch({ - query: username, - limit: 1, - }) - ).first(); - - if (!member) - return $.channel.send( - `Couldn't find a user by the name of \`${username}\`! If you want to send money to someone in a different server, you have to use their user ID!`, - ); - else if (member.user.id === author.id) - return $.channel.send("You can't send money to yourself!"); - else if (member.user.bot && process.argv[2] !== 'dev') - return $.channel.send("You can't send money to a bot!"); - - const target = member.user; - - $.prompt( - await $.channel.send( - `Are you sure you want to send ${$(amount).pluralise( - 'credit', - 's', - )} to this person?\n*(This message will automatically be deleted after 10 seconds.)*`, - { - embed: { - color: '#ffff00', - author: { - name: `${target.username}#${target.discriminator}`, - icon_url: target.displayAvatarURL({ - format: 'png', - dynamic: true, - }), - }, - }, - }, - ), - $.author.id, - () => { - const receiver = Storage.getUser(target.id); - sender.money -= amount; - receiver.money += amount; - Storage.save(); - $.channel.send(getSendEmbed(author, target, amount)); - }, - ); - }, - }), - }), - leaderboard: new Command({ - description: - 'See the richest players tracked by this bot (across servers).', - async run($: CommonLibrary): Promise { - const users = Storage.users; - const ids = Object.keys(users); - ids.sort((a, b) => users[b].money - users[a].money); - const fields = []; - - for (let i = 0, limit = Math.min(10, ids.length); i < limit; i++) { - const id = ids[i]; - const user = await $.client.users.fetch(id); - - fields.push({ - name: `#${i + 1}. ${user.username}#${user.discriminator}`, - value: $(users[id].money).pluralise('credit', 's'), - }); - } - - $.channel.send({ - embed: { - title: 'Top 10 Richest Players', - color: '#ffff00', - fields: fields, - }, - }); - }, - }), - }, - user: new Command({ - description: - 'See how much money someone else has by using their user ID or pinging them.', - async run($: CommonLibrary): Promise { - $.channel.send(getMoneyEmbed($.args[0])); - }, - }), - any: new Command({ - description: 'See how much money someone else has by using their username.', - async run($: CommonLibrary): Promise { - $.callMemberByUsername($.message, $.args.join(' '), (member) => { - $.channel.send(getMoneyEmbed(member.user)); - }); - }, - }), -});