-
-
Notifications
You must be signed in to change notification settings - Fork 101
Delete scam help threads #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d0254d2
created class
Taz03 12c2fac
initial commit
Taz03 78ac16e
code-fixes
Taz03 c7f46cd
added javadocs
Taz03 03c080c
code fixes
Taz03 2f3cdcb
code improvements
Taz03 a213759
code-fixes
Taz03 a2e2be6
CR
Taz03 bdcd985
CR
Taz03 f609030
CR
Taz03 c65b56e
CR
Taz03 bda0928
CR
Taz03 6f65374
CR
Taz03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...main/java/org/togetherjava/tjbot/commands/help/UserBannedDeleteRecentThreadsListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.togetherjava.tjbot.commands.help; | ||
|
||
import net.dv8tion.jda.api.entities.User; | ||
import net.dv8tion.jda.api.events.guild.GuildBanEvent; | ||
import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
import org.jooq.Record1; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.commands.EventReceiver; | ||
import org.togetherjava.tjbot.db.Database; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static org.togetherjava.tjbot.db.generated.Tables.HELP_THREADS; | ||
|
||
/** | ||
* Reacts on users being banned from a guild. It then deletes (not just closes) all their recently | ||
* created help threads. | ||
* <p> | ||
* This for example tackles threads created by scammers, which are otherwise not automatically | ||
* cleaned up on ban. | ||
*/ | ||
public final class UserBannedDeleteRecentThreadsListener extends ListenerAdapter | ||
implements EventReceiver { | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(UserBannedDeleteRecentThreadsListener.class); | ||
private static final Duration RECENT_THREAD_DURATION = Duration.ofMinutes(30); | ||
|
||
private final Database database; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param database to find help threads created by the banned user | ||
*/ | ||
public UserBannedDeleteRecentThreadsListener(Database database) { | ||
this.database = database; | ||
} | ||
|
||
@Override | ||
public void onGuildBan(GuildBanEvent event) { | ||
getUsersRecentHelpThreadIDs(event.getUser()).stream() | ||
.map(event.getJDA()::getThreadChannelById) | ||
.filter(Objects::nonNull) | ||
.forEach(threadChannel -> threadChannel.delete().queue(any -> { | ||
}, failure -> logger.warn("Failed to delete thread {} from banned user {}.", | ||
threadChannel.getId(), event.getUser().getId(), failure))); | ||
} | ||
|
||
private List<Long> getUsersRecentHelpThreadIDs(User user) { | ||
Instant recentThreadThreshold = Instant.now().minus(RECENT_THREAD_DURATION); | ||
|
||
return database | ||
.read(context -> context.select(HELP_THREADS.CHANNEL_ID) | ||
.from(HELP_THREADS) | ||
.where(HELP_THREADS.AUTHOR_ID.eq(user.getIdLong()) | ||
.and(HELP_THREADS.CREATED_AT.greaterThan(recentThreadThreshold))) | ||
.fetch()) | ||
.stream() | ||
.map(Record1::value1) | ||
.toList(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.