Skip to content

Commit

Permalink
fix : 디스코드 1주일 예약 알람 데이터 최신화 받게끔 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonSangWon committed Dec 10, 2023
1 parent 6f10254 commit 4401995
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/main/java/mega/waka/ServerApplication.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package mega.waka;

import mega.waka.discord.DiscordListener;
import mega.waka.entity.Member;
import mega.waka.entity.editor.SevenDaysEditor;
import mega.waka.repository.MemberRepository;
import mega.waka.service.SevenDaysWakaService;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class ServerApplication {
private final MemberRepository memberRepository;
private final SevenDaysWakaService sevenDaysWakaService;
@Value("${discord.bot.token}")
private String token;

@Autowired
public ServerApplication(MemberRepository memberRepository, SevenDaysWakaService sevenDaysWakaService) {
this.memberRepository = memberRepository;
this.sevenDaysWakaService = sevenDaysWakaService;
Expand All @@ -45,7 +43,7 @@ public void startBot() {
JDA jda = JDABuilder.createDefault(token)
.setActivity(Activity.playing("코딩"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new DiscordListener(memberRepository))
.addEventListeners(new DiscordListener(memberRepository, sevenDaysWakaService))
.build();
jda.upsertCommand("개인순위","금주 와카타임 개인순위를 제공합니다.").setGuildOnly(true).queue();
jda.upsertCommand("전체순위","금주 와카타임 전체순위를 제공합니다.").setGuildOnly(true).queue();
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/mega/waka/discord/DiscordListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import mega.waka.entity.language.SevenDaysLanguage;
import mega.waka.entity.project.SevenDaysProject;
import mega.waka.repository.MemberRepository;
import mega.waka.service.SevenDaysWakaService;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.awt.*;
Expand All @@ -33,9 +32,11 @@
public class DiscordListener extends ListenerAdapter {

private final MemberRepository memberRepository;
private final SevenDaysWakaService sevenDaysWakaService;
@Autowired
public DiscordListener(MemberRepository memberRepository) {
public DiscordListener(MemberRepository memberRepository, SevenDaysWakaService sevenDaysWakaService) {
this.memberRepository = memberRepository;
this.sevenDaysWakaService = sevenDaysWakaService;
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { //test->1116650046797135992, 1090659127417638943
Expand Down Expand Up @@ -78,6 +79,7 @@ public void onReady(ReadyEvent event) {
}

public void sendToSchedule(@NotNull ReadyEvent readyEvent){
sevenDaysWakaService.update_SevenDays();
Map<String, Integer> memberMap = returnToMemberTimeByMap();
List<Map.Entry<String,Integer>> sortedList = new ArrayList<>(memberMap.entrySet());
StringBuilder messange = returnToOverallRanking(sortedList);
Expand All @@ -102,6 +104,7 @@ public void sendToSchedule(@NotNull ReadyEvent readyEvent){
}
public Map<String, Integer> returnToMemberTimeByMap (){
Map<String, Integer> memberMap = new HashMap<>();
sevenDaysWakaService.update_SevenDays();
List<Member> memberList = memberRepository.findAll();
for(Member member : memberList){
Pattern pattern = Pattern.compile("\\b(\\d+) hrs (\\d+) min(s)?\\b");
Expand Down Expand Up @@ -286,8 +289,8 @@ public long calculate_NextFriday_FromToday_ToSecond(){
.withHour(0)
.withMinute(0)
.withSecond(0);

if (now.getDayOfWeek() == DayOfWeek.FRIDAY && now.isAfter(nextFriday)) {
if (now.getDayOfWeek() == DayOfWeek.SATURDAY && now.isAfter(nextFriday)) {
nextFriday = nextFriday.plusWeeks(1);
}
nextFriday = nextFriday.withHour(0).withMinute(0);
Expand Down

0 comments on commit 4401995

Please sign in to comment.