Fix commands not working in some cases

#98
Also unregistering DC commands
Also removed the command string from the unknown command message
This commit is contained in:
Norbi Peti 2020-10-30 23:50:08 +01:00
parent 64994ee44e
commit 491b5e4ee9
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 31 additions and 28 deletions

View file

@ -151,11 +151,11 @@
<version>3.1.1</version> <version>3.1.1</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 --> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<!-- <dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId> <artifactId>slf4j-jdk14</artifactId>
<version>1.7.21</version> <version>1.7.21</version>
</dependency> --> </dependency>
<dependency> <dependency>
<groupId>com.github.TBMCPlugins.ChromaCore</groupId> <groupId>com.github.TBMCPlugins.ChromaCore</groupId>
<artifactId>Chroma-Core</artifactId> <artifactId>Chroma-Core</artifactId>

View file

@ -5,9 +5,9 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.Command2DCSender; import buttondevteam.discordplugin.commands.Command2DCSender;
import buttondevteam.discordplugin.util.Timings; import buttondevteam.discordplugin.util.Timings;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import discord4j.common.util.Snowflake;
import discord4j.core.object.entity.Message; import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.Role; import discord4j.core.object.entity.Role;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.core.object.entity.channel.PrivateChannel; import discord4j.core.object.entity.channel.PrivateChannel;
import lombok.val; import lombok.val;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -22,7 +22,7 @@ public class CommandListener {
* @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message * @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message
* @return Whether it <b>did not run</b> the command * @return Whether it <b>did not run</b> the command
*/ */
public static Mono<Boolean> runCommand(Message message, MessageChannel commandChannel, boolean mentionedonly) { public static Mono<Boolean> runCommand(Message message, Snowflake commandChannelID, boolean mentionedonly) {
Timings timings = CommonListeners.timings; Timings timings = CommonListeners.timings;
Mono<Boolean> ret = Mono.just(true); Mono<Boolean> ret = Mono.just(true);
if (message.getContent().length() == 0) if (message.getContent().length() == 0)
@ -35,7 +35,7 @@ public class CommandListener {
timings.printElapsed("B"); timings.printElapsed("B");
if (!(channel instanceof PrivateChannel) if (!(channel instanceof PrivateChannel)
&& !(content.charAt(0) == DiscordPlugin.getPrefix() && !(content.charAt(0) == DiscordPlugin.getPrefix()
&& channel.getId().asLong() == commandChannel.getId().asLong())) // && channel.getId().asLong() == commandChannelID.asLong())) //
return ret; return ret;
timings.printElapsed("C"); timings.printElapsed("C");
tmp = ret.then(channel.type()).thenReturn(true); // Fun (this true is ignored - x) tmp = ret.then(channel.type()).thenReturn(true); // Fun (this true is ignored - x)
@ -62,7 +62,7 @@ public class CommandListener {
try { try {
timings.printElapsed("F"); timings.printElapsed("F");
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString)) if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
return DPUtils.reply(message, channel, "unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString) return DPUtils.reply(message, channel, "unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.")
.map(m -> false); .map(m -> false);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e, DiscordPlugin.plugin); TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e, DiscordPlugin.plugin);

View file

@ -43,8 +43,7 @@ public class CommonListeners {
if (FunModule.executeMemes(event.getMessage())) if (FunModule.executeMemes(event.getMessage()))
return def; return def;
val commandChannel = DiscordPlugin.plugin.commandChannel.get(); val commandChannel = DiscordPlugin.plugin.commandChannel.get();
val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.commandChannel); return event.getMessage().getChannel().map(mch ->
return commandCh.filterWhen(ch -> event.getMessage().getChannel().map(mch ->
(commandChannel != null && mch.getId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat (commandChannel != null && mch.getId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
|| mch instanceof PrivateChannel || mch instanceof PrivateChannel
|| event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels || event.getMessage().getContent().contains("channelcon")) //Only 'channelcon' is allowed in other channels
@ -52,17 +51,17 @@ public class CommonListeners {
if (!shouldRun) if (!shouldRun)
return Mono.just(true); //The condition is only for the first command execution, not mcchat return Mono.just(true); //The condition is only for the first command execution, not mcchat
timings.printElapsed("Run command 1"); timings.printElapsed("Run command 1");
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here return CommandListener.runCommand(event.getMessage(), commandChannel, true); //#bot is handled here
})).filterWhen(ch -> { }).filterWhen(ch -> {
timings.printElapsed("mcchat"); timings.printElapsed("mcchat");
val mcchat = Component.getComponents().get(MinecraftChatModule.class); val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
return Mono.empty(); //Wasn't handled, continue return Mono.just(true); //Wasn't handled, continue
}).filterWhen(ch -> { }).filterWhen(ch -> {
timings.printElapsed("Run command 2"); timings.printElapsed("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false); return CommandListener.runCommand(event.getMessage(), commandChannel, false);
}); });
}).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err, DiscordPlugin.plugin)) }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err, DiscordPlugin.plugin))
.subscribe(); .subscribe();
dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> { dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> {

View file

@ -228,9 +228,6 @@ public class MCChatListener implements Listener {
// Discord // Discord
public Mono<Boolean> handleDiscord(MessageCreateEvent ev) { public Mono<Boolean> handleDiscord(MessageCreateEvent ev) {
val ret = Mono.just(true);
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
return ret;
Timings timings = CommonListeners.timings; Timings timings = CommonListeners.timings;
timings.printElapsed("Chat event"); timings.printElapsed("Chat event");
val author = ev.getMessage().getAuthor(); val author = ev.getMessage().getAuthor();
@ -249,7 +246,7 @@ public class MCChatListener implements Listener {
&& ev.getMessage().getContent().replace(prefix + "", "") && ev.getMessage().getContent().replace(prefix + "", "")
.equalsIgnoreCase("mcchat")); //Either mcchat or /mcchat .equalsIgnoreCase("mcchat")); //Either mcchat or /mcchat
//Allow disabling the chat if needed //Allow disabling the chat if needed
}).filterWhen(channel -> CommandListener.runCommand(ev.getMessage(), channel, true)) }).filterWhen(channel -> CommandListener.runCommand(ev.getMessage(), DiscordPlugin.plugin.commandChannel.get(), true))
//Allow running commands in chat channels //Allow running commands in chat channels
.filter(channel -> { .filter(channel -> {
MCChatUtils.resetLastMessage(channel); MCChatUtils.resetLastMessage(channel);
@ -373,7 +370,7 @@ public class MCChatListener implements Listener {
return true; return true;
} }
module.log(dsender.getName() + " ran from DC: /" + cmd); module.log(dsender.getName() + " ran from DC: /" + cmd);
if (runCustomCommand(dsender, cmdlowercased)) return true; if (dsender instanceof DiscordSender && runCustomCommand(dsender, cmdlowercased)) return true;
val channel = clmd == null ? user.channel.get() : clmd.mcchannel; val channel = clmd == null ? user.channel.get() : clmd.mcchannel;
val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp); val ev = new TBMCCommandPreprocessEvent(dsender, channel, dmessage, clmd == null ? dsender : clmd.dcp);
Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync Bukkit.getScheduler().runTask(DiscordPlugin.plugin, //Commands need to be run sync

View file

@ -114,6 +114,9 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
*/ */
private final ConfigData<Boolean> serverUp = getConfig().getData("serverUp", false); private final ConfigData<Boolean> serverUp = getConfig().getData("serverUp", false);
private final MCChatCommand mcChatCommand = new MCChatCommand(this);
private final ChannelconCommand channelconCommand = new ChannelconCommand(this);
@Override @Override
protected void enable() { protected void enable() {
if (DPUtils.disableIfConfigErrorRes(this, chatChannel, chatChannelMono())) if (DPUtils.disableIfConfigErrorRes(this, chatChannel, chatChannelMono()))
@ -121,8 +124,8 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
listener = new MCChatListener(this); listener = new MCChatListener(this);
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin()); TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled
getPlugin().getManager().registerCommand(new MCChatCommand(this)); getPlugin().getManager().registerCommand(mcChatCommand);
getPlugin().getManager().registerCommand(new ChannelconCommand(this)); getPlugin().getManager().registerCommand(channelconCommand);
val chcons = getConfig().getConfig().getConfigurationSection("chcons"); val chcons = getConfig().getConfig().getConfigurationSection("chcons");
if (chcons == null) //Fallback to old place if (chcons == null) //Fallback to old place
@ -234,6 +237,8 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
chconc.set("brtoggles", chcon.brtoggles.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.toList())); chconc.set("brtoggles", chcon.brtoggles.stream().map(TBMCSystemChatEvent.BroadcastTarget::getName).collect(Collectors.toList()));
} }
listener.stop(true); listener.stop(true);
getPlugin().getManager().unregisterCommand(mcChatCommand);
getPlugin().getManager().unregisterCommand(channelconCommand);
disabling = false; disabling = false;
} }

View file

@ -30,15 +30,17 @@ import java.util.stream.Collectors;
public class GameRoleModule extends Component<DiscordPlugin> { public class GameRoleModule extends Component<DiscordPlugin> {
public List<String> GameRoles; public List<String> GameRoles;
private final RoleCommand command = new RoleCommand(this);
@Override @Override
protected void enable() { protected void enable() {
getPlugin().getManager().registerCommand(new RoleCommand(this)); getPlugin().getManager().registerCommand(command);
GameRoles = DiscordPlugin.mainServer.getRoles().filterWhen(this::isGameRole).map(Role::getName).collect(Collectors.toList()).block(); GameRoles = DiscordPlugin.mainServer.getRoles().filterWhen(this::isGameRole).map(Role::getName).collect(Collectors.toList()).block();
} }
@Override @Override
protected void disable() { protected void disable() {
getPlugin().getManager().unregisterCommand(command);
} }
/** /**