Bael 4700 (#10231)
* Added code examples from the article. * Hide the revoked token * Reduce long line break indentation to 2 spaces, change "your" to "our," and use the -LiveTest convention for the test class. * Reduce Java version from 11 to 1.8 and add the new discord4j module to the root pom. * Remove test due to context failing to start up without a valid bot token. * Replace test class and add error handling for invalid tokens.
This commit is contained in:
parent
42276ed18a
commit
a5d7f70a5f
|
@ -0,0 +1,33 @@
|
||||||
|
README.md
|
||||||
|
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,63 @@
|
||||||
|
<?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>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.3.5.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>discord4j-bot</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>discord4j-bot</name>
|
||||||
|
<description>Demo Discord bot using Discord4J + Spring Boot</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</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>3.1.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,48 @@
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
client.onDisconnect().block();
|
||||||
|
}
|
||||||
|
catch ( Exception exception ) {
|
||||||
|
log.error( "Be sure to use a valid bot token!", exception );
|
||||||
|
}
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {
|
||||||
|
}
|
||||||
|
}
|
2
pom.xml
2
pom.xml
|
@ -397,6 +397,7 @@
|
||||||
<module>ddd</module>
|
<module>ddd</module>
|
||||||
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
||||||
<module>deeplearning4j</module>
|
<module>deeplearning4j</module>
|
||||||
|
<module>discord4j</module>
|
||||||
<module>disruptor</module>
|
<module>disruptor</module>
|
||||||
<module>dozer</module>
|
<module>dozer</module>
|
||||||
<module>drools</module>
|
<module>drools</module>
|
||||||
|
@ -909,6 +910,7 @@
|
||||||
<module>ddd</module>
|
<module>ddd</module>
|
||||||
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
||||||
<module>deeplearning4j</module>
|
<module>deeplearning4j</module>
|
||||||
|
<module>discord4j</module>
|
||||||
<module>disruptor</module>
|
<module>disruptor</module>
|
||||||
<module>dozer</module>
|
<module>dozer</module>
|
||||||
<module>drools</module>
|
<module>drools</module>
|
||||||
|
|
Loading…
Reference in New Issue