Skip to content

Commit

Permalink
refactor: remove internal discord api from api package
Browse files Browse the repository at this point in the history
  • Loading branch information
bendavies99 committed Jun 7, 2023
1 parent 24b24aa commit df8223e
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 124 deletions.
5 changes: 4 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ dependencyManagement {
}

dependencies {
api 'com.discord4j:discord4j-core:3.2.4'
api 'org.springframework.boot:spring-boot-starter'
api "io.projectreactor:reactor-core:3.5.6"
api 'org.projectlombok:lombok'
api group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.15.2'
api group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.15.2'
api group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.15.2'
annotationProcessor 'org.projectlombok:lombok'
}
35 changes: 17 additions & 18 deletions api/src/main/java/net/bdavies/api/command/ICommandResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,43 @@
package net.bdavies.api.command;


import discord4j.core.spec.EmbedCreateSpec;
import net.bdavies.api.obj.message.discord.embed.EmbedMessage;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;

import java.lang.reflect.Type;
import java.util.function.Consumer;

/**
* @author ben.davies99@outlook.com (Ben Davies)
* @since 1.2.7
*/
public interface ICommandResponse {
public interface ICommandResponse
{

/**
* This will return an embed response
* {@link EmbedCreateSpec}
* Respond with a {@link EmbedMessage}
*
* @param embed this is the CreateSpec {@link EmbedCreateSpec}
* @param embed the embed message to send
* @since __RELEASE_VERSION__
*/
boolean sendEmbed(Consumer<EmbedCreateSpec> embed);
boolean sendEmbed(EmbedMessage embed);

/**
* This will return an embed response
* {@link EmbedCreateSpec}
* Respond with a {@link EmbedMessage} using a {@link Mono}
*
* @param embed this is the CreateSpec {@link EmbedCreateSpec}
* @param embed the embed message to send
* @since __RELEASE_VERSION__
*/
boolean sendEmbed(Mono<Consumer<EmbedCreateSpec>> embed);
boolean sendEmbed(Mono<EmbedMessage> embed);

/**
* This will return an embed response
* {@link EmbedCreateSpec}
* <p>
* Respond with a {@link EmbedMessage} using a {@link Flux}
*
* @param embed this is the CreateSpec {@link EmbedCreateSpec}
* @param embed the embed message to send
* @since __RELEASE_VERSION__
*/
boolean sendEmbed(Flux<Consumer<EmbedCreateSpec>> embed);
boolean sendEmbed(Flux<EmbedMessage> embed);

/**
* This will send one String and then the command has finished
Expand Down Expand Up @@ -102,5 +101,5 @@ public interface ICommandResponse {
*/
boolean send(Type type, Object obj);

FluxProcessor<IResponse, IResponse> getResponses();
Sinks.Many<IResponse> getResponses();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
package net.bdavies.api.discord;

/**
* This is a list of utils attached to a command context and they make it easier to do things inside commands.
* This is a list of utils attached to a command context, and they make it easier to do things inside
* commands.
*
* @author ben.davies99@outlook.com (Ben Davies)
* @since 1.0.0
*/
public interface IDiscordCommandUtil {
public interface IDiscordCommandUtil
{
/**
* Send the author of the message a private message
*
* @param text - the message you would like to send
*/
void sendPrivateMessage(String text);
}
39 changes: 2 additions & 37 deletions api/src/main/java/net/bdavies/api/discord/IDiscordFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,11 @@

package net.bdavies.api.discord;

import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.Event;
import discord4j.core.object.entity.User;
import discord4j.core.object.presence.Activity;
import discord4j.core.object.presence.Presence;
import net.bdavies.api.obj.message.discord.DiscordUser;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;

/**
* This is the Public API for the Discord4JWrapper of the Discord API this will be used for plugins
* <p>
* An example use case being calling {@link IDiscordFacade#getClient()} in a plugin will give you access
* to the
* {@link DiscordClient}
* Use DiscordClient at your own risk it is subject to change, I would recommend just using the api given
* to you
* through the facade.
* </p>
*
* @author ben.davies99@outlook.com (Ben Davies)
* @since 1.0.0
*/
Expand All @@ -61,13 +44,6 @@ public interface IDiscordFacade
*/
Mono<Void> logoutBot();

/**
* This will return the discord client.
*
* @return {@link GatewayDiscordClient}
*/
GatewayDiscordClient getClient();

/**
* This is available to the public through plugins and this will return the bot user.
* To use try doing {@code facade.getOurUser().subscribe(user -> System.out.println(user.getUsername())
Expand All @@ -76,23 +52,12 @@ public interface IDiscordFacade
*
* @return {@link Mono} this is a Mono Stream of a User
*/
Mono<User> getOurUser();
Mono<DiscordUser> getBotUser();

/**
* This will update the presence of the bot to the text
*
* @param text {@link String} the text to change it to
* @see discord4j.core.GatewayDiscordClient#updatePresence(discord4j.discordjson.json.gateway.StatusUpdate)
* @see Presence
* @see Activity
*/
void updateBotPlayingText(String text);

/**
* Register a Event Listener to the Discord Client.
*
* @param callback A callback for the event handler
* @param clazz The the Event Class
*/
<T extends Event> void registerEventHandler(Class<T> clazz, Consumer<T> callback);
}
6 changes: 4 additions & 2 deletions server/src/main/java/net/bdavies/BabblebotApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.bdavies.api.plugins.IPluginContainer;
import net.bdavies.api.variables.IVariableContainer;
import net.bdavies.core.CorePlugin;
import net.bdavies.discord.DiscordFacade;
import net.bdavies.plugins.PluginModel;
import net.bdavies.plugins.PluginModelRepository;
import net.bdavies.plugins.PluginType;
Expand Down Expand Up @@ -212,8 +213,9 @@ public void restart()
{
Executors.newSingleThreadExecutor().submit(() -> {
getPluginContainer().shutDownPlugins();
IDiscordFacade facade = get(IDiscordFacade.class);
facade.registerEventHandler(DisconnectEvent.class, d -> log.info("Bot has been logged out!!!"));
DiscordFacade facade = get(DiscordFacade.class);
facade.getClient().getEventDispatcher().on(DisconnectEvent.class)
.subscribe(d -> log.info("Bot has been logged out!!!"));
facade.logoutBot().block();
try
{
Expand Down
14 changes: 8 additions & 6 deletions server/src/main/java/net/bdavies/command/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import net.bdavies.api.command.ICommandDispatcher;
import net.bdavies.api.command.ICommandMiddleware;
import net.bdavies.api.config.EPluginPermission;
import net.bdavies.api.discord.IDiscordFacade;
import net.bdavies.api.obj.DiscordColor;
import net.bdavies.api.obj.message.discord.embed.EmbedAuthor;
import net.bdavies.api.obj.message.discord.embed.EmbedFooter;
Expand All @@ -49,6 +48,7 @@
import net.bdavies.api.plugins.Plugin;
import net.bdavies.command.errors.UsageException;
import net.bdavies.command.parser.MessageParser;
import net.bdavies.discord.DiscordFacade;
import net.bdavies.discord.obj.factories.EmbedMessageFactory;
import net.bdavies.variables.VariableParser;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -109,7 +109,8 @@ public void addCommand(String namespace, ICommand command, IApplication applicat
} else
{
namespaceCommands.add(command);
IDiscordFacade facade = application.get(IDiscordFacade.class);
//Todo: Move to renderer
DiscordFacade facade = application.get(DiscordFacade.class);
long applicationId = facade.getClient().getRestClient().getApplicationId().block();
facade.getClient().getGuilds().subscribe(g -> {
facade.getClient().getRestClient().getApplicationService()
Expand All @@ -134,7 +135,7 @@ public void addNamespace(String namespace, List<ICommand> commandsToAdd, IApplic
}

//TODO: Make this better
IDiscordFacade facade = application.get(IDiscordFacade.class);
DiscordFacade facade = application.get(DiscordFacade.class);
long applicationId = facade.getClient().getRestClient().getApplicationId().block();
facade.getClient().getGuilds().subscribe(g -> {
commandsToAdd.forEach(c -> facade.getClient().getRestClient().getApplicationService()
Expand Down Expand Up @@ -299,7 +300,7 @@ public void execute(MessageParser parser, String message, Mono<TextChannel> chan

getCommandsFromNamespace(namespace).filter(e -> checkType(e.getType(), commandContext.getType()))
.filter(e -> Arrays.stream(e.getAliases())
.anyMatch(alias -> alias.toLowerCase().equals(commandName.toLowerCase())))
.anyMatch(alias -> alias.equalsIgnoreCase(commandName)))
.doOnError(e -> log.error("Error in the command dispatcher.", e)).doOnComplete(() -> {
if (!hasFoundOne.get())
{
Expand All @@ -313,7 +314,7 @@ public void execute(MessageParser parser, String message, Mono<TextChannel> chan
if (!hasSentMessage.get())
{
channel.subscribe(
c -> c.createMessage("Babblebot command could'nt be found.").subscribe());
c -> c.createMessage("Babblebot command couldn't be found.").subscribe());
} else
{
sb.append("```");
Expand All @@ -336,6 +337,7 @@ public void execute(MessageParser parser, String message, Mono<TextChannel> chan
// g.getName()));
c.exec(application, commandContext);
return commandContext.getCommandResponse().getResponses()
.asFlux()
.log(Loggers.getLogger("CommandResponses"));
}).subscribe(s -> {
if (s.isStringResponse())
Expand Down Expand Up @@ -363,7 +365,7 @@ public void execute(MessageParser parser, String message, Mono<TextChannel> chan
em.setTimestamp(Instant.now());
}
Optional<Color> color = guild.flatMap(
g -> application.get(IDiscordFacade.class).getClient().getSelf()
g -> application.get(DiscordFacade.class).getClient().getSelf()
.flatMap(u -> u.asMember(g.getId()).flatMap(PartialMember::getColor)))
.blockOptional();
if (em.getColor() == null && color.isPresent())
Expand Down
23 changes: 12 additions & 11 deletions server/src/main/java/net/bdavies/command/CommandResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@

import discord4j.core.spec.EmbedCreateSpec;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.EmitterProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;
import reactor.core.publisher.Mono;
import net.bdavies.api.obj.message.discord.embed.EmbedMessage;
import reactor.core.publisher.*;
import net.bdavies.api.command.ICommandResponse;
import net.bdavies.api.command.IResponse;
import net.bdavies.command.response.ResponseHandler;
Expand All @@ -47,25 +45,28 @@
@Slf4j
public class CommandResponse implements ICommandResponse {

private final FluxProcessor<IResponse, IResponse> processor;
private final Sinks.Many<IResponse> processor;

public CommandResponse() {
log.info("Constructor");
processor = EmitterProcessor.create();
processor = Sinks.many().multicast().onBackpressureBuffer();
}

@Override
public boolean sendEmbed(Consumer<EmbedCreateSpec> embed) {
public boolean sendEmbed(EmbedMessage embed)
{
return send(createEmbedType(), embed);
}

@Override
public boolean sendEmbed(Mono<Consumer<EmbedCreateSpec>> embed) {
public boolean sendEmbed(Mono<EmbedMessage> embed)
{
return send(createMonoType(createEmbedType()), embed);
}

@Override
public boolean sendEmbed(Flux<Consumer<EmbedCreateSpec>> embed) {
public boolean sendEmbed(Flux<EmbedMessage> embed)
{
return send(createFluxType(createEmbedType()), embed);
}

Expand Down Expand Up @@ -127,13 +128,13 @@ public boolean send(Type type, Object obj) {
return true;
}

processor.onComplete();
processor.tryEmitComplete();
log.error("Unable to send Object of type: " + type + ", through the command dispatcher.");
return false;
}

@Override
public FluxProcessor<IResponse, IResponse> getResponses() {
public Sinks.Many<IResponse> getResponses() {
return processor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

import net.bdavies.api.command.IResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -41,11 +41,11 @@
public abstract class ResponseHandler
{

private final FluxProcessor<IResponse, IResponse> processor;
private final Sinks.Many<IResponse> processor;
private final Type type;


protected ResponseHandler(Type type, FluxProcessor<IResponse, IResponse> processor)
protected ResponseHandler(Type type, Sinks.Many<IResponse> processor)
{
this.processor = processor;
this.type = type;
Expand Down Expand Up @@ -95,17 +95,17 @@ private boolean isAMono()

private void handleFlux(Flux<IResponse> responses)
{
responses.subscribe(processor::onNext, null, processor::onComplete);
responses.subscribe(processor::tryEmitNext, null, processor::tryEmitComplete);
}

private void handleMono(Mono<IResponse> response)
{
response.subscribe(processor::onNext, null, processor::onComplete);
response.subscribe(processor::tryEmitNext, null, processor::tryEmitComplete);
}

private void handleBase(IResponse response)
{
processor.onNext(response);
processor.onComplete();
processor.tryEmitNext(response);
processor.tryEmitComplete();
}
}
Loading

0 comments on commit df8223e

Please sign in to comment.