Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 디스코드 연동 세팅 #69

Merged
merged 14 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ dependencies {

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Discord
implementation 'net.dv8tion:JDA:5.0.0-beta.20'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gdschongik.gdsc.domain.discord.listener;

import com.gdschongik.gdsc.global.discord.Listener;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

@Slf4j
@Listener
public class PingpongListener extends ListenerAdapter {

@Override
public void onMessageReceived(MessageReceivedEvent event) {
User author = event.getAuthor();
TextChannel channel = event.getChannel().asTextChannel();
Message message = event.getMessage();
String content = message.getContentRaw(); // get only textual content of message

log.info("Message from {} in {}: {}", author.getName(), channel.getName(), message.getContentDisplay());

if (author.isBot()) return;

if (content.equals("!ping")) {
channel.sendMessage("Pong!").queue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.gdschongik.gdsc.global.config;

import com.gdschongik.gdsc.global.discord.ListenerBeanPostProcessor;
import com.gdschongik.gdsc.global.property.DiscordProperty;
import lombok.RequiredArgsConstructor;
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 net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class DiscordConfig {

private final DiscordProperty discordProperty;

@Bean
@ConditionalOnProperty(value = "discord.enabled", havingValue = "true", matchIfMissing = true)
public JDA jda() {
return JDABuilder.createDefault(discordProperty.getToken())
.setActivity(Activity.playing("테스트"))
.enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MEMBERS)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.build();
}

@Bean
@ConditionalOnBean(JDA.class)
public ListenerBeanPostProcessor listenerBeanPostProcessor(JDA jda) {
return new ListenerBeanPostProcessor(jda);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.gdschongik.gdsc.global.config;

import com.gdschongik.gdsc.global.property.DiscordProperty;
import com.gdschongik.gdsc.global.property.JwtProperty;
import com.gdschongik.gdsc.global.property.RedisProperty;
import com.gdschongik.gdsc.global.property.SwaggerProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties({JwtProperty.class, RedisProperty.class, SwaggerProperty.class})
@EnableConfigurationProperties({JwtProperty.class, RedisProperty.class, SwaggerProperty.class, DiscordProperty.class})
@Configuration
public class PropertyConfig {}
12 changes: 12 additions & 0 deletions src/main/java/com/gdschongik/gdsc/global/discord/Listener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gdschongik.gdsc.global.discord;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Listener {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.gdschongik.gdsc.global.discord;

import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.JDA;
import org.springframework.beans.factory.config.BeanPostProcessor;

@RequiredArgsConstructor
public class ListenerBeanPostProcessor implements BeanPostProcessor {

private final JDA jda;

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean.getClass().isAnnotationPresent(Listener.class)) {
jda.addEventListener(bean);
}
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gdschongik.gdsc.global.property;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Getter
@AllArgsConstructor
@ConfigurationProperties(prefix = "discord")
public class DiscordProperty {

private final String token;
}
2 changes: 2 additions & 0 deletions src/main/resources/application-discord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
discord:
token: ${DISCORD_BOT_TOKEN:}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spring:
- security
- swagger
- actuator
- discord

logging:
level:
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/gdschongik/gdsc/GdscApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class GdscApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class AdminMemberServiceTest {
@Autowired
private MemberRepository memberRepository;
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ spring:

datasource:
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL

discord:
enabled: false
Loading