Skip to content

Commit

Permalink
fix : test
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonSangWon committed Oct 22, 2023
1 parent 610b4c7 commit ad07100
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/main/java/mega/waka/ServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
@SpringBootApplication
public class ServerApplication {
private static MemberRepository memberRepository;
private static SevenDaysWakaService sevenDaysWakaService;

public ServerApplication(MemberRepository memberRepository, SevenDaysWakaService sevenDaysWakaService) {
public ServerApplication(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
this.sevenDaysWakaService = sevenDaysWakaService;
}

public static void main(String[] args) {
Expand All @@ -32,7 +30,7 @@ public static void main(String[] args) {
JDA jda = JDABuilder.createDefault(discordBotToken)
.setActivity(Activity.playing("코딩"))
.enableIntents(GatewayIntent.MESSAGE_CONTENT)
.addEventListeners(new DiscordListener(memberRepository, sevenDaysWakaService))
.addEventListeners(new DiscordListener(memberRepository))
.build();
jda.upsertCommand("개인순위","금주 와카타임 개인순위를 제공합니다.").setGuildOnly(true).queue();
jda.upsertCommand("전체순위","금주 와카타임 전체순위를 제공합니다.").setGuildOnly(true).queue();
Expand Down
56 changes: 51 additions & 5 deletions src/main/java/mega/waka/discord/DiscordListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mega.waka.discord;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.NotNull;
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.EmbedBuilder;
Expand All @@ -13,6 +13,7 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -39,16 +40,61 @@
public class DiscordListener extends ListenerAdapter {

private final MemberRepository memberRepository;
private final SevenDaysWakaService sevenDaysWakaService;
public DiscordListener(MemberRepository memberRepository, SevenDaysWakaService sevenDaysWakaService) {

public DiscordListener(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
this.sevenDaysWakaService = sevenDaysWakaService;
}
@Transactional
@JsonIgnore
public void update_SevenDays(){
List<Member> members = memberRepository.findAll();
String responseData="";
try {
RestTemplate restTemplate = new RestTemplate();
String apiUrl ="https://wakatime.com/api/v1/users/current/summaries?range=last_7_days";
for (Member member : members) {

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiUrl);

HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(member.getSecretKey(),"");

ResponseEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
new HttpEntity<>(headers),
String.class
);
responseData = response.getBody();

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(responseData);
JSONObject cumulative_total = (JSONObject) jsonObject.get("cumulative_total");
member.setSevenDays(cumulative_total.get("text").toString());

memberRepository.save(member);

DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
if(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREAN).equals("금요일") && !member.getMoney().getUpdateDate().isEqual(LocalDate.now())){
int money = member.getMoney().getAmount();
String [] time = member.getSevenDays().split(" ");
int hour = Integer.valueOf(time[0]);
money += (hour *9620)/10000;
member.getMoney().setAmount(money);
member.getMoney().setUpdateDate(LocalDate.now());
memberRepository.save(member);
}

}
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
if(!event.getChannel().getId().equals("1090659127417638943")) return;
super.onSlashCommandInteraction(event);
sevenDaysWakaService.update_SevenDays();
update_SevenDays();
User user = event.getUser();
String returnMessage = "";
String newMessage = "!!!!!***근무 시간 미달자 ***!!!! \n";
Expand Down

0 comments on commit ad07100

Please sign in to comment.