Skip to content

Commit

Permalink
New Update
Browse files Browse the repository at this point in the history
1. fix
  • Loading branch information
megoRU committed Feb 6, 2024
1 parent ca0d0a0 commit d8fff61
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/main/java/main/controller/UpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void slashEvent(@NotNull SlashCommandInteractionEvent event) {
changeCommand.change(event);
}
case "scheduling" -> {
SchedulingCommand schedulingCommand = new SchedulingCommand(schedulingRepository);
SchedulingCommand schedulingCommand = new SchedulingCommand(schedulingRepository, activeGiveawayRepository);
schedulingCommand.scheduling(event);
}
case "participants" -> {
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/main/core/events/SchedulingCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main.core.events;

import main.giveaway.GiveawayRegistry;
import main.giveaway.utils.ChecksClass;
import main.giveaway.utils.GiveawayUtils;
import main.jsonparser.JSONParsers;
import main.model.entity.ActiveGiveaways;
import main.model.entity.Scheduling;
import main.model.repository.ActiveGiveawayRepository;
import main.model.repository.SchedulingRepository;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel;
Expand All @@ -17,17 +18,21 @@

import java.awt.*;
import java.util.Objects;
import java.util.Optional;

@Service
public class SchedulingCommand {

private final SchedulingRepository schedulingRepository;
private final ActiveGiveawayRepository activeGiveawayRepository;

private static final JSONParsers jsonParsers = new JSONParsers();

@Autowired
public SchedulingCommand(SchedulingRepository schedulingRepository) {
public SchedulingCommand(SchedulingRepository schedulingRepository,
ActiveGiveawayRepository activeGiveawayRepository) {
this.schedulingRepository = schedulingRepository;
this.activeGiveawayRepository = activeGiveawayRepository;
}

public void scheduling(@NotNull SlashCommandInteractionEvent event) {
Expand All @@ -54,28 +59,32 @@ public void scheduling(@NotNull SlashCommandInteractionEvent event) {
boolean canSendGiveaway = ChecksClass.canSendGiveaway(textChannel, event);
if (!canSendGiveaway) return; //Сообщение уже отправлено

//Обработать уведомление
event.deferReply().setEphemeral(true).queue();

Scheduling scheduling = schedulingRepository.findByGuildLongId(guildIdLong);
ActiveGiveaways activeGiveaways = activeGiveawayRepository.findByGuildLongId(guildIdLong);

if (GiveawayRegistry.getInstance().hasGiveaway(guildIdLong)) {
if (activeGiveaways != null) {
String messageGiftNeedStopGiveaway = jsonParsers.getLocale("message_gift_need_stop_giveaway", guildId);
EmbedBuilder errors = new EmbedBuilder();
errors.setColor(Color.GREEN);
errors.setDescription(messageGiftNeedStopGiveaway);
event.replyEmbeds(errors.build()).queue();
event.getHook().sendMessageEmbeds(errors.build()).queue();
return;
} else if (scheduling != null) {
String messageGiftNeedStopGiveaway = jsonParsers.getLocale("message_gift_need_cancel_giveaway", guildId);
EmbedBuilder errors = new EmbedBuilder();
errors.setColor(Color.GREEN);
errors.setDescription(messageGiftNeedStopGiveaway);
event.replyEmbeds(errors.build()).queue();
event.getHook().sendMessageEmbeds(errors.build()).queue();
return;
}

if ((startTime != null && !startTime.matches(GiveawayUtils.ISO_TIME_REGEX)
|| (endTime != null && !endTime.matches(GiveawayUtils.ISO_TIME_REGEX)))) {
String wrongDate = jsonParsers.getLocale("wrong_date", guildId);
event.reply(wrongDate).queue();
event.getHook().sendMessage(wrongDate).queue();
return;
}

Expand All @@ -93,11 +102,11 @@ public void scheduling(@NotNull SlashCommandInteractionEvent event) {

if (role == null && isOnlyForSpecificRole) {
String slashErrorOnlyForThisRole = jsonParsers.getLocale("slash_error_only_for_this_role", guildId);
event.reply(slashErrorOnlyForThisRole).setEphemeral(true).queue();
event.getHook().sendMessage(slashErrorOnlyForThisRole).setEphemeral(true).queue();
return;
} else if (role != null && role == guildIdLong && isOnlyForSpecificRole) {
String slashErrorRoleCanNotBeEveryone = jsonParsers.getLocale("slash_error_role_can_not_be_everyone", guildId);
event.reply(slashErrorRoleCanNotBeEveryone).setEphemeral(true).queue();
event.getHook().sendMessage(slashErrorRoleCanNotBeEveryone).setEphemeral(true).queue();
return;
}

Expand Down Expand Up @@ -135,10 +144,10 @@ public void scheduling(@NotNull SlashCommandInteractionEvent event) {
start.setColor(Color.GREEN);
start.setDescription(scheduleStart);

event.replyEmbeds(start.build()).setEphemeral(true).queue();
event.getHook().sendMessageEmbeds(start.build()).setEphemeral(true).queue();
} else {
String startInNotTextChannels = jsonParsers.getLocale("start_in_not_text_channels", guildId);
event.reply(startInNotTextChannels).queue();
event.getHook().sendMessage(startInNotTextChannels).queue();
}
}
}
38 changes: 23 additions & 15 deletions src/main/java/main/core/events/StartCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import main.giveaway.utils.ChecksClass;
import main.giveaway.utils.GiveawayUtils;
import main.jsonparser.JSONParsers;
import main.model.entity.ActiveGiveaways;
import main.model.entity.Scheduling;
import main.model.repository.ActiveGiveawayRepository;
import main.model.repository.ListUsersRepository;
Expand All @@ -20,11 +21,15 @@

import java.awt.*;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service
@AllArgsConstructor
public class StartCommand {

private static final JSONParsers jsonParsers = new JSONParsers();
private final static Logger LOGGER = Logger.getLogger(StartCommand.class.getName());

private final ListUsersRepository listUsersRepository;
private final ActiveGiveawayRepository activeGiveawayRepository;
Expand All @@ -40,6 +45,8 @@ public void start(@NotNull SlashCommandInteractionEvent event) {

if (event.getGuild() == null) return;

event.deferReply().queue();

var guildId = event.getGuild().getIdLong();
var userIdLong = event.getUser().getIdLong();
var title = event.getOption("title", OptionMapping::getAsString);
Expand All @@ -53,19 +60,21 @@ public void start(@NotNull SlashCommandInteractionEvent event) {
Integer minParticipants = event.getOption("min_participants", OptionMapping::getAsInt);
var forbiddenRole = event.getOption("forbidden_role", OptionMapping::getAsRole);

Scheduling schedulingByGuildLongId = schedulingRepository.findByGuildLongId(guildId);
if (GiveawayRegistry.getInstance().hasGiveaway(guildId)) {
Scheduling scheduling = schedulingRepository.findByGuildLongId(guildId);
ActiveGiveaways activeGiveaways = activeGiveawayRepository.findByGuildLongId(guildId);

if (activeGiveaways != null) {
String messageGiftNeedStopGiveaway = jsonParsers.getLocale("message_gift_need_stop_giveaway", guildId);
EmbedBuilder errors = new EmbedBuilder();
errors.setColor(Color.GREEN);
errors.setDescription(messageGiftNeedStopGiveaway);
event.replyEmbeds(errors.build()).queue();
} else if (schedulingByGuildLongId != null) {
event.getHook().sendMessageEmbeds(errors.build()).queue();
} else if (scheduling != null) {
String messageGiftNeedStopGiveaway = jsonParsers.getLocale("message_gift_need_cancel_giveaway", guildId);
EmbedBuilder errors = new EmbedBuilder();
errors.setColor(Color.GREEN);
errors.setDescription(messageGiftNeedStopGiveaway);
event.replyEmbeds(errors.build()).queue();
event.getHook().sendMessageEmbeds(errors.build()).queue();
} else {
try {
String urlImage = null;
Expand All @@ -88,14 +97,14 @@ public void start(@NotNull SlashCommandInteractionEvent event) {
String startWrongTime = String.format(jsonParsers.getLocale("start_wrong_time", guildId), time, startExamples);

embedBuilder.setDescription(startWrongTime);
event.replyEmbeds(embedBuilder.build()).setEphemeral(true).queue();
event.getHook().sendMessageEmbeds(embedBuilder.build()).setEphemeral(true).queue();
return;
}

if (title != null && title.length() >= MessageEmbed.TITLE_MAX_LENGTH) {
String slashError256 = jsonParsers.getLocale("slash_error_256", guildId);
embedBuilder.setDescription(slashError256);
event.replyEmbeds(embedBuilder.build()).setEphemeral(true).queue();
event.getHook().sendMessageEmbeds(embedBuilder.build()).setEphemeral(true).queue();
return;
}

Expand All @@ -105,23 +114,23 @@ public void start(@NotNull SlashCommandInteractionEvent event) {

if (role == null && isOnlyForSpecificRole) {
String slashErrorOnlyForThisRole = jsonParsers.getLocale("slash_error_only_for_this_role", guildId);
event.reply(slashErrorOnlyForThisRole).setEphemeral(true).queue();
event.getHook().sendMessage(slashErrorOnlyForThisRole).setEphemeral(true).queue();
return;
} else if (role != null && role.getIdLong() == guildId && isOnlyForSpecificRole) {
String slashErrorRoleCanNotBeEveryone = jsonParsers.getLocale("slash_error_role_can_not_be_everyone", guildId);
event.reply(slashErrorRoleCanNotBeEveryone).setEphemeral(true).queue();
event.getHook().sendMessage(slashErrorRoleCanNotBeEveryone).setEphemeral(true).queue();
return;
} else if (role != null && !isOnlyForSpecificRole) {
String giftNotificationForThisRole = String.format(jsonParsers.getLocale("gift_notification_for_this_role", guildId), role.getIdLong());
if (role.getIdLong() == guildId) {
giftNotificationForThisRole = String.format(jsonParsers.getLocale("gift_notification_for_everyone", guildId), "@everyone");
event.reply(giftNotificationForThisRole).queue();
event.getHook().sendMessage(giftNotificationForThisRole).queue();
} else {
event.reply(giftNotificationForThisRole).queue();
event.getHook().sendMessage(giftNotificationForThisRole).queue();
}
} else if (role != null) {
String giftNotificationForThisRole = String.format(jsonParsers.getLocale("gift_notification_for_this_role", guildId), role.getIdLong());
event.reply(giftNotificationForThisRole).queue();
event.getHook().sendMessage(giftNotificationForThisRole).queue();
}

GiveawayBuilder.Builder giveawayBuilder = new GiveawayBuilder.Builder();
Expand Down Expand Up @@ -150,16 +159,15 @@ public void start(@NotNull SlashCommandInteractionEvent event) {
if (!event.isAcknowledged()) {
try {
String sendSlashMessage = String.format(jsonParsers.getLocale("send_slash_message", guildId), event.getChannel().getId());
event.reply(sendSlashMessage).setEphemeral(true).queue();
event.getHook().sendMessage(sendSlashMessage).setEphemeral(true).queue();
} catch (Exception ignored) {
}
}

giveaway.startGiveaway(event.getChannel().asGuildMessageChannel(), false);

} catch (Exception e) {
if (!e.getMessage().contains("Time in the past")) {
e.printStackTrace();
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
String slashErrors = jsonParsers.getLocale("slash_errors", guildId);
EmbedBuilder errors = new EmbedBuilder();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/main/core/events/StopCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void stop(@NotNull SlashCommandInteractionEvent event) {
return;
}


if (!event.getOptions().get(0).getAsString().matches("\\d{1,2}")) {
String slashErrors = jsonParsers.getLocale("slash_errors", guildId);
EmbedBuilder errors = new EmbedBuilder();
Expand All @@ -65,7 +64,6 @@ public void stop(@NotNull SlashCommandInteractionEvent event) {
return;
}


//TODO: Что это)
EmbedBuilder stop = new EmbedBuilder();
Long count = event.getOption("count", OptionMapping::getAsLong);
Expand All @@ -88,6 +86,7 @@ public void stop(@NotNull SlashCommandInteractionEvent event) {
stop.setDescription(slashStopErrors);
event.replyEmbeds(stop.build()).queue();
}

giveaway.stopGiveaway(Integer.parseInt(event.getOptions().get(0).getAsString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import main.model.entity.ActiveGiveaways;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -27,5 +28,9 @@ public interface ActiveGiveawayRepository extends JpaRepository<ActiveGiveaways,
@EntityGraph(attributePaths = {"participants"})
List<ActiveGiveaways> findAll();

@Nullable
ActiveGiveaways findByGuildLongId(Long guildLongId);

@Nullable
ActiveGiveaways findByIdUserWhoCreateGiveawayAndGuildLongId(Long idUserWhoCreateGiveaway, Long guildLongId);
}
4 changes: 2 additions & 2 deletions src/main/resources/json/eng.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"messages_events_site": ":zap: [megoru.ru](https://megoru.ru)\n",
"messages_events_add_me_to_other_guilds": ":robot: [Add me to other guilds](https://discord.com/oauth2/authorize?client_id=808277484524011531&permissions=2147740736&scope=applications.commands%20bot)\n",
"message_gift_not_admin": "You are not Admin or you can't managed messages!",
"message_gift_need_stop_giveaway": "First you need to </stop:941286272390037536> Giveaway",
"message_gift_need_cancel_giveaway": "First you need to: </cancel:1102283573349851167> Giveaway",
"message_gift_need_stop_giveaway": "First you need to </stop:989192262317645855> Giveaway",
"message_gift_need_cancel_giveaway": "First you need to: </cancel:1101972035820847114> Giveaway",
"giveaway_registry_error": "The giveaway was canceled because the bot was unable to get the ID\nyour post for editing. Please try again. **Write us what exactly happened.\nWe are trying to fix this bug. Support guild:** https://discord.gg/UrWG3R683d",
"gift_ends_at": "%s | Ends at:",
"gift_ends": "Ended in",
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/json/rus.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"messages_events_site": ":zap: [megoru.ru](https://megoru.ru)\n",
"messages_events_add_me_to_other_guilds": ":robot: [Добавь меня в другие гильдии](https://discord.com/oauth2/authorize?client_id=808277484524011531&permissions=2147740736&scope=applications.commands%20bot)\n",
"message_gift_not_admin": "Вы не являетесь Админом или не можете управлять сообщениями!",
"message_gift_need_stop_giveaway": "Сначала вам нужно завершить Giveaway: </stop:941286272390037536>",
"message_gift_need_cancel_giveaway": "Сначала вам нужно отменить Giveaway: </cancel:1102283573349851167>",
"message_gift_need_stop_giveaway": "Сначала вам нужно завершить Giveaway: </stop:989192262317645855>",
"message_gift_need_cancel_giveaway": "Сначала вам нужно отменить Giveaway: </cancel:1101972035820847114>",
"giveaway_registry_error": "Giveaway был отменен, потому что бот не смог получить ID своего сообщения\nПожалуйста, попробуйте снова. **Напишите нам, что именно произошло.\nМы пытаемся исправить эту ошибку. Сервер поддержки:** https://discord.gg/UrWG3R683d",
"gift_ends_at": "%s | Завершится:",
"gift_ends": "Завершилась",
Expand Down

0 comments on commit d8fff61

Please sign in to comment.