Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,19 @@
"item-duplicate": "The item already exist",
"role-to-high": "The specified role is higher than the highest role of the bot. Therefore the bot can't give the role to users. The item was **not** created.",
"delete-item": "The user %u has deleted the shop item %i",
"edit-item": "The user %u has edited the item %i. Possible changes are:\nNew name: %n\nNew price: %p\nNew role: %r",
"user-purchase": "The user %u has purchased the shop item %i for %p.",
"shop-command-description": "Use the shop-system",
"shop-command-description-add": "Create a new item in the shop (admins only)",
"shop-option-description-itemName": "Name of the item",
"shop-option-description-newItemName": "New name of the Item",
"shop-option-description-itemID": "ID of the Item",
"shop-option-description-price": "Price of the item",
"shop-option-description-role": "Role to give to users who buy the item",
"shop-command-description-buy": "Buy an item",
"shop-command-description-list": "List all items in the shop",
"shop-command-description-delete": "Remove an item from the shop",
"shop-command-description-edit": "Edit an item",
"channel-not-found": "Can't find the leaderboard channel with the ID %c",
"command-description-deposit": "Deposit xyz to your bank",
"option-description-amount-deposit": "Amount to deposit",
Expand All @@ -802,7 +805,8 @@
"migration-happening": "Database not up-to-date. Migrating database...",
"migration-done": "Migrated database successfully.",
"nothing-selected": "Select an item to buy it",
"select-menu-price": "Price: %p"
"select-menu-price": "Price: %p",
"price-less-than-zero": "The price can't be less or equal to zero"
},
"status-role": {
"fulfilled": "Status-role condition is fulfilled",
Expand Down
47 changes: 42 additions & 5 deletions modules/economy-system/commands/shop.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {createShopItem, createShopMsg, deleteShopItem, shopMsg, buyShopItem} = require('../economy-system');
const {createShopItem, createShopMsg, deleteShopItem, shopMsg, buyShopItem, updateShopItem} = require('../economy-system');
const {localize} = require('../../../src/functions/localize');

/**
* @param {*} interaction Interaction
* @returns {Promise<boolean>} Result
*/
async function checkPerms(interaction) {
async function checkPermsAndSendReplyOnFail(interaction) {
const result = interaction.client.configurations['economy-system']['config']['shopManagers'].includes(interaction.user.id) || interaction.client.config['botOperators'].includes(interaction.user.id);
if (!result) await interaction.reply({
content: interaction.client.strings['not_enough_permissions'],
Expand All @@ -16,7 +16,7 @@ async function checkPerms(interaction) {

module.exports.subcommands = {
'add': async function (interaction) {
if (!await checkPerms(interaction)) return;
if (!await checkPermsAndSendReplyOnFail(interaction)) return;
await interaction.deferReply({ephemeral: !interaction.client.configurations['economy-system']['config']['publicCommandReplies']});
await createShopItem(interaction);
await shopMsg(interaction.client);
Expand All @@ -32,10 +32,16 @@ module.exports.subcommands = {
interaction.reply(msg);
},
'delete': async function (interaction) {
if (!await checkPerms(interaction)) return;
if (!await checkPermsAndSendReplyOnFail(interaction)) return;
await interaction.deferReply({ephemeral: !interaction.client.configurations['economy-system']['config']['publicCommandReplies']});
await deleteShopItem(interaction);
await shopMsg(interaction.client);
},
'edit': async function (interaction) {
if (!await checkPermsAndSendReplyOnFail(interaction)) return;
await interaction.deferReply({ephemeral: !interaction.client.configurations['economy-system']['config']['publicCommandReplies']});
await updateShopItem(interaction);
await shopMsg(interaction.client);
}
};

Expand Down Expand Up @@ -117,6 +123,37 @@ module.exports.config = {
required: false
}
]
}
},
{
type: 'SUB_COMMAND',
name: 'edit',
description: localize('economy-system', 'shop-command-description-edit'),
options: [
{
type: 'STRING',
required: true,
name: 'item-id',
description: localize('economy-system', 'shop-option-description-itemID')
},
{
type: 'STRING',
required: false,
name: 'item-new-name',
description: localize('economy-system', 'shop-option-description-newItemName')
},
{
type: 'INTEGER',
required: false,
name: 'new-price',
description: localize('economy-system', 'shop-option-description-price')
},
{
type: 'ROLE',
required: false,
name: 'new-role',
description: localize('economy-system', 'shop-option-description-role')
}
]
},
]
};
28 changes: 28 additions & 0 deletions modules/economy-system/configs/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,34 @@
}
]
},
{
"name": "itemEdit",
"humanName": {},
"default": {
"en": "Successfully edited the item %name%. Check it out using `/shop list`"
},
"description": {
"en": "Message that gets sent when a shop item gets edited"
},
"type": "string",
"allowEmbed": true,
"params": [
{
"name": "name",
"description": {
"en": "Name of the edited item",
"de": "Name des bearbeiteten Items"
}
},
{
"name": "id",
"description": {
"en": "Id of the edited item",
"de": "ID des bearbeiteten Items"
}
}
]
},
{
"name": "depositMsg",
"humanName": {
Expand Down
104 changes: 102 additions & 2 deletions modules/economy-system/economy-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,16 @@ async function createShopItem(interaction) {
const role = await interaction.options.getRole('role', true);
const price = await interaction.options.getInteger('price');
const model = interaction.client.models['economy-system']['Shop'];
if (interaction.guild.me.roles.highest.comparePositionTo(role) <= 0) return await interaction.editReply(localize('economy-system', 'role-to-high'));
if (interaction.guild.members.me.roles.highest.comparePositionTo(role) <= 0) {
await interaction.editReply(localize('economy-system', 'role-to-high'));
return resolve(localize('economy-system', 'role-to-high'));
}

if(price<=0) {
await interaction.editReply(localize('economy-system', 'price-less-than-zero'));
return resolve(localize('economy-system', 'price-less-than-zero'));
}

const itemModel = await model.findOne({
where: {
[Op.or]: [
Expand Down Expand Up @@ -382,12 +391,102 @@ async function deleteShopItem(interaction) {
});
}

/**
* Function to update a shop-item
* @param {*} interaction Interaction
* @returns {Promise}
*/
async function updateShopItem(interaction) {
return new Promise(async (resolve) => {
const id = interaction.options.get('item-id')['value'];

if (!id) {
await interaction.editReply('Please use the id!'); //IDK how this should happen
return resolve();
}

const item = await interaction.client.models['economy-system']['Shop'].findOne({
where: {
id: id
}
});

if (!item) {
await interaction.editReply(embedType(interaction.client.configurations['economy-system']['strings']['noMatches'], {
'%id%': id,
'%name%': '-'
}));
return resolve();
}

const newNameOption = interaction.options.get('item-new-name');
const newPrice = interaction.options.getInteger('new-price');
const newRole = interaction.options.getRole('new-role');
if (newRole && interaction.guild.members.me.roles.highest.comparePositionTo(newRole) <= 0) {
await interaction.editReply(localize('economy-system', 'role-to-high'));
return resolve(localize('economy-system', 'role-to-high'));
}

if(newPrice !== null && newPrice<=0) {
await interaction.editReply(localize('economy-system', 'price-less-than-zero'));
return resolve(localize('economy-system', 'price-less-than-zero'));
}

if (newNameOption) {
const collidingItem = await interaction.client.models['economy-system']['Shop'].findOne({
where: {
name: newNameOption['value']
}
});
if (collidingItem && collidingItem['id'] !== id) {
await interaction.editReply(embedType(interaction.client.configurations['economy-system']['strings']['itemDuplicate'], {
'%id%': id,
'%name%': "-"
}));
return resolve(localize('economy-system', 'item-duplicate'));
}
}

if (newNameOption) {
item.name = newNameOption['value'];
}
if (newPrice !== null) {
item.price = newPrice;
}
if (newRole) {
item.role = newRole['id'];
}

await item.save();

await interaction.editReply(embedType(interaction.client.configurations['economy-system']['strings']['itemEdit'], {
'%name%': item.name,
'%id%': item.id
}));
interaction.client.logger.info(`[economy-system] ` + localize('economy-system', 'edit-item', {
u: interaction.user.tag,
i: id,
n: newNameOption ? newNameOption['value'] : "-",
p: newPrice ? newPrice : "-",
r: newRole ? newRole['name'] : "-",
}));
if (interaction.client.logChannel) await interaction.client.logChannel.send(`[economy-system] ` + localize('economy-system', 'edit-item', {
u: interaction.user.tag,
i: id,
n: newNameOption ? newNameOption['value'] : "-",
p: newPrice ? newPrice : "-",
r: newRole ? newRole['name'] : "-",
}));
resolve(`Edited the item ${item.name} successfully`);
});
}

/**
* Create the shop message
* @param {Client} client Client
* @param {object} guild Object of the guild
* @param {boolean} ephemeral Should the message be ephemeral?
* @returns {string}
* @returns {Promise<string>}
*/
async function createShopMsg(client, guild, ephemeral) {
const items = await client.models['economy-system']['Shop'].findAll();
Expand Down Expand Up @@ -515,6 +614,7 @@ module.exports.createShopItemAPI = createShopItemAPI;
module.exports.createShopItem = createShopItem;
module.exports.deleteShopItemAPI = deleteShopItemAPI;
module.exports.deleteShopItem = deleteShopItem;
module.exports.updateShopItem = updateShopItem;
module.exports.createShopMsg = createShopMsg;
module.exports.shopMsg = shopMsg;
module.exports.createLeaderboard = leaderboard;