Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Nov 5, 2024
1 parent 76bbf3c commit 103d357
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
71 changes: 68 additions & 3 deletions src/events/MessageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class MessageCreate {
channel instanceof ThreadChannel // Type guard
) {
const parentChannel = message.guild?.channels.cache.get(
channel.parentId!,
channel.parentId!
);
if (
parentChannel &&
Expand Down Expand Up @@ -85,6 +85,7 @@ export class MessageCreate {
count: 0,
lastMessage: null,
same: false,
recentLinks: new Map<string, Set<string>>(),
});
}

Expand Down Expand Up @@ -149,9 +150,73 @@ export class MessageCreate {
count: 0,
lastMessage: null,
same: false,
recentLinks: new Map<string, Set<string>>(),
});
}

const linkPattern = /\[.*?\]\(https?:\/\/[^\s)]+\)|(?:https?:\/\/)[^\s]+/gi;
const links = message.content.match(linkPattern);

if (links) {
const currentTime = Date.now();

// Clean up old entries (older than 5 minutes)
if (userState.recentLinks) {
for (const [link, channels] of userState.recentLinks.entries()) {
if (currentTime - Number(link.split("|")[1]) > 5 * 60 * 1000) {
userState.recentLinks.delete(link);
}
}
}

for (const link of links) {
const linkKey = `${link}|${currentTime}`;

if (!userState.recentLinks.has(linkKey)) {
userState.recentLinks.set(linkKey, new Set());
}

const channelSet = userState.recentLinks.get(linkKey)!;

// If link was already posted in this channel, ignore it
if (channelSet.has(message.channel.id)) {
continue;
}

channelSet.add(message.channel.id);

// Check if the same link was posted in multiple channels
let linkPostCount = 0;
for (const [, channels] of userState.recentLinks) {
if (channels.size > 0) {
linkPostCount++;
}
}

// If the same or similar links were posted in 3 or more channels within 5 minutes
if (linkPostCount >= 3) {
await deleteUserMessages({
days: 7,
jail: true,
memberId: message.author.id,
user: message.author,
guild: message.guild!,
});

// Reset the user's state after taking action
previousMessages.set(message.author.id, {
count: 0,
lastMessage: null,
same: false,
recentLinks: new Map(),
});

return;
}
}
}
}

@SimpleCommand({ aliases: [""], prefix: ["✅", ":white_check_mark:"] })
async checkThreadHelpLike(command: SimpleCommandMessage) {
const message = command.message;
Expand Down Expand Up @@ -200,14 +265,14 @@ export class MessageCreate {
const channel = (await message.channel.fetch()) as TextChannel;

const replyMsg = await channel.messages.fetch(
message.reference?.messageId,
message.reference?.messageId
);

await message.delete();

channel.send({
content: await translate(
Buffer.from(replyMsg.content, "utf-8").toString(),
Buffer.from(replyMsg.content, "utf-8").toString()
),
allowedMentions: { users: [] },
});
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface UserState {
count: number;
lastMessage: Message | null;
same?: boolean;
recentLinks: Map<string, Set<string>>;
}

export type Commands =
Expand Down

0 comments on commit 103d357

Please sign in to comment.