Skip to content

Commit

Permalink
feat: Add the @CommandMiddleware support for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
bendavies99 committed Aug 3, 2023
1 parent 0ffdf67 commit 79a4a80
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
Expand Up @@ -306,12 +306,13 @@ public String getNamespaceFromCommandName(String commandName)

public void registerGlobalMiddleware(ICommandMiddleware middleware)
{
log.info("Registered global middleware: {}", middleware);
this.middlewareList.get(null).add(middleware);
}


public void registerPluginMiddleware(IPluginSettings plugin, ICommandMiddleware middleware)
{
log.info("Registered plugin middleware: {} -- {}", plugin, middleware);
middlewareList.putIfAbsent(plugin, new ArrayList<>());
middlewareList.get(plugin).add(middleware);
}
Expand Down
Expand Up @@ -42,7 +42,6 @@
@Slf4j
public class DropdownViewHandler extends ResponseHandler
{

public DropdownViewHandler(Type type, Sinks.Many<IResponse> processor)
{
super(type, processor);
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/net/babblebot/core/CorePlugin.java
Expand Up @@ -60,7 +60,7 @@ public boolean checkIfChannelIgnored(ICommandContext ctx)
{
if (ctx instanceof DiscordCommandContext discordCommandContext)
{
log.debug("Checking if channel {} is ignored by babblebot", discordCommandContext
log.info("Checking if channel {} is ignored by babblebot", discordCommandContext
.getMessage().getChannel().getName());
return ignoreRepository
.findByChannel((discordCommandContext).getMessage().getChannel())
Expand Down
Expand Up @@ -30,7 +30,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.babblebot.api.IApplication;
import net.babblebot.api.command.ICommandRegistry;
import net.babblebot.api.plugins.*;
import net.babblebot.api.service.IVersionService;
import net.babblebot.command.CommandRegistry;
Expand All @@ -55,7 +54,7 @@ public class PluginContainer implements IPluginContainer
private final Map<Object, PluginPermissionContainer> pluginPermissionsMap = new HashMap<>();
private final IVersionService versionService;
private final IApplication application;
private final ICommandRegistry commandRegistry;
private final CommandRegistry commandRegistry;

@Override
public void addPlugin(Object obj, IPluginModel model)
Expand Down
Expand Up @@ -26,11 +26,13 @@
package net.babblebot.plugins;

import lombok.extern.slf4j.Slf4j;
import net.babblebot.discord.DiscordCommandContext;
import net.babblebot.BabblebotApplication;
import net.babblebot.api.IApplication;
import net.babblebot.api.command.*;
import net.babblebot.api.config.IConfig;
import net.babblebot.api.obj.message.discord.DiscordMessage;
import net.babblebot.discord.DiscordCommandContext;
import org.springframework.context.ApplicationContext;
import org.springframework.data.util.Pair;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -82,6 +84,38 @@ public List<Pair<Boolean, ICommandMiddleware>> parseMiddleware()
}
}

BabblebotApplication bbApp = (BabblebotApplication) application;
ApplicationContext context = bbApp.getApplicationContext();
context.getBeansWithAnnotation(CommandMiddleware.class).values().forEach(middleware -> {
Class<?> mc = middleware.getClass();
CommandMiddleware cm = mc.getAnnotation(CommandMiddleware.class);
if (!cm.global())
{
if (cm.plugin() != Object.class)
{
if (cm.plugin() == pluginObj.getClass())
{
if (Arrays.stream(mc.getInterfaces()).anyMatch(i -> i == ICommandMiddleware.class))
{
ICommandMiddleware commandMiddleware = (ICommandMiddleware) middleware;
commandsMiddleware.add(Pair.of(false, commandMiddleware));
} else
{
log.warn("Cannot register command middleware class: {} because there's no " +
"implementation of ICommandMiddleware", mc);
}
}
} else
{
log.error("Cannot register command middleware class: {} because the setup is incorrect " +
"for a plugin middleware ensure you set global to false and the plugin " +
"parameter",
mc);
}

}
});

return commandsMiddleware;
}

Expand Down
Expand Up @@ -25,6 +25,7 @@

package net.babblebot.plugins;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import net.babblebot.api.plugins.IPluginSettings;

Expand All @@ -33,6 +34,7 @@
* @since 1.2.7
*/
@Slf4j
@ToString
public class PluginSettings implements IPluginSettings
{

Expand Down
Expand Up @@ -30,10 +30,13 @@
import lombok.val;
import net.babblebot.BabblebotApplication;
import net.babblebot.api.IApplication;
import net.babblebot.api.command.CommandMiddleware;
import net.babblebot.api.command.ICommandMiddleware;
import net.babblebot.api.plugins.IPlugin;
import net.babblebot.api.plugins.IPluginModel;
import net.babblebot.api.plugins.Plugin;
import net.babblebot.api.plugins.PluginConfig;
import net.babblebot.command.CommandRegistry;
import net.babblebot.plugins.PluginConfigParser;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.support.GenericApplicationContext;
Expand Down Expand Up @@ -126,6 +129,8 @@ public Flux<Object> importPlugin(IPluginModel config)
applicationContext::removeBeanDefinition);
}
}


if (c.isAnnotationPresent(PluginConfig.class))
{
application.get(PluginConfigParser.class)
Expand All @@ -135,6 +140,28 @@ public Flux<Object> importPlugin(IPluginModel config)
{
applicationContext.registerBean(c);
}

if (c.isAnnotationPresent(CommandMiddleware.class))
{
CommandMiddleware middleware = c.getAnnotation(
CommandMiddleware.class);
if (middleware.global())
{
if (Arrays.stream(c.getInterfaces())
.anyMatch(i -> i == ICommandMiddleware.class))
{
application.get(CommandRegistry.class)
.registerGlobalMiddleware(
(ICommandMiddleware) application.get(c));
} else
{
log.warn(
"Cannot register command middleware class: {} " +
"because there's no " +
"implementation of ICommandMiddleware", c);
}
}
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -281,7 +308,8 @@ private boolean isRegistrableBean(Class<?> c)
Entity.class,
Controller.class,
RestController.class,
Entity.class
Entity.class,
CommandMiddleware.class
);

return Arrays.stream(c.getAnnotations())
Expand Down

0 comments on commit 79a4a80

Please sign in to comment.