Skip to content

Commit

Permalink
feat: 🚸 add penalty cost if spamming messages
Browse files Browse the repository at this point in the history
If a user sends messages too fast, it will give them a penalty defined by "penaltyAmount" so that users will lose credits by spamming. Spam rate is set by "maxMessageAmount" which is how many messages a user is allowed to send during "checkTime"
  • Loading branch information
VermiumSifell committed May 31, 2023
1 parent 3875c9e commit 8c1c0af
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/events/messageCreate/components/earnCredits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,44 @@ export default async (message: Message) => {
}

try {
await creditsManager.give(guild, author, 1);
const filter = (msg: Message) => {
return msg.author == message.author;
};

if (message.author.bot) return;

const checkTime = 5 * 1000; // Milliseconds
const maxMessageAmount = 3; // Anti Spam Rule, remove 1 credit per message above this value during "checkTime" variable
const amount = 1; //Amount to give if valid
const penaltyAmount = 2; //Amount to take if invalid

await message.channel
.awaitMessages({ filter, time: checkTime })
.then(async (messages) => {
// Sounds logic with <= but since it goes down to 0 it should be < since we do not want to add at 3 too since then we add 4
// If user is below "maxMessageAmount"
if (messages.size < maxMessageAmount) {
await creditsManager.give(guild, author, amount);
}

// Sounds logic with > but since it goes down to 0 it should be >= since we want to remove at 0 too
// If user exceeds "maxMessageAmount"
if (messages.size >= maxMessageAmount) {
await creditsManager.take(guild, author, penaltyAmount);
}

// When it finished calculating results
if (messages.size === 0) {
await setCooldown(guild, author);
}
});
} catch (error: unknown) {
logger.error(
`Failed to give credits to user ${author.username} in guild ${
guild.name
} when sending a message: ${String(error)}`
);
}

await setCooldown(guild, author);
};

function isMessageValid(
Expand All @@ -44,6 +72,8 @@ function isMessageValid(
content: string
): boolean {
return (
guild &&
author &&
!author.bot &&
channel.type === ChannelType.GuildText &&
content.length >= MINIMUM_LENGTH
Expand Down

0 comments on commit 8c1c0af

Please sign in to comment.