Skip to content

Commit 961e41e

Browse files
committed
Fix bots being flagged for "account too new"
Also fix a FNFE which happened when the `autoicons` folder did not exist. Also fix the painter scheduling server icon change too late.
1 parent b68ef22 commit 961e41e

File tree

8 files changed

+70
-11
lines changed

8 files changed

+70
-11
lines changed

src/painter/java/com/mcmoddev/mmdbot/painter/ThePainter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void shutdown() {
150150
private static Date measureNext12PMUTC() {
151151
final var day = OffsetDateTime.now(ZoneId.of("UTC")).plusDays(1);
152152
final var calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
153-
calendar.set(day.getYear(), day.getMonthValue(), day.getDayOfMonth(), 0, 0, 0);
153+
calendar.set(day.getYear(), day.getMonthValue() - 1, day.getDayOfMonth(), 0, 0, 0);
154154
return calendar.getTime();
155155
}
156156
}

src/painter/java/com/mcmoddev/mmdbot/painter/servericon/GenerateIconCommand.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.jagrosh.jdautilities.command.SlashCommand;
2424
import com.jagrosh.jdautilities.command.SlashCommandEvent;
2525
import com.mcmoddev.mmdbot.painter.ThePainter;
26-
import net.dv8tion.jda.api.Permission;
2726
import net.dv8tion.jda.api.entities.Icon;
2827
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
2928
import net.dv8tion.jda.api.interactions.commands.OptionType;
@@ -41,9 +40,6 @@ public class GenerateIconCommand extends SlashCommand {
4140
public GenerateIconCommand() {
4241
name = "generate";
4342
help = "Generate an icon";
44-
userPermissions = new Permission[] {
45-
Permission.MANAGE_ROLES
46-
};
4743
options = List.of(
4844
new OptionData(OptionType.STRING, "color", "Primary default colour for the icon", true),
4945
new OptionData(OptionType.STRING, "pattern-color", "Background pattern colour"),

src/painter/java/com/mcmoddev/mmdbot/painter/servericon/ServerIconCommand.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ public static final class Set extends SlashCommand {
6868
private Set() {
6969
name = "set-icon";
7070
help = "Set the server's icon";
71-
userPermissions = new Permission[] {
72-
Permission.MANAGE_ROLES
73-
};
7471
guildOnly = true;
7572
options = List.of(
7673
new OptionData(OptionType.ATTACHMENT, "icon", "Server icon as file", false),

src/painter/java/com/mcmoddev/mmdbot/painter/servericon/auto/AutomaticIconConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static AutomaticIconConfiguration get(String guildId) throws IOException
5656

5757
public void save(String guildId) throws IOException {
5858
final var path = ThePainter.getInstance().getRunPath().resolve("autoicons").resolve(guildId + ".json");
59+
if (!Files.exists(path)) Files.createDirectories(path.getParent());
5960
try (final var writer = Files.newBufferedWriter(path)) {
6061
GSON.toJson(this, writer);
6162
}

src/painter/java/com/mcmoddev/mmdbot/painter/servericon/auto/DayCounter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static DayCounter read() {
5151
@SneakyThrows
5252
public void write() {
5353
Path path = ThePainter.getInstance().getRunPath().resolve("autoicons/days.json");
54-
Files.createDirectories(path.getParent());
54+
if (!Files.exists(path)) Files.createDirectories(path.getParent());
5555
try (final var writer = Files.newBufferedWriter(path)) {
5656
AutomaticIconConfiguration.GSON.toJson(days(), writer);
5757
}

src/painter/java/com/mcmoddev/mmdbot/painter/servericon/auto/command/AutoIconSetCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.mcmoddev.mmdbot.painter.servericon.ServerIconMaker;
2929
import com.mcmoddev.mmdbot.painter.servericon.auto.AutomaticIconConfiguration;
3030
import com.mcmoddev.mmdbot.painter.servericon.auto.DayCounter;
31+
import com.mcmoddev.mmdbot.painter.util.CooldownManager;
3132
import lombok.SneakyThrows;
3233
import net.dv8tion.jda.api.events.GenericEvent;
3334
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
@@ -46,6 +47,7 @@
4647
import java.util.Comparator;
4748
import java.util.List;
4849
import java.util.Objects;
50+
import java.util.concurrent.TimeUnit;
4951

5052
public class AutoIconSetCommand extends SlashCommand implements EventListener {
5153
public static final AutoIconSetCommand INSTANCE = new AutoIconSetCommand();
@@ -75,11 +77,18 @@ protected void execute(final SlashCommandEvent event) {
7577
.queue();
7678
}
7779

80+
private static final CooldownManager BUTTON_COOLDOWN = new CooldownManager(10, TimeUnit.SECONDS);
7881
@Override
7982
public void onEvent(@NotNull final GenericEvent ge) {
8083
if (!(ge instanceof ButtonInteractionEvent event)) return;
8184
if (!(Objects.equals(event.getButton().getId(), BUTTON_ID))) return;
8285

86+
if (!BUTTON_COOLDOWN.check(event.getUser())) {
87+
event.reply("You can use this button " + BUTTON_COOLDOWN.coolDownFriendly(event.getUser()) + ".")
88+
.setEphemeral(true).queue();
89+
return;
90+
}
91+
8392
event.deferReply().queue();
8493
try {
8594
final var cfg = AutomaticIconConfiguration.get(event.getGuild().getId());
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* MMDBot - https://github.com/MinecraftModDevelopment/MMDBot
3+
* Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment>
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation;
8+
* Specifically version 2.1 of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
* USA
19+
* https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
20+
*/
21+
package com.mcmoddev.mmdbot.painter.util;
22+
23+
import com.github.benmanes.caffeine.cache.Cache;
24+
import com.github.benmanes.caffeine.cache.Caffeine;
25+
import net.dv8tion.jda.api.entities.User;
26+
import net.dv8tion.jda.api.utils.TimeFormat;
27+
28+
import java.time.Duration;
29+
import java.time.Instant;
30+
import java.util.Objects;
31+
import java.util.concurrent.TimeUnit;
32+
33+
public class CooldownManager {
34+
private final Cache<Long, Instant> users;
35+
private final Duration cooldownTime;
36+
37+
public CooldownManager(final long time, final TimeUnit unit) {
38+
this.cooldownTime = Duration.of(time, unit.toChronoUnit());
39+
this.users = Caffeine.newBuilder()
40+
.expireAfterWrite(time, unit)
41+
.build();
42+
}
43+
44+
public boolean check(final User user) {
45+
if (users.asMap().containsKey(user.getIdLong())) {
46+
return false;
47+
}
48+
users.put(user.getIdLong(), Instant.now());
49+
return true;
50+
}
51+
52+
public String coolDownFriendly(final User user) {
53+
return TimeFormat.RELATIVE.format(Objects.requireNonNull(users.getIfPresent(user.getIdLong()))
54+
.plus(cooldownTime));
55+
}
56+
}

src/watcher/java/com/mcmoddev/mmdbot/watcher/punishments/NewAccount.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public String getReason(final GuildMemberJoinEvent event, final Member member) {
5050
}
5151

5252
@Override
53-
public boolean test(final GuildMemberJoinEvent guildMemberJoinEvent) {
54-
return guildMemberJoinEvent.getMember()
53+
public boolean test(final GuildMemberJoinEvent event) {
54+
return !event.getUser().isBot() && event.getMember()
5555
.getTimeCreated()
5656
.toInstant()
5757
.isAfter(Instant.now() // TODO Make the threshold a config

0 commit comments

Comments
 (0)