Skip to content

Commit a83db20

Browse files
committed
automatically cancel event after game is over
1 parent f650dfa commit a83db20

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/main/java/pw/chew/mlb/listeners/GameFeedHandler.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import net.dv8tion.jda.api.Permission;
66
import net.dv8tion.jda.api.entities.Guild;
77
import net.dv8tion.jda.api.entities.MessageEmbed;
8+
import net.dv8tion.jda.api.entities.ScheduledEvent;
9+
import net.dv8tion.jda.api.entities.User;
810
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
911
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
1012
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
@@ -574,6 +576,30 @@ public static void endGame(String gamePk, GameState currentState, String scoreca
574576
} catch (InsufficientPermissionException ignored) {
575577
logger.debug("Insufficient permissions to send message to channel " + game.channelId());
576578
}
579+
580+
// Check for scheduled game and end it, game is over
581+
List<ScheduledEvent> events = channel.getGuild().getScheduledEvents();
582+
for (ScheduledEvent event : events) {
583+
User creator = event.getCreator();
584+
// If no creator, skip
585+
if (creator == null) continue;
586+
// If not us, skip
587+
if (!creator.getId().equals(jda.getSelfUser().getId())) continue;
588+
589+
// Get description
590+
String description = event.getDescription();
591+
// If no description, skip
592+
if (description == null) continue;
593+
// Game link is at very end
594+
String id = description.split("/")[description.split("/").length - 1];
595+
596+
// If we can't do anything, skip
597+
if (!channel.getGuild().getSelfMember().hasPermission(Permission.MANAGE_EVENTS)) continue;
598+
599+
if (id.equals(gamePk)) {
600+
event.delete().queue();
601+
}
602+
}
577603
}
578604

579605
// Remove the games from the active games list

0 commit comments

Comments
 (0)