JAVA-20381 Moved modules
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
@@ -0,0 +1,7 @@
|
||||
## DISCORD4J
|
||||
|
||||
This module contains articles about Discord4J
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Creating a Discord Bot with Discord4J + Spring Boot](https://www.baeldung.com/spring-discord4j-bot)
|
||||
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>discord4j</artifactId>
|
||||
<name>discord4j</name>
|
||||
<description>Demo Discord bot using Discord4J + Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>saas-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.discord4j</groupId>
|
||||
<artifactId>discord4j-core</artifactId>
|
||||
<version>${discord4j-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<discord4j-core.version>3.1.1</discord4j-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.discordbot;
|
||||
|
||||
import com.baeldung.discordbot.events.EventListener;
|
||||
import discord4j.core.DiscordClientBuilder;
|
||||
import discord4j.core.GatewayDiscordClient;
|
||||
import discord4j.core.event.domain.Event;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class BotConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger( BotConfiguration.class );
|
||||
|
||||
@Value("${token}")
|
||||
private String token;
|
||||
|
||||
@Bean
|
||||
public <T extends Event> GatewayDiscordClient gatewayDiscordClient(List<EventListener<T>> eventListeners) {
|
||||
GatewayDiscordClient client = null;
|
||||
|
||||
try {
|
||||
client = DiscordClientBuilder.create(token)
|
||||
.build()
|
||||
.login()
|
||||
.block();
|
||||
|
||||
for(EventListener<T> listener : eventListeners) {
|
||||
client.on(listener.getEventType())
|
||||
.flatMap(listener::execute)
|
||||
.onErrorResume(listener::handleError)
|
||||
.subscribe();
|
||||
}
|
||||
}
|
||||
catch ( Exception exception ) {
|
||||
log.error( "Be sure to use a valid bot token!", exception );
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.discordbot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DiscordBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DiscordBotApplication.class, args);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.discordbot.events;
|
||||
|
||||
import discord4j.core.event.domain.Event;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public interface EventListener<T extends Event> {
|
||||
|
||||
Logger LOG = LoggerFactory.getLogger(EventListener.class);
|
||||
|
||||
Class<T> getEventType();
|
||||
Mono<Void> execute(T event);
|
||||
|
||||
default Mono<Void> handleError(Throwable error) {
|
||||
LOG.error("Unable to process " + getEventType().getSimpleName(), error);
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.discordbot.events;
|
||||
|
||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class MessageCreateListener extends MessageListener implements EventListener<MessageCreateEvent> {
|
||||
|
||||
@Override
|
||||
public Class<MessageCreateEvent> getEventType() {
|
||||
return MessageCreateEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
return processCommand(event.getMessage());
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.discordbot.events;
|
||||
|
||||
import discord4j.core.object.entity.Message;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public abstract class MessageListener {
|
||||
|
||||
public Mono<Void> processCommand(Message eventMessage) {
|
||||
return Mono.just(eventMessage)
|
||||
.filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
|
||||
.filter(message -> message.getContent().equalsIgnoreCase("!todo"))
|
||||
.flatMap(Message::getChannel)
|
||||
.flatMap(channel -> channel.createMessage("Things to do today:\n" +
|
||||
" - write a bot\n" +
|
||||
" - eat lunch\n" +
|
||||
" - play a game"))
|
||||
.then();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.discordbot.events;
|
||||
|
||||
import discord4j.core.event.domain.message.MessageUpdateEvent;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class MessageUpdateListener extends MessageListener implements EventListener<MessageUpdateEvent> {
|
||||
|
||||
@Override
|
||||
public Class<MessageUpdateEvent> getEventType() {
|
||||
return MessageUpdateEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> execute(MessageUpdateEvent event) {
|
||||
return Mono.just(event)
|
||||
.filter(MessageUpdateEvent::isContentChanged)
|
||||
.flatMap(MessageUpdateEvent::getMessage)
|
||||
.flatMap(super::processCommand);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
token: 'our-token-here'
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.discordbot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class DiscordBotLiveTest {
|
||||
|
||||
@Test
|
||||
public void contextTest() {
|
||||
}
|
||||
}
|
||||
@@ -16,11 +16,13 @@
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>discord4j</module>
|
||||
<module>jira-rest-integration</module>
|
||||
<module>sentry-servlet</module>
|
||||
<module>slack</module>
|
||||
<module>stripe</module>
|
||||
<module>twilio</module>
|
||||
<module>twitter4j</module>
|
||||
<module>sentry-servlet</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Create a Slack Plugin in Java](https://www.baeldung.com/java-slack-plugin)
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.examples</groupId>
|
||||
<artifactId>slack</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>slack</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>saas-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.hubspot.slack</groupId>
|
||||
<artifactId>slack-base</artifactId>
|
||||
<version>${slack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.hubspot.slack</groupId>
|
||||
<artifactId>slack-java-client</artifactId>
|
||||
<version>${slack.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.examples.slack.MainClass</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.examples.slack.MainClass</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<slack.version>1.4</slack.version>
|
||||
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class DiskSpaceErrorChecker implements ErrorChecker {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DiskSpaceErrorChecker.class);
|
||||
|
||||
private final ErrorReporter errorReporter;
|
||||
|
||||
private final double limit;
|
||||
|
||||
public DiskSpaceErrorChecker(ErrorReporter errorReporter, double limit) {
|
||||
this.errorReporter = errorReporter;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void check() {
|
||||
LOG.info("Checking disk space");
|
||||
FileSystems.getDefault().getFileStores().forEach(fileStore -> {
|
||||
try {
|
||||
long totalSpace = fileStore.getTotalSpace();
|
||||
long usableSpace = fileStore.getUsableSpace();
|
||||
double usablePercentage = ((double) usableSpace) / totalSpace;
|
||||
LOG.debug("File store {} has {} of {} ({}) usable space",
|
||||
fileStore, usableSpace, totalSpace, usablePercentage);
|
||||
|
||||
if (totalSpace > 0 && usablePercentage < limit) {
|
||||
String error = String.format("File store %s only has %d%% usable disk space",
|
||||
fileStore.name(), (int)(usablePercentage * 100));
|
||||
errorReporter.reportProblem(error);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error getting disk space for file store {}", fileStore, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
public interface ErrorChecker {
|
||||
void check();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
public interface ErrorReporter {
|
||||
void reportProblem(String problem);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import com.hubspot.slack.client.SlackClient;
|
||||
import com.hubspot.slack.client.SlackClientFactory;
|
||||
import com.hubspot.slack.client.SlackClientRuntimeConfig;
|
||||
|
||||
public class MainClass {
|
||||
public static final long MINUTES = 1000 * 60;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
SlackClientRuntimeConfig runtimeConfig = SlackClientRuntimeConfig.builder()
|
||||
.setTokenSupplier(() -> "<Your API Token>")
|
||||
.build();
|
||||
|
||||
SlackClient slackClient = SlackClientFactory.defaultFactory().build(runtimeConfig);
|
||||
|
||||
ErrorReporter slackChannelErrorReporter = new SlackChannelErrorReporter(slackClient, "general");
|
||||
ErrorReporter slackUserErrorReporter = new SlackUserErrorReporter(slackClient, "[email protected]");
|
||||
|
||||
ErrorChecker diskSpaceErrorChecker10pct = new DiskSpaceErrorChecker(slackChannelErrorReporter, 0.1);
|
||||
ErrorChecker diskSpaceErrorChecker2pct = new DiskSpaceErrorChecker(slackUserErrorReporter, 0.02);
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
diskSpaceErrorChecker10pct.check();
|
||||
}
|
||||
}, 0, 5 * MINUTES);
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
diskSpaceErrorChecker2pct.check();
|
||||
}
|
||||
}, 0, 5 * MINUTES);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
import com.hubspot.slack.client.SlackClient;
|
||||
import com.hubspot.slack.client.methods.params.chat.ChatPostMessageParams;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SlackChannelErrorReporter implements ErrorReporter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SlackChannelErrorReporter.class);
|
||||
|
||||
private final SlackClient slackClient;
|
||||
|
||||
private final String channel;
|
||||
|
||||
public SlackChannelErrorReporter(SlackClient slackClient, String channel) {
|
||||
this.slackClient = slackClient;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportProblem(String problem) {
|
||||
LOG.debug("Sending message to channel {}: {}", channel, problem);
|
||||
slackClient.postMessage(
|
||||
ChatPostMessageParams.builder()
|
||||
.setText(problem)
|
||||
.setChannelId(channel)
|
||||
.build()
|
||||
).join().unwrapOrElseThrow();
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.examples.slack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.hubspot.slack.client.SlackClient;
|
||||
import com.hubspot.slack.client.methods.params.chat.ChatPostMessageParams;
|
||||
import com.hubspot.slack.client.methods.params.conversations.ConversationCreateParams;
|
||||
import com.hubspot.slack.client.methods.params.im.ImOpenParams;
|
||||
import com.hubspot.slack.client.methods.params.users.UserEmailParams;
|
||||
import com.hubspot.slack.client.methods.params.users.UsersInfoParams;
|
||||
import com.hubspot.slack.client.models.response.im.ImOpenResponse;
|
||||
import com.hubspot.slack.client.models.response.users.UsersInfoResponse;
|
||||
import com.hubspot.slack.client.models.users.SlackUser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SlackUserErrorReporter implements ErrorReporter {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SlackUserErrorReporter.class);
|
||||
|
||||
private final SlackClient slackClient;
|
||||
|
||||
private final String user;
|
||||
|
||||
public SlackUserErrorReporter(SlackClient slackClient, String user) {
|
||||
this.slackClient = slackClient;
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reportProblem(String problem) {
|
||||
LOG.debug("Sending message to user {}: {}", user, problem);
|
||||
UsersInfoResponse usersInfoResponse = slackClient
|
||||
.lookupUserByEmail(UserEmailParams.builder()
|
||||
.setEmail(user)
|
||||
.build()
|
||||
).join().unwrapOrElseThrow();
|
||||
|
||||
ImOpenResponse imOpenResponse = slackClient.openIm(ImOpenParams.builder()
|
||||
.setUserId(usersInfoResponse.getUser().getId())
|
||||
.build()
|
||||
).join().unwrapOrElseThrow();
|
||||
|
||||
imOpenResponse.getChannel().ifPresent(channel -> {
|
||||
slackClient.postMessage(
|
||||
ChatPostMessageParams.builder()
|
||||
.setText(problem)
|
||||
.setChannelId(channel.getId())
|
||||
.build()
|
||||
).join().unwrapOrElseThrow();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user