Skip to content

Commit 3095331

Browse files
authored
Merge pull request #464 from danthe1st/block-forum-spam
block job/projects posts if other recent posts exist
2 parents f33aceb + f20233c commit 3095331

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/main/java/net/discordjug/javabot/listener/JobChannelCloseOldPostsListener.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package net.discordjug.javabot.listener;
22

33
import java.awt.Color;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
46

57
import lombok.RequiredArgsConstructor;
68
import net.discordjug.javabot.data.config.BotConfig;
9+
import net.discordjug.javabot.data.config.guild.ModerationConfig;
710
import net.discordjug.javabot.util.InteractionUtils;
811
import net.dv8tion.jda.api.EmbedBuilder;
912
import net.dv8tion.jda.api.entities.channel.ChannelType;
@@ -24,19 +27,41 @@ public void onChannelCreate(ChannelCreateEvent event) {
2427
if (event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD) {
2528
return;
2629
}
30+
2731
ThreadChannel post = event.getChannel().asThreadChannel();
28-
if (post.getParentChannel().getIdLong() !=
29-
botConfig.get(event.getGuild()).getModerationConfig().getJobChannelId()) {
32+
ModerationConfig moderationConfig = botConfig.get(event.getGuild()).getModerationConfig();
33+
long jobChannelId = moderationConfig.getJobChannelId();
34+
long projectChannelId = moderationConfig.getProjectChannelId();
35+
long parentChannelId = post.getParentChannel().getIdLong();
36+
37+
if (parentChannelId != jobChannelId && parentChannelId != projectChannelId) {
3038
return;
3139
}
3240

33-
34-
boolean postClosed = false;
35-
36-
for (ThreadChannel otherPost : post.getParentChannel().getThreadChannels()) {
37-
if (otherPost.getOwnerIdLong() == post.getOwnerIdLong() &&
38-
otherPost.getIdLong() != post.getIdLong() &&
39-
!otherPost.isPinned()) {
41+
List<ThreadChannel> threadChannels = post.getParentChannel()
42+
.getThreadChannels()
43+
.stream()
44+
.filter(c -> c.getOwnerIdLong() == post.getOwnerIdLong())
45+
.filter(otherPost -> otherPost.getIdLong() != post.getIdLong())
46+
.filter(c -> !c.isPinned())
47+
.collect(Collectors.toList());
48+
49+
for (ThreadChannel otherPost : threadChannels) {
50+
if(otherPost.getTimeCreated().plusDays(7).isAfter(post.getTimeCreated())) {
51+
post.sendMessageEmbeds(
52+
new EmbedBuilder()
53+
.setTitle("Post closed")
54+
.setDescription("This post has been blocked because you have created other recent posts.\nPlease do not spam posts.")
55+
.build())
56+
.setContent(post.getOwner().getAsMention())
57+
.flatMap(msg -> post.getManager().setArchived(true).setLocked(true))
58+
.queue();
59+
return;
60+
}
61+
}
62+
63+
if(parentChannelId == jobChannelId && !threadChannels.isEmpty()) {
64+
for (ThreadChannel otherPost : threadChannels) {
4065
otherPost.sendMessageEmbeds(
4166
new EmbedBuilder()
4267
.setTitle("Post closed")
@@ -45,10 +70,7 @@ public void onChannelCreate(ChannelCreateEvent event) {
4570
.build())
4671
.flatMap(msg -> otherPost.getManager().setArchived(true).setLocked(true))
4772
.queue();
48-
postClosed = true;
4973
}
50-
}
51-
if (postClosed) {
5274
post.sendMessageEmbeds(
5375
new EmbedBuilder()
5476
.setTitle("Posts closed")

0 commit comments

Comments
 (0)