File tree Expand file tree Collapse file tree 11 files changed +234
-0
lines changed Expand file tree Collapse file tree 11 files changed +234
-0
lines changed Original file line number Diff line number Diff line change
1
+ Command to generate project:
2
+ mvn archetype: generate -DarchetypeGroupId=org.mneidinger.maven.archetype -DarchetypeArtifactId=Archetype-Discord-Bot -DarchetypeVersion=0.0.1-SNAPSHOT -DgroupId=org.mneidinger.discordbot -DartifactId=DiscordPollBot -Dversion=1.0.0-SNAPSHOT -Dgoals=compiler: compile
Original file line number Diff line number Diff line change
1
+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+
5
+ <modelVersion >4.0.0</modelVersion >
6
+ <groupId >org.mneidinger.maven.archetype</groupId >
7
+ <artifactId >Archetype-Discord-Bot</artifactId >
8
+ <version >0.0.1-SNAPSHOT</version >
9
+ <packaging >maven-archetype</packaging >
10
+
11
+ <build >
12
+ <extensions >
13
+ <extension >
14
+ <groupId >org.apache.maven.archetype</groupId >
15
+ <artifactId >archetype-packaging</artifactId >
16
+ <version >3.1.1</version >
17
+ </extension >
18
+ </extensions >
19
+ </build >
20
+
21
+ </project >
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <archetype-descriptor
3
+ xmlns=" https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0"
4
+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
5
+ xsi:schemaLocation=" https://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.1.0 https://maven.apache.org/xsd/archetype-descriptor-1.1.0.xsd"
6
+ name=" quickstart" >
7
+
8
+ <fileSets >
9
+ <fileSet filtered =" true" packaged =" true" >
10
+ <directory >src/main/java</directory >
11
+ <includes >
12
+ <include >**/*.java</include >
13
+ </includes >
14
+ </fileSet >
15
+ <fileSet >
16
+ <directory >src/main/resources</directory >
17
+ </fileSet >
18
+ <fileSet >
19
+ <directory >src/test/java</directory >
20
+ </fileSet >
21
+ <fileSet >
22
+ <directory >src/test/resources</directory >
23
+ </fileSet >
24
+ </fileSets >
25
+
26
+ <requiredProperties >
27
+ <requiredProperty key =" discord4jVersion" >
28
+ <defaultValue >3.3.0-RC1</defaultValue >
29
+ </requiredProperty >
30
+ <requiredProperty key =" springBootStarterVersion" >
31
+ <defaultValue >3.4.2</defaultValue >
32
+ </requiredProperty >
33
+ </requiredProperties >
34
+
35
+ </archetype-descriptor >
Original file line number Diff line number Diff line change
1
+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
2
+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
3
+ xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
4
+ <modelVersion >4.0.0</modelVersion >
5
+
6
+ <groupId >${groupId} </groupId >
7
+ <artifactId >${artifactId} </artifactId >
8
+ <version >${version} </version >
9
+ <packaging >jar</packaging >
10
+
11
+ <name >${artifactId} </name >
12
+
13
+ <properties >
14
+ <maven .compiler.release>21</maven .compiler.release>
15
+ </properties >
16
+
17
+ <dependencies >
18
+ <dependency >
19
+ <groupId >com.discord4j</groupId >
20
+ <artifactId >discord4j-core</artifactId >
21
+ <version >${discord4jVersion} </version >
22
+ </dependency >
23
+ <dependency >
24
+ <groupId >org.springframework.boot</groupId >
25
+ <artifactId >spring-boot-starter</artifactId >
26
+ <version >${springBootStarterVersion} </version >
27
+ </dependency >
28
+ <dependency >
29
+ <groupId >org.springframework.boot</groupId >
30
+ <artifactId >spring-boot-starter-webflux</artifactId >
31
+ <version >${springBootStarterVersion} </version >
32
+ </dependency >
33
+ </dependencies >
34
+
35
+ </project >
Original file line number Diff line number Diff line change
1
+ package $ {package };
2
+
3
+ import java .util .List ;
4
+
5
+ import org .mneidinger .discordbot .BotConfiguration ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+ import org .springframework .beans .factory .annotation .Value ;
9
+ import org .springframework .context .annotation .Bean ;
10
+ import org .springframework .context .annotation .Configuration ;
11
+
12
+ import discord4j .core .DiscordClientBuilder ;
13
+ import discord4j .core .GatewayDiscordClient ;
14
+ import discord4j .core .event .domain .Event ;
15
+
16
+ import $ {package }.event .*;
17
+
18
+ @ Configuration
19
+ public class BotConfiguration {
20
+
21
+ private static final Logger log = LoggerFactory .getLogger ( BotConfiguration .class );
22
+
23
+ @ Value ("${token}" )
24
+ private String token ;
25
+
26
+ @ Bean
27
+ <T extends Event > GatewayDiscordClient gatewayDiscordClient (List <EventListener <T >> eventListeners ) {
28
+
29
+ try {
30
+ GatewayDiscordClient client = DiscordClientBuilder .create (token )
31
+ .build ()
32
+ .login ()
33
+ .block ();
34
+
35
+ for (EventListener <T > listener : eventListeners ) {
36
+ client .on (listener .getEventType ())
37
+ .flatMap (listener ::execute )
38
+ .onErrorResume (listener ::handleError )
39
+ .subscribe ();
40
+ }
41
+
42
+ return client ;
43
+ }catch (Exception e ) {
44
+ log .error ("Exception!: " , e );
45
+ }
46
+ return null ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ package $ {package };
2
+
3
+ import org .springframework .boot .SpringApplication ;
4
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
+
6
+ @ SpringBootApplication
7
+ public class $ {artifactId }Application {
8
+
9
+ public static void main (String [] args ) {
10
+ SpringApplication .run ($ {artifactId }Application .class , args );
11
+ }
12
+
13
+ }
Original file line number Diff line number Diff line change
1
+ package $ {package }.event ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+
6
+ import discord4j .core .event .domain .Event ;
7
+ import reactor .core .publisher .Mono ;
8
+
9
+ public interface EventListener <T extends Event > {
10
+
11
+ Logger LOG = LoggerFactory .getLogger (EventListener .class );
12
+
13
+ Class <T > getEventType ();
14
+ Mono <Void > execute (T event );
15
+
16
+ default Mono <Void > handleError (Throwable error ){
17
+ LOG .error ("Unable to process " + getEventType ().getSimpleName (), error );
18
+ return Mono .empty ();
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package $ {package }.event ;
2
+
3
+ import org .springframework .stereotype .Service ;
4
+
5
+ import discord4j .core .event .domain .message .MessageCreateEvent ;
6
+ import reactor .core .publisher .Mono ;
7
+
8
+ @ Service
9
+ public class MessageCreateListener extends MessageListener implements EventListener <MessageCreateEvent > {
10
+
11
+ @ Override
12
+ public Class <MessageCreateEvent > getEventType () {
13
+ return MessageCreateEvent .class ;
14
+ }
15
+
16
+ @ Override
17
+ public Mono <Void > execute (MessageCreateEvent event ) {
18
+ return processCommand (event .getMessage ());
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package $ {package }.event ;
2
+
3
+ import discord4j .core .object .entity .Message ;
4
+ import reactor .core .publisher .Mono ;
5
+
6
+ public abstract class MessageListener {
7
+
8
+ public Mono <Void > processCommand (Message eventMessage ){
9
+ return Mono .just (eventMessage )
10
+ .filter (message -> message .getAuthor ().map (user -> !user .isBot ()).orElse (false ))
11
+ .filter (message -> message .getContent ().equalsIgnoreCase ("!todo" ))
12
+ .flatMap (Message ::getChannel )
13
+ .flatMap (channel -> channel .createMessage ("Things to do today:\n - write a bot\n - eat lunch\n - play a game" ))
14
+ .then ();
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ package $ {package }.event ;
2
+
3
+ import org .springframework .stereotype .Service ;
4
+
5
+ import discord4j .core .event .domain .message .MessageUpdateEvent ;
6
+ import reactor .core .publisher .Mono ;
7
+
8
+ @ Service
9
+ public class MessageUpdateListener extends MessageListener implements EventListener <MessageUpdateEvent > {
10
+
11
+ @ Override
12
+ public Class <MessageUpdateEvent > getEventType () {
13
+ return MessageUpdateEvent .class ;
14
+ }
15
+
16
+ @ Override
17
+ public Mono <Void > execute (MessageUpdateEvent event ) {
18
+ return Mono .just (event )
19
+ .filter (MessageUpdateEvent ::isContentChanged )
20
+ .flatMap (MessageUpdateEvent ::getMessage )
21
+ .flatMap (super ::processCommand );
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments