✨ shop roles and fix guild creation
This commit is contained in:
parent
6861b62ee3
commit
31283ed9ce
15 changed files with 269 additions and 4 deletions
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
|
@ -24,5 +24,8 @@
|
|||
],
|
||||
"editor.fontFamily": "Cascadia Code",
|
||||
"editor.fontLigatures": true,
|
||||
"git.enableCommitSigning": true
|
||||
"git.enableCommitSigning": true,
|
||||
"files.associations": {
|
||||
"*.yaml": "home-assistant"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
"dotenv": "^16.0.0",
|
||||
"i18next": "^21.6.13",
|
||||
"mongoose": "^6.2.3",
|
||||
"node-schedule": "^2.1.0",
|
||||
"pino": "^7.0.0-rc.9",
|
||||
"quick.db": "^7.1.3",
|
||||
"uuid": "^8.3.2"
|
||||
|
|
24
src/commands/shop/addons/roles.js
Normal file
24
src/commands/shop/addons/roles.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
const { v4: uuidv4 } = require('uuid');
|
||||
const axios = require('axios');
|
||||
const config = require('../../../../config.json');
|
||||
const logger = require('../../../handlers/logger');
|
||||
|
||||
const { credits, apis } = require('../../../helpers/database/models');
|
||||
const creditNoun = require('../../../helpers/creditNoun');
|
||||
|
||||
module.exports = async (interaction) => {
|
||||
const name = interaction.options.getString('name');
|
||||
|
||||
guild.roles
|
||||
.create({
|
||||
data: {
|
||||
name,
|
||||
color: 'BLUE',
|
||||
},
|
||||
reason: `${interaction.member.id} bought from shop`,
|
||||
})
|
||||
.then(console.log)
|
||||
.catch(console.error);
|
||||
|
||||
interaction.editReply({ content: 'Roles' });
|
||||
};
|
|
@ -4,6 +4,7 @@ const { Permissions } = require('discord.js');
|
|||
const guilds = require('../../helpers/database/models/guildSchema');
|
||||
|
||||
const pterodactyl = require('./addons/pterodactyl');
|
||||
const roles = require('./roles');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
|
@ -18,6 +19,31 @@ module.exports = {
|
|||
.setName('amount')
|
||||
.setDescription('How much credits you want to withdraw.')
|
||||
)
|
||||
)
|
||||
.addSubcommandGroup((group) =>
|
||||
group
|
||||
.setName('roles')
|
||||
.setDescription('Manage custom roles.')
|
||||
.addSubcommand((command) =>
|
||||
command
|
||||
.setName('buy')
|
||||
.setDescription('Buy a custom role')
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName('name')
|
||||
.setDescription('Name of the role you wish to purchase.')
|
||||
)
|
||||
)
|
||||
.addSubcommand((command) =>
|
||||
command
|
||||
.setName('cancel')
|
||||
.setDescription('Cancel a custom role')
|
||||
.addStringOption((option) =>
|
||||
option
|
||||
.setName('name')
|
||||
.setDescription('Name of the role you wish to cancel.')
|
||||
)
|
||||
)
|
||||
),
|
||||
async execute(interaction) {
|
||||
// If subcommand is pterodactyl
|
||||
|
@ -25,5 +51,11 @@ module.exports = {
|
|||
// Execute pterodactyl addon
|
||||
await pterodactyl(interaction);
|
||||
}
|
||||
|
||||
// If subcommand group is roles
|
||||
else if (interaction.options.getSubcommandGroup() === 'roles') {
|
||||
// Execute roles addon
|
||||
await roles(interaction);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
53
src/commands/shop/roles/addons/buy.js
Normal file
53
src/commands/shop/roles/addons/buy.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
const { v4: uuidv4 } = require('uuid');
|
||||
const axios = require('axios');
|
||||
const config = require('../../../../../config.json');
|
||||
const logger = require('../../../../handlers/logger');
|
||||
|
||||
const {
|
||||
credits,
|
||||
apis,
|
||||
shopRoles,
|
||||
guilds,
|
||||
} = require('../../../../helpers/database/models');
|
||||
const creditNoun = require('../../../../helpers/creditNoun');
|
||||
|
||||
module.exports = async (interaction) => {
|
||||
const { member } = interaction;
|
||||
|
||||
const name = await interaction.options.getString('name');
|
||||
|
||||
await interaction.guild.roles
|
||||
.create({
|
||||
name,
|
||||
color: 'RED',
|
||||
reason: `${interaction.member.id} bought from shop`,
|
||||
})
|
||||
.then(async (data) => {
|
||||
// Get guild object
|
||||
const guild = await guilds.findOne({
|
||||
guildId: interaction.member.guild.id,
|
||||
});
|
||||
|
||||
const userObject = await credits.findOne({
|
||||
userId: member.id,
|
||||
guildId: interaction.member.guild.id,
|
||||
});
|
||||
const pricePerHour = guild.shop.roles.pricePerHour;
|
||||
|
||||
userObject.balance -= pricePerHour;
|
||||
|
||||
shopRoles.create({
|
||||
roleId: data.id,
|
||||
userId: member.id,
|
||||
guildId: member.guild.id,
|
||||
pricePerHour,
|
||||
lastPayed: new Date(),
|
||||
});
|
||||
|
||||
interaction.member.roles.add(data.id);
|
||||
shopRoles.find().then((data) => console.log(data));
|
||||
})
|
||||
.catch(console.error);
|
||||
|
||||
await interaction.editReply({ content: 'Roles bought' });
|
||||
};
|
13
src/commands/shop/roles/addons/cancel.js
Normal file
13
src/commands/shop/roles/addons/cancel.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const { v4: uuidv4 } = require('uuid');
|
||||
const axios = require('axios');
|
||||
const config = require('../../../../../config.json');
|
||||
const logger = require('../../../../handlers/logger');
|
||||
|
||||
const { credits, apis } = require('../../../../helpers/database/models');
|
||||
const creditNoun = require('../../../../helpers/creditNoun');
|
||||
|
||||
module.exports = async (interaction) => {
|
||||
const name = interaction.options.getString('name');
|
||||
|
||||
interaction.editReply({ content: 'Roles canceled' });
|
||||
};
|
4
src/commands/shop/roles/addons/index.js
Normal file
4
src/commands/shop/roles/addons/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
const buy = require('./buy');
|
||||
const cancel = require('./cancel');
|
||||
|
||||
module.exports = { buy, cancel };
|
29
src/commands/shop/roles/index.js
Normal file
29
src/commands/shop/roles/index.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
const { Permissions } = require('discord.js');
|
||||
const config = require('../../../../config.json');
|
||||
const logger = require('../../../handlers/logger');
|
||||
|
||||
const { buy, cancel } = require('./addons');
|
||||
|
||||
module.exports = async (interaction) => {
|
||||
// Destructure member
|
||||
const { member } = interaction;
|
||||
|
||||
// If subcommand is buy
|
||||
if (interaction.options.getSubcommand() === 'buy') {
|
||||
// Execute buy addon
|
||||
await buy(interaction);
|
||||
}
|
||||
|
||||
// If subcommand is cancel
|
||||
if (interaction.options.getSubcommand() === 'cancel') {
|
||||
// Execute cancel addon
|
||||
await cancel(interaction);
|
||||
}
|
||||
|
||||
// Send debug message
|
||||
await logger.debug(
|
||||
`Guild: ${member.guild.id} User: ${member.id} executed /${
|
||||
interaction.commandName
|
||||
} ${interaction.options.getSubcommandGroup()} ${interaction.options.getSubcommand()}`
|
||||
);
|
||||
};
|
|
@ -7,7 +7,10 @@ module.exports = {
|
|||
const { client } = guild;
|
||||
|
||||
// Create guild object if not already created
|
||||
await guilds.findOne({ guildId: guild.id }, { new: true, upsert: true });
|
||||
const guildExist = await guilds.findOne({ guildId: guild.id });
|
||||
if (!guildExist) {
|
||||
await guilds.create({ guildId: guild.id });
|
||||
}
|
||||
|
||||
// Set client status
|
||||
await client.user.setPresence({
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const events = require('./events');
|
||||
const commands = require('./commands');
|
||||
const locale = require('./locale');
|
||||
const schedules = require('./schedules');
|
||||
|
||||
module.exports = { events, commands, locale };
|
||||
module.exports = { events, commands, locale, schedules };
|
||||
|
|
52
src/handlers/schedules.js
Normal file
52
src/handlers/schedules.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
const schedule = require('node-schedule');
|
||||
const { shopRoles, credits, guilds } = require('../helpers/database/models');
|
||||
const logger = require('./logger');
|
||||
|
||||
module.exports = async (client) => {
|
||||
// schedule.scheduleJob('*/1 * * *', function () {
|
||||
// console.log('The answer to life, the universe, and everything!');
|
||||
// });
|
||||
|
||||
schedule.scheduleJob('*/30 * * * *', async () => {
|
||||
shopRoles.find().then(async (data) => {
|
||||
data.map(async (role) => {
|
||||
var payed = new Date(role.lastPayed);
|
||||
|
||||
oneHourAfterPayed = payed.setHours(payed.getHours() + 1);
|
||||
|
||||
if (new Date() > oneHourAfterPayed) {
|
||||
// Get guild object
|
||||
const guild = await guilds.findOne({
|
||||
guildId: role.guildId,
|
||||
});
|
||||
|
||||
const userObject = await credits.findOne({
|
||||
userId: role.userId,
|
||||
guildId: role.guildId,
|
||||
});
|
||||
const pricePerHour = guild.shop.roles.pricePerHour;
|
||||
|
||||
if (userObject.balance < pricePerHour) {
|
||||
const rGuild = await client.guilds.cache.get(`${role.guildId}`);
|
||||
let rMember = await rGuild.members.fetch(`${role.userId}`);
|
||||
|
||||
await rMember.roles
|
||||
.remove(`${role.roleId}`)
|
||||
.then(console.log)
|
||||
.catch(console.error); //Removes all roles
|
||||
}
|
||||
|
||||
role.lastPayed = new Date();
|
||||
role.save();
|
||||
userObject.balance -= pricePerHour;
|
||||
userObject.save();
|
||||
await logger.debug(
|
||||
`${role.roleId} was payed one hour later. BEFORE: ${payed} AFTER: ${oneHourAfterPayed} UPDATED: ${role.updatedAt} CREATED: ${role.createdAt}`
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
await logger.debug('Checking schedules! (Every 30 minutes)');
|
||||
});
|
||||
};
|
|
@ -40,6 +40,14 @@ const guildSchema = new mongoose.Schema(
|
|||
default: 900000,
|
||||
},
|
||||
},
|
||||
shop: {
|
||||
roles: {
|
||||
pricePerHour: {
|
||||
type: mongoose.SchemaTypes.Number,
|
||||
default: 5,
|
||||
},
|
||||
},
|
||||
},
|
||||
points: {
|
||||
status: {
|
||||
type: mongoose.SchemaTypes.Boolean,
|
||||
|
|
|
@ -5,6 +5,7 @@ const guilds = require('./guildSchema');
|
|||
const apis = require('./apiSchema');
|
||||
const timeouts = require('./timeoutSchema');
|
||||
const counters = require('./counterSchema');
|
||||
const shopRoles = require('./shopRolesSchema');
|
||||
|
||||
module.exports = {
|
||||
credits,
|
||||
|
@ -14,4 +15,5 @@ module.exports = {
|
|||
apis,
|
||||
timeouts,
|
||||
counters,
|
||||
shopRoles,
|
||||
};
|
||||
|
|
39
src/helpers/database/models/shopRolesSchema.js
Normal file
39
src/helpers/database/models/shopRolesSchema.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const mongoose = require('mongoose');
|
||||
|
||||
const shopRoleSchema = new mongoose.Schema(
|
||||
{
|
||||
roleId: {
|
||||
type: mongoose.SchemaTypes.Decimal128,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
userId: {
|
||||
type: mongoose.SchemaTypes.Decimal128,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
guildId: {
|
||||
type: mongoose.SchemaTypes.Decimal128,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
pricePerHour: {
|
||||
type: mongoose.SchemaTypes.Number,
|
||||
required: true,
|
||||
unique: false,
|
||||
index: true,
|
||||
default: 5,
|
||||
},
|
||||
lastPayed: {
|
||||
type: mongoose.SchemaTypes.Date,
|
||||
unique: false,
|
||||
index: true,
|
||||
},
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
module.exports = mongoose.model('shopRole', shopRoleSchema);
|
|
@ -2,7 +2,7 @@
|
|||
const { Client, Intents } = require('discord.js'); // discord.js
|
||||
|
||||
const { database } = require('./helpers'); // helpers
|
||||
const { events, commands, locale } = require('./handlers'); // handlers
|
||||
const { events, commands, locale, schedules } = require('./handlers'); // handlers
|
||||
|
||||
const config = require('../config.json'); // config.json
|
||||
|
||||
|
@ -16,6 +16,7 @@ const config = require('../config.json'); // config.json
|
|||
await locale();
|
||||
await events(client);
|
||||
await commands(client);
|
||||
await schedules(client);
|
||||
|
||||
await client.login(config.bot.token);
|
||||
})();
|
||||
|
|
Loading…
Add table
Reference in a new issue