Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
megoRU committed Nov 7, 2024
1 parent 42a2d54 commit c24d250
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
33 changes: 24 additions & 9 deletions src/main/java/main/config/BotStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public synchronized void startBot() {
//Устанавливаем языки
setLanguages();
getLocalizationFromDB();
getSchedulingFromDB();

List<GatewayIntent> intents = Arrays.asList(
GatewayIntent.GUILD_MEMBERS,
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.GUILD_EMOJIS_AND_STICKERS,
GatewayIntent.GUILD_MESSAGE_REACTIONS,
GatewayIntent.DIRECT_MESSAGES,
GatewayIntent.DIRECT_MESSAGE_TYPING);
GatewayIntent.GUILD_MEMBERS,
GatewayIntent.GUILD_MESSAGES,
GatewayIntent.GUILD_MESSAGE_REACTIONS,
GatewayIntent.DIRECT_MESSAGES,
GatewayIntent.DIRECT_MESSAGE_TYPING);

jdaBuilder.disableCache(
CacheFlag.ACTIVITY,
Expand Down Expand Up @@ -156,6 +156,20 @@ public synchronized void startBot() {
}
}

private void getSchedulingFromDB() {
try {
List<Scheduling> schedulingList = schedulingRepository.findAll();
GiveawayRegistry instance = GiveawayRegistry.getInstance();

for (Scheduling scheduling : schedulingList) {
Long guildId = scheduling.getGuildId();
instance.putScheduling(guildId, scheduling);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}

private void updateSlashCommands() {
try {
CommandListUpdateAction commands = jda.updateCommands();
Expand Down Expand Up @@ -402,8 +416,10 @@ private void topGGAndStatcord() {

@Scheduled(fixedDelay = 5, initialDelay = 5, timeUnit = TimeUnit.SECONDS)
private void scheduleStartGiveaway() {
List<Scheduling> allScheduling = schedulingRepository.findAll();
for (Scheduling scheduling : allScheduling) {
GiveawayRegistry instance = GiveawayRegistry.getInstance();
Collection<Scheduling> scheduledGiveaways = instance.getScheduledGiveaways();

for (Scheduling scheduling : scheduledGiveaways) {
Timestamp localTime = new Timestamp(System.currentTimeMillis());

if (localTime.after(scheduling.getDateCreateGiveaway())) {
Expand All @@ -426,7 +442,6 @@ private void scheduleStartGiveaway() {
giveawayRepositoryService,
updateController);

GiveawayRegistry instance = GiveawayRegistry.getInstance();
instance.putGift(scheduling.getGuildId(), giveaway);

String formattedDate = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/main/core/events/CancelCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void cancel(@NotNull SlashCommandInteractionEvent event) {
schedulingRepository.deleteById(guildId);
instance.removeGuildFromGiveaway(guildId);
cancel.setDescription(cancelSchedulingGiveaway);
instance.removeScheduling(guildId);
} else if (activeGiveaways != null) {
String cancelGiveaway = jsonParsers.getLocale("cancel_giveaway", guildId);

Expand All @@ -49,7 +50,6 @@ public void cancel(@NotNull SlashCommandInteractionEvent event) {
cancel.setDescription(cancelGiveaway);
} else {
String cancelSchedulingGiveaway = jsonParsers.getLocale("no_active_giveaway", guildId);

cancel.setDescription(cancelSchedulingGiveaway);
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/main/core/events/SchedulingCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package main.core.events;

import main.giveaway.GiveawayRegistry;
import main.giveaway.GiveawayUtils;
import main.jsonparser.JSONParsers;
import main.model.entity.ActiveGiveaways;
Expand Down Expand Up @@ -127,6 +128,9 @@ public void scheduling(@NotNull SlashCommandInteractionEvent event) {

schedulingRepository.save(scheduling);

GiveawayRegistry instance = GiveawayRegistry.getInstance();
instance.putScheduling(guildId, scheduling);

String scheduleEnd = jsonParsers.getLocale("schedule_end", guildId);
long timeStart = Objects.requireNonNull(timeProcessor(startTime)).getTime() / 1000;
if (endTime != null) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/main/giveaway/GiveawayRegistry.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package main.giveaway;

import main.model.entity.Scheduling;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GiveawayRegistry {

//Возвращает GiveawayData по long id
private static final Map<Long, Giveaway> giveawayMap = new ConcurrentHashMap<>();
private static final Map<Long, Scheduling> schedulingMap = new ConcurrentHashMap<>();
private static volatile GiveawayRegistry giveawayRegistry;

private GiveawayRegistry() {
Expand All @@ -30,6 +33,18 @@ public Collection<Giveaway> getAllGiveaway() {
return giveawayMap.values();
}

public Collection<Scheduling> getScheduledGiveaways() {
return schedulingMap.values();
}

public void removeScheduling(long guildId) {
schedulingMap.remove(guildId);
}

public void putScheduling(long guildId, Scheduling scheduling) {
schedulingMap.put(guildId, scheduling);
}

@Nullable
public Giveaway getGiveaway(long guildId) {
return giveawayMap.get(guildId);
Expand Down

0 comments on commit c24d250

Please sign in to comment.