Compare commits
2 commits
afdbbee495
...
7254a242db
Author | SHA1 | Date | |
---|---|---|---|
7254a242db | |||
d225d7037c |
4 changed files with 58 additions and 60 deletions
|
@ -1,12 +1,12 @@
|
||||||
import { PermissionFlagsBits, InteractionType } from 'discord.js';
|
import { PermissionFlagsBits, InteractionType } from 'discord.js';
|
||||||
import db from '../../models/index.js';
|
import db from '../../models/index.js';
|
||||||
|
import ratelimiter from '../../utils/ratelimiter.js';
|
||||||
|
|
||||||
const { ownerId } = process.env;
|
const { ownerId } = process.env;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'interactionCreate',
|
name: 'interactionCreate',
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
const ratelimit = global.ratelimit;
|
|
||||||
const client = interaction.client;
|
const client = interaction.client;
|
||||||
if (interaction.type !== InteractionType.ApplicationCommand) return;
|
if (interaction.type !== InteractionType.ApplicationCommand) return;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ export default {
|
||||||
|
|
||||||
if (!command) return;
|
if (!command) return;
|
||||||
|
|
||||||
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m`);
|
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m with slash`);
|
||||||
|
|
||||||
// Owner only check
|
// Owner only check
|
||||||
if (command.ownerOnly && interaction.user.id !== ownerId) {
|
if (command.ownerOnly && interaction.user.id !== ownerId) {
|
||||||
|
@ -51,35 +51,15 @@ export default {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Check the ratelimit
|
||||||
|
const doRateLimit = ratelimiter.check(interaction.user, commandName, command);
|
||||||
|
if (doRateLimit) {
|
||||||
|
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m is rate limited on \x1b[33m${commandName}\x1b[0m for ${ratelimiter.timeLeft(userID, commandName)} seconds`);
|
||||||
|
return interaction.reply({ content: doRateLimit, ephemeral: true });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!ratelimit[userID]) {
|
|
||||||
ratelimit[userID] = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const date = new Date();
|
|
||||||
if (ratelimit[userID][commandName]) {
|
|
||||||
if (ratelimit[userID][commandName].cooldown) {
|
|
||||||
if (date > ratelimit[userID][commandName].cooldown) {
|
|
||||||
ratelimit[userID][commandName].limit = 0;
|
|
||||||
ratelimit[userID][commandName].cooldown = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command.ratelimit === ratelimit[userID][commandName].limit) {
|
|
||||||
return await interaction.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`, ephemeral: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (command.ratelimit) {
|
|
||||||
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
|
|
||||||
if (command.ratelimit === ratelimit[userID][commandName].limit) {
|
|
||||||
date.setSeconds(date.getSeconds() + command.cooldown);
|
|
||||||
|
|
||||||
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interaction.prefix = '/';
|
interaction.prefix = '/';
|
||||||
|
|
||||||
const args = {};
|
const args = {};
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
import { ApplicationCommandOptionType, EmbedBuilder, PermissionFlagsBits } from 'discord.js';
|
import { ApplicationCommandOptionType, EmbedBuilder, PermissionFlagsBits } from 'discord.js';
|
||||||
import db from '../../models/index.js';
|
import db from '../../models/index.js';
|
||||||
import { rand } from '../../utils/rand.js';
|
import { rand } from '../../utils/rand.js';
|
||||||
|
import ratelimiter from '../../utils/ratelimiter.js';
|
||||||
|
|
||||||
const { ownerId, prefix } = process.env;
|
const { ownerId, prefix } = process.env;
|
||||||
const prefixs = prefix.split(',');
|
const prefixs = prefix.split(',');
|
||||||
|
@ -289,7 +290,7 @@ export default {
|
||||||
const userTag = message.author.tag;
|
const userTag = message.author.tag;
|
||||||
const userID = message.author.id;
|
const userID = message.author.id;
|
||||||
|
|
||||||
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m`);
|
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m launched command \x1b[33m${commandName}\x1b[0m with prefix`);
|
||||||
|
|
||||||
// Owner only check
|
// Owner only check
|
||||||
if (command.ownerOnly && message.author.id !== ownerId) {
|
if (command.ownerOnly && message.author.id !== ownerId) {
|
||||||
|
@ -311,35 +312,14 @@ export default {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check the ratelimit
|
||||||
|
const doRateLimit = ratelimiter.check(message.author, commandName, command);
|
||||||
|
if (doRateLimit) {
|
||||||
|
return message.reply({ content: doRateLimit, ephemeral: true });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ratelimit = global.ratelimit;
|
|
||||||
if (!ratelimit[userID]) {
|
|
||||||
ratelimit[userID] = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const date = new Date();
|
|
||||||
if (ratelimit[userID][commandName]) {
|
|
||||||
if (ratelimit[userID][commandName].cooldown) {
|
|
||||||
if (date > ratelimit[userID][commandName].cooldown) {
|
|
||||||
ratelimit[userID][commandName].limit = 0;
|
|
||||||
ratelimit[userID][commandName].cooldown = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command.ratelimit === ratelimit[userID][commandName].limit) {
|
|
||||||
return await message.reply({ content: `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)}huh seconds.`, ephemeral: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (command.ratelimit) {
|
|
||||||
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
|
|
||||||
if (command.ratelimit === ratelimit[userID][commandName].limit) {
|
|
||||||
date.setSeconds(date.getSeconds() + command.cooldown);
|
|
||||||
|
|
||||||
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
message.user = message.author;
|
message.user = message.author;
|
||||||
message.isMessage = true;
|
message.isMessage = true;
|
||||||
message.prefix = `${messageArray[0]} `;
|
message.prefix = `${messageArray[0]} `;
|
||||||
|
|
|
@ -6,7 +6,6 @@ export default {
|
||||||
once: true,
|
once: true,
|
||||||
async execute(client) {
|
async execute(client) {
|
||||||
// Init global variables.
|
// Init global variables.
|
||||||
global.ratelimit = {};
|
|
||||||
global.boards = {};
|
global.boards = {};
|
||||||
|
|
||||||
const ytdlpVersion = await new Promise((resolve, reject) => {
|
const ytdlpVersion = await new Promise((resolve, reject) => {
|
||||||
|
|
39
utils/ratelimiter.js
Normal file
39
utils/ratelimiter.js
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
const ratelimit = {};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
check,
|
||||||
|
};
|
||||||
|
function check(user, commandName, commands) {
|
||||||
|
const userID = user.id;
|
||||||
|
const userTag = user.tag;
|
||||||
|
|
||||||
|
if (!ratelimit[userID]) {
|
||||||
|
ratelimit[userID] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = new Date();
|
||||||
|
if (ratelimit[userID][commandName]) {
|
||||||
|
if (ratelimit[userID][commandName].cooldown) {
|
||||||
|
if (date > ratelimit[userID][commandName].cooldown) {
|
||||||
|
ratelimit[userID][commandName].limit = 0;
|
||||||
|
ratelimit[userID][commandName].cooldown = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commands.ratelimit === ratelimit[userID][commandName].limit) {
|
||||||
|
console.log(`\x1b[33m${userTag} (${userID})\x1b[0m is rate limited on \x1b[33m${commandName}\x1b[0m for ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds`);
|
||||||
|
return `You are being rate limited. You can try again in ${Math.floor((ratelimit[userID][commandName].cooldown - date) / 1000)} seconds.`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (commands.ratelimit) {
|
||||||
|
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName] ? ratelimit[userID][commandName].limit + 1 : 1 };
|
||||||
|
if (commands.ratelimit === ratelimit[userID][commandName].limit) {
|
||||||
|
date.setSeconds(date.getSeconds() + commands.cooldown);
|
||||||
|
|
||||||
|
ratelimit[userID][commandName] = { limit: ratelimit[userID][commandName].limit, cooldown: date };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
Loading…
Reference in a new issue