✨ Add a work command
This commit is contained in:
parent
5cf3138c53
commit
8e47072fca
18 changed files with 1133 additions and 1037 deletions
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
|
@ -46,5 +46,13 @@
|
|||
"titleBar.inactiveBackground": "#33669999",
|
||||
"titleBar.inactiveForeground": "#e7e7e799"
|
||||
},
|
||||
"cSpell.words": ["Controlpanel", "discordjs", "pino", "upsert", "uuidv"]
|
||||
"cSpell.words": [
|
||||
"Controlpanel",
|
||||
"discordjs",
|
||||
"pino",
|
||||
"runned",
|
||||
"Timout",
|
||||
"upsert",
|
||||
"uuidv"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ module.exports = async (interaction) => {
|
|||
const user = await interaction.options.getUser('user');
|
||||
|
||||
await credits
|
||||
// eslint-disable-next-line max-len
|
||||
.findOne({ userId: user ? user.id : interaction.user.id, guildId: interaction.member.guild.id })
|
||||
.then(async (data) => {
|
||||
if (!data) {
|
||||
|
|
|
@ -10,6 +10,7 @@ module.exports = async (interaction) => {
|
|||
const user = await interaction.options.getUser('user');
|
||||
const amount = await interaction.options.getInteger('amount');
|
||||
const reason = await interaction.options.getString('reason');
|
||||
// eslint-disable-next-line max-len
|
||||
const data = await credits.findOne({ userId: interaction.user.id, guildId: interaction.member.guild.id });
|
||||
|
||||
if (user.id === interaction.user.id) {
|
||||
|
@ -72,7 +73,7 @@ module.exports = async (interaction) => {
|
|||
};
|
||||
const dmEmbed = {
|
||||
title: 'Gift',
|
||||
description: `You recieved ${creditNoun(amount)} from ${interaction.user}${reason ? ` with reason: ${reason}` : ''}. Your new balance is ${creditNoun(
|
||||
description: `You received ${creditNoun(amount)} from ${interaction.user}${reason ? ` with reason: ${reason}` : ''}. Your new balance is ${creditNoun(
|
||||
toUser.balance,
|
||||
)}.`,
|
||||
color: 0x22bb33,
|
||||
|
|
|
@ -23,6 +23,7 @@ module.exports = async (interaction) => {
|
|||
}
|
||||
const amount = await interaction.options.getInteger('amount');
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
const user = await credits.findOne({ userId: interaction.user.id, guildId: interaction.member.guild.id });
|
||||
const dmUser = interaction.client.users.cache.get(interaction.member.user.id);
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ const logger = require('../../../handlers/logger');
|
|||
|
||||
const guilds = require('../../../helpers/database/models/guildSchema');
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
module.exports = async (interaction) => {
|
||||
try {
|
||||
const status = await interaction.options.getBoolean('status');
|
||||
|
@ -11,6 +12,8 @@ module.exports = async (interaction) => {
|
|||
const rate = await interaction.options.getNumber('rate');
|
||||
const timeout = await interaction.options.getNumber('timeout');
|
||||
const minimumLength = await interaction.options.getNumber('minimum-length');
|
||||
const workRate = await interaction.options.getNumber('work-rate');
|
||||
const workTimeout = await interaction.options.getNumber('work-timeout');
|
||||
|
||||
const guild = await guilds.findOne({ guildId: interaction.member.guild.id });
|
||||
|
||||
|
@ -19,6 +22,9 @@ module.exports = async (interaction) => {
|
|||
guild.credits.token = token !== null ? token : guild.credits.token;
|
||||
guild.credits.rate = rate !== null ? rate : guild.credits.rate;
|
||||
guild.credits.timeout = timeout !== null ? timeout : guild.credits.timeout;
|
||||
guild.credits.workRate = workRate !== null ? workRate : guild.credits.workRate;
|
||||
// eslint-disable-next-line max-len
|
||||
guild.credits.workTimeout = workTimeout !== null ? workTimeout : guild.credits.workTimeout;
|
||||
// eslint-disable-next-line max-len
|
||||
guild.credits.minimumLength = minimumLength !== null ? minimumLength : guild.credits.minimumLength;
|
||||
|
||||
|
@ -27,7 +33,14 @@ module.exports = async (interaction) => {
|
|||
title: 'Credits',
|
||||
description: 'Following settings is set',
|
||||
color: config.colors.success,
|
||||
fields: [{ name: 'Status', value: `${guild.credits.status}`, inline: true }, { name: 'URL', value: `${guild.credits.url}`, inline: true }, { name: 'Token', value: `${guild.credits.token}` }, { name: 'Rate', value: `${guild.credits.rate}`, inline: true }, { name: 'Minimum Length', value: `${guild.credits.minimumLength}`, inline: true }, { name: 'Timeout', value: `${guild.credits.timeout}`, inline: true }],
|
||||
fields: [{ name: 'Status', value: `${guild.credits.status}`, inline: true },
|
||||
{ name: 'URL', value: `${guild.credits.url}`, inline: true },
|
||||
{ name: 'Token', value: `${guild.credits.token}` },
|
||||
{ name: 'Rate', value: `${guild.credits.rate}`, inline: true },
|
||||
{ name: 'Minimum Length', value: `${guild.credits.minimumLength}`, inline: true },
|
||||
{ name: 'Timeout', value: `${guild.credits.timeout}`, inline: true },
|
||||
{ name: 'Work Rate', value: `${guild.credits.workRate}`, inline: true },
|
||||
{ name: 'Work Timeout', value: `${guild.credits.workTimeout}`, inline: true }],
|
||||
timestamp: new Date(),
|
||||
footer: { iconURL: config.footer.icon, text: config.footer.text },
|
||||
};
|
||||
|
|
65
src/commands/credits/addons/work.js
Normal file
65
src/commands/credits/addons/work.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const config = require('../../../../config.json');
|
||||
const logger = require('../../../handlers/logger');
|
||||
const guilds = require('../../../helpers/database/models/guildSchema');
|
||||
const credits = require('../../../helpers/database/models/creditSchema');
|
||||
const creditNoun = require('../../../helpers/creditNoun');
|
||||
|
||||
const workedRecently = new Set();
|
||||
|
||||
// eslint-disable-next-line consistent-return
|
||||
module.exports = async (interaction) => {
|
||||
try {
|
||||
const guild = await guilds.findOne({ guildId: interaction.member.guild.id });
|
||||
if (!workedRecently.has(interaction.member.id)) {
|
||||
const creditsEarned = Math.floor(Math.random() * guild.credits.workRate);
|
||||
await credits
|
||||
.findOneAndUpdate(
|
||||
{ userId: interaction.member.id, guildId: interaction.member.guild.id },
|
||||
{ $inc: { balance: creditsEarned } },
|
||||
{ new: true, upsert: true },
|
||||
)
|
||||
.then(async () => {
|
||||
logger.debug(`Credits added to user: ${interaction.member.id}`);
|
||||
const embed = {
|
||||
title: 'Work',
|
||||
description: `You earned ${creditNoun(creditsEarned)}`,
|
||||
color: config.colors.success,
|
||||
timestamp: new Date(),
|
||||
footer: { iconURL: config.footer.icon, text: config.footer.text },
|
||||
};
|
||||
|
||||
return interaction.editReply({ embeds: [embed], ephemeral: true });
|
||||
})
|
||||
.catch(async (err) => {
|
||||
await logger.error(err);
|
||||
});
|
||||
|
||||
workedRecently.add(interaction.member.id);
|
||||
setTimeout(() => {
|
||||
logger.debug(
|
||||
`User: ${interaction.member.id} has not worked within last ${
|
||||
guild.credits.workTimeout / 1000
|
||||
} seconds, work can be runned`,
|
||||
);
|
||||
workedRecently.delete(interaction.member.id);
|
||||
}, guild.credits.timeout);
|
||||
} else {
|
||||
logger.debug(
|
||||
`User: ${interaction.member.id} has already worked within last ${
|
||||
guild.credits.workTimeout / 1000
|
||||
} seconds, no work is runned`,
|
||||
);
|
||||
const embed = {
|
||||
title: 'Work',
|
||||
description: `You can not work now, wait ${guild.credits.workTimeout / 1000} seconds until timeout is out.`,
|
||||
color: config.colors.error,
|
||||
timestamp: new Date(),
|
||||
footer: { iconURL: config.footer.icon, text: config.footer.text },
|
||||
};
|
||||
|
||||
return interaction.editReply({ embeds: [embed], ephemeral: true });
|
||||
}
|
||||
} catch (e) {
|
||||
await logger.error(e);
|
||||
}
|
||||
};
|
|
@ -13,6 +13,7 @@ const top = require('./addons/top');
|
|||
const transfer = require('./addons/transfer');
|
||||
const set = require('./addons/set');
|
||||
const settings = require('./addons/settings');
|
||||
const work = require('./addons/work');
|
||||
|
||||
module.exports = {
|
||||
permissions: new Permissions([
|
||||
|
@ -78,24 +79,17 @@ module.exports = {
|
|||
.addSubcommand((subcommand) => subcommand
|
||||
.setName('settings')
|
||||
.setDescription('Manage credit settings. (ADMIN)')
|
||||
.addBooleanOption((option) => option
|
||||
.setName('status')
|
||||
.setDescription('Toggle credits.'))
|
||||
.addStringOption((option) => option
|
||||
.setName('url')
|
||||
.setDescription('Controlpanel.gg URL.'))
|
||||
.addStringOption((option) => option
|
||||
.setName('token')
|
||||
.setDescription('Controlpanel.gg token.'))
|
||||
.addNumberOption((option) => option
|
||||
.setName('rate')
|
||||
.setDescription('Credits rate.'))
|
||||
.addNumberOption((option) => option
|
||||
.setName('minimum-length')
|
||||
.setDescription('Minimum length for credits.'))
|
||||
.addNumberOption((option) => option
|
||||
.setName('timeout')
|
||||
.setDescription('Timeout between credits (milliseconds).'))),
|
||||
.addBooleanOption((option) => option.setName('status').setDescription('Toggle credits.'))
|
||||
.addStringOption((option) => option.setName('url').setDescription('Controlpanel.gg URL.'))
|
||||
.addStringOption((option) => option.setName('token').setDescription('Controlpanel.gg token.'))
|
||||
.addNumberOption((option) => option.setName('rate').setDescription('Credits rate.'))
|
||||
.addNumberOption((option) => option.setName('minimum-length').setDescription('Minimum length for credits.'))
|
||||
.addNumberOption((option) => option.setName('work-rate').setDescription('Work rate (rate).'))
|
||||
.addNumberOption((option) => option.setName('work-timeout').setDescription('Timeout between working for credits (milliseconds).'))
|
||||
.addNumberOption((option) => option.setName('timeout').setDescription('Timeout between credits (milliseconds).')))
|
||||
.addSubcommand((subcommand) => subcommand
|
||||
.setName('work')
|
||||
.setDescription('Work for credits.')),
|
||||
async execute(interaction) {
|
||||
const guild = await guilds.findOne({ guildId: interaction.member.guild.id });
|
||||
|
||||
|
@ -130,6 +124,8 @@ module.exports = {
|
|||
await transfer(interaction);
|
||||
} else if (interaction.options.getSubcommand() === 'set') {
|
||||
await set(interaction);
|
||||
} else if (interaction.options.getSubcommand() === 'work') {
|
||||
await work(interaction);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
const config = require('../../config.json');
|
||||
const logger = require('../handlers/logger');
|
||||
|
||||
const guilds = require('../helpers/database/models/guildSchema');
|
||||
|
@ -6,8 +5,14 @@ const guilds = require('../helpers/database/models/guildSchema');
|
|||
module.exports = {
|
||||
name: 'interactionCreate',
|
||||
async execute(guild) {
|
||||
const guildExist = await guilds.findOne({ guildId: guild.id });
|
||||
const guildExist = await guilds.findOne({ guildId: guild.id })
|
||||
.then(logger.debug(`Found guild: ${guild.id}`))
|
||||
.catch(logger.error);
|
||||
|
||||
if (!guildExist) { await guilds.create({ guildId: guild.id }); }
|
||||
if (!guildExist) {
|
||||
await guilds.create({ guildId: guild.id })
|
||||
.then(() => logger.debug(`Create guild: ${guild.id} was success`))
|
||||
.catch((e) => logger.error(e));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
|
@ -28,6 +28,12 @@ const guildSchema = new mongoose.Schema(
|
|||
timeout: {
|
||||
type: mongoose.SchemaTypes.Number, default: 5000,
|
||||
},
|
||||
workRate: {
|
||||
type: mongoose.SchemaTypes.Number, default: 15,
|
||||
},
|
||||
workTimeout: {
|
||||
type: mongoose.SchemaTypes.Number, default: 900000,
|
||||
},
|
||||
},
|
||||
},
|
||||
{ timestamps: true },
|
||||
|
|
Loading…
Add table
Reference in a new issue