|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/GeyserDiscordBot |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.discordbot.background; |
| 27 | + |
| 28 | +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; |
| 29 | +import java.time.OffsetDateTime; |
| 30 | +import java.util.List; |
| 31 | +import net.dv8tion.jda.api.Permission; |
| 32 | +import net.dv8tion.jda.api.entities.Guild; |
| 33 | +import net.dv8tion.jda.api.entities.guild.SecurityIncidentActions; |
| 34 | +import org.geysermc.discordbot.GeyserBot; |
| 35 | +import org.geysermc.discordbot.storage.ServerSettings; |
| 36 | + |
| 37 | +public class SecurityActionsApplier implements Runnable { |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + Long2ObjectMap<List<String>> enabledActionsPerGuild = ServerSettings.getListForAllServers("security-actions"); |
| 41 | + if (enabledActionsPerGuild.isEmpty()) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // The max is 24h, so just set it to 12h to be sure. |
| 46 | + // It runs every hour anyway. |
| 47 | + OffsetDateTime later = OffsetDateTime.now().plusHours(12); |
| 48 | + |
| 49 | + for (Long2ObjectMap.Entry<List<String>> entry : enabledActionsPerGuild.long2ObjectEntrySet()) { |
| 50 | + Guild guild = GeyserBot.getJDA().getGuildById(entry.getLongKey()); |
| 51 | + if (guild == null || !guild.getSelfMember().hasPermission(Permission.MANAGE_SERVER)) continue; |
| 52 | + |
| 53 | + boolean invitesDisabled = entry.getValue().contains("invites"); |
| 54 | + boolean dmsDisabled = entry.getValue().contains("dms"); |
| 55 | + |
| 56 | + if (invitesDisabled || dmsDisabled) { |
| 57 | + guild.modifySecurityIncidents(SecurityIncidentActions.enabled( |
| 58 | + invitesDisabled ? later : null, |
| 59 | + dmsDisabled ? later : null |
| 60 | + )).queue(); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + SecurityIncidentActions actions = guild.getSecurityIncidentActions(); |
| 65 | + if (actions.getDirectMessagesDisabledUntil() != null || actions.getInvitesDisabledUntil() != null) { |
| 66 | + guild.modifySecurityIncidents(SecurityIncidentActions.disabled()).queue(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments