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 7, 2024
1 parent 083e277 commit 48c4eaf
Show file tree
Hide file tree
Showing 28 changed files with 301 additions and 229 deletions.
66 changes: 33 additions & 33 deletions src/main/java/main/config/BotStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import main.model.entity.ActiveGiveaways;
import main.model.entity.Participants;
import main.model.entity.Scheduling;
import main.model.repository.ActiveGiveawayRepository;
import main.model.repository.ListUsersRepository;
import main.model.repository.ParticipantsRepository;
import main.model.repository.SchedulingRepository;
import main.model.entity.Settings;
import main.model.repository.*;
import main.threads.StopGiveawayByTimer;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand All @@ -40,7 +38,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

Expand All @@ -67,7 +64,7 @@ public class BotStart {
private static final JSONParsers jsonParsers = new JSONParsers();
public static final String activity = "/help | ";
//String - guildLongId
private static final ConcurrentMap<String, String> mapLanguages = new ConcurrentHashMap<>();
private static final ConcurrentMap<Long, Settings> mapLanguages = new ConcurrentHashMap<>();

@Getter
private static JDA jda;
Expand All @@ -84,6 +81,7 @@ public class BotStart {
private final ListUsersRepository listUsersRepository;
private final UpdateController updateController;
private final SchedulingRepository schedulingRepository;
private final SettingsRepository settingsRepository;

//DataBase
@Value("${spring.datasource.url}")
Expand All @@ -98,12 +96,14 @@ public BotStart(ActiveGiveawayRepository activeGiveawayRepository,
ParticipantsRepository participantsRepository,
ListUsersRepository listUsersRepository,
UpdateController updateController,
SchedulingRepository schedulingRepository) {
SchedulingRepository schedulingRepository,
SettingsRepository settingsRepository) {
this.activeGiveawayRepository = activeGiveawayRepository;
this.participantsRepository = participantsRepository;
this.listUsersRepository = listUsersRepository;
this.updateController = updateController;
this.schedulingRepository = schedulingRepository;
this.settingsRepository = settingsRepository;
}

@PostConstruct
Expand Down Expand Up @@ -150,7 +150,7 @@ public synchronized void startBot() {
System.out.println("IsDevMode: " + Config.isIsDev());

//Обновить команды
// updateSlashCommands();
updateSlashCommands();
System.out.println("20:22");
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -177,13 +177,17 @@ private void updateSlashCommands() {
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Примеры: 1, 2... Если не указано -> стандартное значение при запуске"));

//Set language
List<OptionData> optionsLanguage = new ArrayList<>();
optionsLanguage.add(new OptionData(STRING, "bot", "Setting the bot language")
List<OptionData> optionsSettings = new ArrayList<>();
optionsSettings.add(new OptionData(STRING, "language", "Setting the bot language")
.addChoice("\uD83C\uDDEC\uD83C\uDDE7 English Language", "eng")
.addChoice("\uD83C\uDDF7\uD83C\uDDFA Russian Language", "rus")
.setRequired(true)
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Настройка языка бота"));

optionsSettings.add(new OptionData(STRING, "color", "Embed color: #00FF00")
.setName("color")
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Embed цвет: #00FF00"));

//Scheduling Giveaway
List<OptionData> optionsScheduling = new ArrayList<>();

Expand Down Expand Up @@ -302,10 +306,10 @@ private void updateSlashCommands() {
.setGuildOnly(true)
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Проверка разрешений бота"));

commands.addCommands(Commands.slash("language", "Setting language")
.addOptions(optionsLanguage)
commands.addCommands(Commands.slash("settings", "Bot settings")
.addOptions(optionsSettings)
.setGuildOnly(true)
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Настройка языка")
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Настройки бота")
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MANAGE_SERVER)));

commands.addCommands(Commands.slash("start", "Create Giveaway")
Expand Down Expand Up @@ -360,6 +364,7 @@ private void updateSlashCommands() {

commands.addCommands(Commands.slash("cancel", "Cancel Giveaway")
.setGuildOnly(true)
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
.setDescriptionLocalization(DiscordLocale.RUSSIAN, "Отменить Giveaway"));

commands.queue();
Expand Down Expand Up @@ -407,7 +412,7 @@ private void scheduleStartGiveaway() {
Long role = scheduling.getRoleIdLong();
Boolean isOnlyForSpecificRole = scheduling.getIsForSpecificRole();
Long guildIdLong = scheduling.getGuildLongId();
String guildId = String.valueOf(scheduling.getGuildLongId());
Long guildId = scheduling.getGuildLongId();

Giveaway giveaway = new Giveaway(
scheduling.getGuildLongId(),
Expand Down Expand Up @@ -505,6 +510,7 @@ public void setGiveawayAndUsersInGift() {
String url_image = activeGiveaways.getUrlImage();
long id_user_who_create_giveaway = activeGiveaways.getIdUserWhoCreateGiveaway();
Integer min_participants = activeGiveaways.getMinParticipants();
boolean finishGiveaway = activeGiveaways.isFinishGiveaway();

Map<String, String> participantsMap = new HashMap<>();
Set<Participants> participantsList = activeGiveaways.getParticipants();
Expand Down Expand Up @@ -534,6 +540,7 @@ public void setGiveawayAndUsersInGift() {
participantsRepository,
listUsersRepository,
giveawayData,
finishGiveaway,
updateController);

GiveawayRegistry instance = GiveawayRegistry.getInstance();
Expand All @@ -552,16 +559,19 @@ public void setGiveawayAndUsersInGift() {

instance.putGiveawayTimer(guild_long_id, stopGiveawayByTimer, timer);
}

if (finishGiveaway) {
giveaway.stopGiveaway(count_winners);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("getMessageIdFromDB()");
}
}

@Async
@Scheduled(fixedDelay = 240000, initialDelay = 25000)
public synchronized void updateUserList() {
@Scheduled(fixedDelay = 150, initialDelay = 25, timeUnit = TimeUnit.SECONDS)
public void updateUserList() {
List<Giveaway> giveawayDataList = new LinkedList<>(GiveawayRegistry.getAllGiveaway());
for (Giveaway giveaway : giveawayDataList) {
try {
Expand All @@ -573,8 +583,7 @@ public synchronized void updateUserList() {
}
}

@Async
public synchronized void updateGiveawayByGuild(Giveaway giveawayData) {
public void updateGiveawayByGuild(Giveaway giveawayData) {
long guildIdLong = giveawayData.getGuildId();
boolean isForSpecificRole = giveawayData.isForSpecificRole();
long messageId = giveawayData.getMessageId();
Expand Down Expand Up @@ -671,20 +680,11 @@ public synchronized void updateGiveawayByGuild(Giveaway giveawayData) {

private void getLocalizationFromDB() {
try {
Connection connection = DriverManager.getConnection(URL_CONNECTION, USER_CONNECTION, PASSWORD_CONNECTION);
Statement statement = connection.createStatement();
String sql = "SELECT * FROM language";
ResultSet rs = statement.executeQuery(sql);

while (rs.next()) {
mapLanguages.put(rs.getString("server_id"), rs.getString("language"));
List<Settings> settingsList = settingsRepository.findAll();
for (Settings settings : settingsList) {
mapLanguages.put(settings.getServerId(), settings);
}

rs.close();
statement.close();
connection.close();
System.out.println("getLocalizationFromDB()");
} catch (SQLException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -693,7 +693,7 @@ private boolean hasGift(long guildIdLong) {
return GiveawayRegistry.getInstance().hasGiveaway(guildIdLong);
}

public static Map<String, String> getMapLanguages() {
public static Map<Long, Settings> getMapLanguages() {
return mapLanguages;
}
}
21 changes: 10 additions & 11 deletions src/main/java/main/controller/UpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class UpdateController {

//REPO
private final ActiveGiveawayRepository activeGiveawayRepository;
private final LanguageRepository languageRepository;
private final ParticipantsRepository participantsRepository;
private final ListUsersRepository listUsersRepository;
private final SchedulingRepository schedulingRepository;
private final SettingsRepository settingsRepository;

//LOGGER
private final static Logger LOGGER = Logger.getLogger(UpdateController.class.getName());
Expand All @@ -40,15 +40,15 @@ public class UpdateController {

@Autowired
public UpdateController(ActiveGiveawayRepository activeGiveawayRepository,
LanguageRepository languageRepository,
ParticipantsRepository participantsRepository,
ListUsersRepository listUsersRepository,
SchedulingRepository schedulingRepository) {
SchedulingRepository schedulingRepository,
SettingsRepository settingsRepository) {
this.activeGiveawayRepository = activeGiveawayRepository;
this.languageRepository = languageRepository;
this.participantsRepository = participantsRepository;
this.listUsersRepository = listUsersRepository;
this.schedulingRepository = schedulingRepository;
this.settingsRepository = settingsRepository;
}

public void registerBot(CoreBot coreBot) {
Expand Down Expand Up @@ -99,10 +99,9 @@ private void slashEvent(@NotNull SlashCommandInteractionEvent event) {
PredefinedCommand predefinedCommand = new PredefinedCommand(listUsersRepository, activeGiveawayRepository, participantsRepository);
predefinedCommand.predefined(event, this);
}

case "language" -> {
LanguageCommand languageCommand = new LanguageCommand(languageRepository);
languageCommand.language(event);
case "settings" -> {
SettingsCommand settingsCommand = new SettingsCommand(settingsRepository);
settingsCommand.language(event);
}
case "list" -> {
ListCommand listCommand = new ListCommand(participantsRepository);
Expand All @@ -117,7 +116,7 @@ private void slashEvent(@NotNull SlashCommandInteractionEvent event) {
changeCommand.change(event, this);
}
case "scheduling" -> {
SchedulingCommand schedulingCommand = new SchedulingCommand(schedulingRepository);
SchedulingCommand schedulingCommand = new SchedulingCommand(schedulingRepository, activeGiveawayRepository);
schedulingCommand.scheduling(event);
}
case "participants" -> {
Expand All @@ -142,7 +141,7 @@ private void slashEvent(@NotNull SlashCommandInteractionEvent event) {
private void buttonEvent(@NotNull ButtonInteractionEvent event) {
if (event.getGuild() == null) return;
if (Objects.equals(event.getButton().getId(), event.getGuild().getId() + ":" + ButtonChangeLanguage.CHANGE_LANGUAGE)) {
ButtonChangeLanguage buttonChangeLanguage = new ButtonChangeLanguage(languageRepository);
ButtonChangeLanguage buttonChangeLanguage = new ButtonChangeLanguage(settingsRepository);
buttonChangeLanguage.change(event);
}
}
Expand All @@ -153,7 +152,7 @@ private void joinEvent(@NotNull GuildJoinEvent event) {
}

private void leaveEvent(@NotNull GuildLeaveEvent event) {
LeaveEvent leaveEvent = new LeaveEvent(activeGiveawayRepository, languageRepository);
LeaveEvent leaveEvent = new LeaveEvent(activeGiveawayRepository, schedulingRepository);
leaveEvent.leave(event);
}

Expand Down
29 changes: 15 additions & 14 deletions src/main/java/main/core/events/ButtonChangeLanguage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,45 @@

import main.config.BotStart;
import main.jsonparser.JSONParsers;
import main.model.entity.Language;
import main.model.repository.LanguageRepository;
import main.model.entity.Settings;
import main.model.repository.SettingsRepository;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ButtonChangeLanguage {

private final LanguageRepository languageRepository;
private final SettingsRepository settingsRepository;

public static final String CHANGE_LANGUAGE = "CHANGE_LANGUAGE";
private static final JSONParsers jsonParsers = new JSONParsers();
private static final String FLAG_RUS = "\uD83C\uDDF7\uD83C\uDDFA"; //🇷🇺

@Autowired
public ButtonChangeLanguage(LanguageRepository languageRepository) {
this.languageRepository = languageRepository;
public ButtonChangeLanguage(SettingsRepository settingsRepository) {
this.settingsRepository = settingsRepository;
}

public void change(@NotNull ButtonInteractionEvent event) {
if (event.getGuild() == null) return;
long guildId = event.getGuild().getIdLong();

event.deferEdit().queue();
if (event.getButton().getEmoji() != null) {
String buttonName = event.getButton().getEmoji().getName().contains(FLAG_RUS) ? "rus" : "eng";
event.editButton(event.getButton().asDisabled()).queue();

Language language = new Language();
language.setServerId(Objects.requireNonNull(event.getGuild()).getId());
language.setLanguage(buttonName);
languageRepository.save(language);
Settings settings = new Settings();
settings.setServerId(guildId);
settings.setLanguage(buttonName);
settingsRepository.save(settings);

BotStart.getMapLanguages().put(event.getGuild().getId(), buttonName);
String buttonLanguage = String.format(jsonParsers.getLocale("button_language", event.getGuild().getId()), buttonName);
BotStart.getMapLanguages().put(guildId, settings);
String buttonLanguage = String.format(jsonParsers.getLocale("button_language", guildId), buttonName);

event.getHook().sendMessage(buttonLanguage).setEphemeral(true).queue();
}
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/main/core/events/ChangeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public ChangeCommand(ActiveGiveawayRepository activeGiveawayRepository) {
}

public void change(@NotNull SlashCommandInteractionEvent event, UpdateController updateController) {

var guildIdLong = Objects.requireNonNull(event.getGuild()).getIdLong();
var guildId = Objects.requireNonNull(event.getGuild()).getId();
var guildId = Objects.requireNonNull(event.getGuild()).getIdLong();

Giveaway giveaway = GiveawayRegistry.getInstance().getGiveaway(guildIdLong);
if (giveaway == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/main/core/events/CheckBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class CheckBot {
private static final JSONParsers jsonParsers = new JSONParsers();

public void check(@NotNull SlashCommandInteractionEvent event) {
var guildId = Objects.requireNonNull(event.getGuild()).getId();
var guildId = Objects.requireNonNull(event.getGuild()).getIdLong();

GuildChannelUnion textChannel = event.getOption("textchannel", OptionMapping::getAsChannel);
GuildChannel guildChannel = textChannel != null ? textChannel : event.getGuildChannel().asTextChannel();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/main/core/events/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HelpCommand {
private final static JSONParsers jsonParsers = new JSONParsers();

public void help(@NotNull SlashCommandInteractionEvent event) {
var guildId = Objects.requireNonNull(event.getGuild()).getId();
var guildId = Objects.requireNonNull(event.getGuild()).getIdLong();

EmbedBuilder info = new EmbedBuilder();
info.setColor(Color.GREEN);
Expand Down
Loading

0 comments on commit 48c4eaf

Please sign in to comment.