Skip to content
Open
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 @@ -2,6 +2,8 @@ plugins {
id'java'
id'application'
id'com.github.johnrengelman.shadow' version '5.2.0'
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

group 'de.foursoft'
Expand All @@ -20,6 +22,7 @@ repositories {
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation ('net.dv8tion:JDA:4.3.0_310') {
exclude module: 'opus-java'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
29 changes: 29 additions & 0 deletions src/main/java/de/foursoft/discordbot/BotConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.foursoft.discordbot;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import net.dv8tion.jda.api.hooks.EventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Proxy;

@Configuration
public class BotConfiguration {

@Autowired
ListenerAdapterDispatcher dispatcher;

@Bean
public EventWaiter eventWaiter(){
return new EventWaiter();
}

@Bean
public EventListener eventListener(){
return (EventListener) Proxy.newProxyInstance(
BotConfiguration.class.getClassLoader(),
new Class[] { EventListener.class },
dispatcher);
}
}
49 changes: 49 additions & 0 deletions src/main/java/de/foursoft/discordbot/BotInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.foursoft.discordbot;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.security.auth.login.LoginException;

@Component
public class BotInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(FourSoftDiscordBot.class);

@Autowired
private EventWaiter eventWaiter;
@Autowired
private EventListener eventListener;

@PostConstruct
private void init(){
Configuration config = new Configuration("config.properties");

try {
JDABuilder.createDefault(config.getToken()) // init default settings
.setChunkingFilter(ChunkingFilter.ALL) // load all members
.enableIntents(
GatewayIntent.GUILD_MEMBERS, // allow access to guild members
GatewayIntent.GUILD_PRESENCES // access to online status and activities
)
.setMemberCachePolicy(MemberCachePolicy.ALL) // always cache members
.addEventListeners(eventListener)
.addEventListeners(eventWaiter) // waits for events
.build().awaitReady(); // wait until the API is ready
} catch (LoginException e) {
LOGGER.error("Could not start the Bot, invalid Token!");
System.exit(-1);
} catch (InterruptedException e) {
LOGGER.error("Could not start the Bot, interrupted while waiting!");
System.exit(-1);
}
}
}
113 changes: 0 additions & 113 deletions src/main/java/de/foursoft/discordbot/BotListener.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/main/java/de/foursoft/discordbot/CommandRegistry.java

This file was deleted.

65 changes: 6 additions & 59 deletions src/main/java/de/foursoft/discordbot/FourSoftDiscordBot.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,15 @@
package de.foursoft.discordbot;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
import de.foursoft.discordbot.commands.PingCommand;
import de.foursoft.discordbot.commands.ReactCommand;
import de.foursoft.discordbot.commands.ResetCommand;
import de.foursoft.discordbot.commands.SecretCommand;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.login.LoginException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("de.foursoft")
public class FourSoftDiscordBot {

private static final Logger LOGGER = LoggerFactory.getLogger(FourSoftDiscordBot.class);

private final JDA discordApi;

private final EventWaiter eventWaiter = new EventWaiter();

private final CommandRegistry commandRegistry = new CommandRegistry();

public FourSoftDiscordBot() {
Configuration config = new Configuration("config.properties");

// init commands
commandRegistry.addCommand("ping", new PingCommand())
.addCommand("react", new ReactCommand())
.addCommand("secret", new SecretCommand(eventWaiter))
.addCommand("reset", new ResetCommand());

JDA tmpJda = null;
try {
tmpJda = JDABuilder.createDefault(config.getToken()) // init default settings
.setChunkingFilter(ChunkingFilter.ALL) // load all members
.enableIntents(
GatewayIntent.GUILD_MEMBERS, // allow access to guild members
GatewayIntent.GUILD_PRESENCES // access to online status and activities
)
.setMemberCachePolicy(MemberCachePolicy.ALL) // always cache members
.addEventListeners(new BotListener(commandRegistry)) // enable the class to receive events
.addEventListeners(eventWaiter) // waits for events
.build().awaitReady(); // wait until the API is ready
} catch (LoginException e) {
LOGGER.error("Could not start the Bot, invalid Token!");
System.exit(-1);
} catch (InterruptedException e) {
LOGGER.error("Could not start the Bot, interrupted while waiting!");
System.exit(-1);
}

discordApi = tmpJda;
}

public JDA getDiscordApi() {
return discordApi;
}

public static void main(String[] args) {
new FourSoftDiscordBot();
SpringApplication.run(FourSoftDiscordBot.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.foursoft.discordbot;

import de.foursoft.discordbot.eventconsumer.EventConsumer;
import net.dv8tion.jda.api.events.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ListenerAdapterDispatcher implements InvocationHandler {

@Autowired
private List<EventConsumer> allEventConsumers;

private Map<Class, List<EventConsumer>> consumerByEvent;

@PostConstruct
private void init(){
consumerByEvent = allEventConsumers.stream().collect(Collectors.groupingBy(EventConsumer::getClassOfT));
}


@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

if(args.length != 1){
return null;
}

Object event = args[0];

consumerByEvent.getOrDefault(event.getClass(), Collections.emptyList())
.forEach(c -> c.execute((Event) event));

return null;
}
}
10 changes: 0 additions & 10 deletions src/main/java/de/foursoft/discordbot/commands/Command.java

This file was deleted.

This file was deleted.

Loading