Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Cooldown-Functions'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean committed Feb 11, 2023
2 parents 756a0e9 + a98e127 commit e4de18c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 33 deletions.
112 changes: 112 additions & 0 deletions src/commands/cooldown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const { EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const guildModel = require('../util/Models/guildModel');
function time(str, sec = false) {
const x = sec ? 1 : 1000;
if (typeof str !== 'string') return 0;
const fixed = str.replace(/\s/g, '');
const tail = +fixed.match(/-?\d+$/g) || 0;
const parts = (fixed.match(/-?\d+[^-0-9]+/g) || [])
.map(v => +v.replace(/[^-0-9]+/g, '') * ({ s: x, m: 60 * x, h: 3600 * x, d: 86400 * x }[v.replace(/[-0-9]+/g, '')] || 0));
return [tail, ...parts].reduce((a, b) => a + b, 0);
};
module.exports = {
requireGuild: true,
data: new SlashCommandBuilder()
.setName('cooldown')
.setDescription('Configure cooldown for multiple commands.')
.setDMPermission(false)
.setDescriptionLocalizations({
de: 'TBA',
"es-ES": 'TBA'
})
.addSubcommand((subcommand) => subcommand
.setName('replay')
.setDescription('Cooldown for Replay Button')
.addStringOption((option) =>
option
.setName('time')
.setDescription('Change the cooldown for the voting timer. Example: 30s, 5m, 2d')
.setRequired(true)
))
.addSubcommand((subcommand) => subcommand
.setName('voting')
.setDescription('Cooldown for Voting Timer.')
.addStringOption((option) =>
option
.setName('time')
.setDescription('Change the cooldown for the voting timer. Example: 30s, 5m, 2d')
.setRequired(true)
)),

/**
* @param {CommandInteraction} interaction
* @param {Client} client
* @param {guildModel} guildDb
*/
async execute(interaction, client, guildDb) {
const { COOLDOWN } = require(`../languages/${guildDb.language}.json`);
if (
interaction.member.permissions.has(PermissionFlagsBits.ManageGuild)
) {
const timeStamp = interaction.options.getString('time')
switch (interaction.options.getSubcommand()) {
case "replay":
const replay = time(timeStamp);
if (replay < 5000) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.notEnough })
else if (replay > 86400000) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.tooMuchReplay })
else if (timeStamp.includes("-")) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.syntax })

await client.database.updateGuild(interaction.guildId, {
replayCooldown: replay
}, true)

const replayembed = new EmbedBuilder()
.setColor("#2f3037")
.setDescription(`${COOLDOWN.embed.replaySuccess}\`${timeStamp}\``);
await interaction.reply({
embeds: [replayembed],
ephemeral: true,
})
.catch((err) => {

});
break;
case "voting":
const voting = time(timeStamp);
if (voting < 5000) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.notEnough })
else if (voting > 604800000) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.tooMuch })
else if (timeStamp.includes("-")) return interaction.reply({ ephemeral: true, content: COOLDOWN.error.syntax })

await client.database.updateGuild(interaction.guildId, {
votingCooldown: voting
}, true)

const votingembed = new EmbedBuilder()
.setColor("#2f3037")
.setDescription(`${COOLDOWN.embed.votingSuccess}\`${timeStamp}\``);
await interaction.reply({
embeds: [votingembed],
ephemeral: true,
})
.catch((err) => {

});
break;
}
} else {
const errorembed = new EmbedBuilder()
.setColor("#F00505")
.setTitle("Error!")
.setDescription(REPLAY.embed.missingPerms);

return interaction
.reply({
embeds: [errorembed],
ephemeral: true,
})
.catch((err) => {
return;
});
}
},
};
33 changes: 2 additions & 31 deletions src/commands/replay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits, ButtonBuilder, ActionRowBuilder} = require('discord.js');
const { EmbedBuilder, SlashCommandBuilder, PermissionFlagsBits, ButtonBuilder, ActionRowBuilder } = require('discord.js');
const guildModel = require('../util/Models/guildModel');
require("dotenv").config();
const Topgg = require(`@top-gg/sdk`)
Expand All @@ -22,15 +22,6 @@ module.exports = {
.setName('enable')
.setDescription('Disable or enable the replay button.')
.setRequired(true)
))
.addSubcommand((subcommand) => subcommand
.setName('cooldown')
.setDescription('Change the cooldown for the replay button.')
.addNumberOption((option) =>
option
.setName('cooldown')
.setDescription('Change the cooldown for the replay button. Use seconds to determine how long.')
.setRequired(true)
)),

/**
Expand All @@ -39,7 +30,7 @@ module.exports = {
* @param {guildModel} guildDb
*/
async execute(interaction, client, guildDb) {
const {REPLAY} = require(`../languages/${guildDb.language}.json`);
const { REPLAY } = require(`../languages/${guildDb.language}.json`);
if (
interaction.member.permissions.has(PermissionFlagsBits.ManageGuild)
) {
Expand Down Expand Up @@ -88,26 +79,6 @@ module.exports = {
return;
});
break;
case "cooldown":
const cooldown = interaction.options.getNumber('cooldown');

await client.database.updateGuild(interaction.guildId, {
replayCooldown: cooldown * 1000,
}, true)

const nochannelEmbed = new EmbedBuilder()
.setColor("#2f3037")
.setTitle("Error!")
.setDescription(`${REPLAY.embed.cooldownSuccess}\`${cooldown.toLocaleString()}\`${REPLAY.embed.cooldownSuccess2}`);
await interaction
.reply({
embeds: [nochannelEmbed],
ephemeral: true,
})
.catch((err) => {

});
break;
}
} else {
const errorembed = new EmbedBuilder()
Expand Down
14 changes: 12 additions & 2 deletions src/languages/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,25 @@
"missingPerms": "You are missing the `Manage Server` permission to use this command."
}
},
"COOLDOWN": {
"error": {
"syntax": "Cooldowns can't have negative numbers! Make sure to put only positive numbers.",
"notEnough": "Cooldown can't be below 5 seconds! Make sure to put all cooldowns above 5 seconds.",
"tooMuch": "Voting cooldown can't be above a week! Make sure to put all cooldowns below one week.",
"tooMuchReplay": "Replay cooldown can't be above a day! Make sure to put all cooldowns below one day."
},
"embed": {
"votingSuccess": "Successfully set voting cooldown to ",
"replaySuccess": "Successfully set replay cooldown to "
}
},
"REPLAY": {
"embed": {
"title": "Would You Replays",
"description": "You've successfully set Replay to",
"errorDesc": "In order to use this command you need to vote for the bot!",
"errorAlready": "Replaying is already disabled",
"errorAlready2": "Replaying is already enabled",
"cooldownSuccess": "Successfully put cooldown for ",
"cooldownSuccess2": " seconds!",
"success": "Replay has been disabled successfully",
"missingPerms": "You are missing the `Manage Server` permission to use this command."
}
Expand Down
4 changes: 4 additions & 0 deletions src/util/Models/guildModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const guildProfile = new Schema(
type: Number,
default: 30000,
},
votingCooldown: {
type: Number,
default: 20000,
},
botJoined: {
type: Number,
},
Expand Down

0 comments on commit e4de18c

Please # to comment.