Rework how commands are handled

This commit is contained in:
destro174 2022-04-13 10:16:44 +02:00
parent d86dd9c0bc
commit f2b86e1831
9 changed files with 54 additions and 0 deletions

View File

@ -15,6 +15,7 @@ public abstract class DiscordCommand {
public abstract String getPermission();// TODO discord and LP permissions
public abstract String getDescription();
public abstract String getSyntax();
public abstract long getChannel();
public abstract void handleCommand(Message message, String sender, String command, String[] args);

View File

@ -37,6 +37,11 @@ public class DiscordBroadCast extends DiscordCommand {
return "broadcast";
}
@Override
public long getChannel() {
return 0;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
//TODO also send this to the bot channel, optional command args for color and decoration?

View File

@ -3,6 +3,8 @@ package com.alttd.proxydiscordlink.bot.commands;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.bot.DiscordCommand;
import com.alttd.proxydiscordlink.bot.objects.DiscordRole;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.config.Config;
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
import com.alttd.proxydiscordlink.util.Utilities;
import com.velocitypowered.api.proxy.Player;
@ -39,6 +41,11 @@ public class DiscordLinkCommand extends DiscordCommand {
return "link <code>";
}
@Override
public long getChannel() {
return BotConfig.LINK_CHANNEL;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
Member member = message.getMember();

View File

@ -29,6 +29,11 @@ public class DiscordNick extends DiscordCommand {
return "nick <username/nickname>";
}
@Override
public long getChannel() {
return 0;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
MessageChannel channel = message.getChannel();

View File

@ -50,6 +50,11 @@ public class DiscordServerList extends DiscordCommand {
return "serverlist";
}
@Override
public long getChannel() {
return BotConfig.STAFF_COMMAND_CHANNEL;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
String serverName = "Altitude";

View File

@ -44,6 +44,11 @@ public class DiscordStaffList extends DiscordCommand {
return "staffList";
}
@Override
public long getChannel() {
return BotConfig.STAFF_COMMAND_CHANNEL;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
LuckPerms luckPerms = Utilities.getLuckPerms();

View File

@ -1,6 +1,7 @@
package com.alttd.proxydiscordlink.bot.commands;
import com.alttd.proxydiscordlink.bot.DiscordCommand;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
@ -26,6 +27,11 @@ public class DiscordUnlink extends DiscordCommand {
return "unlink";
}
@Override
public long getChannel() {
return BotConfig.LINK_CHANNEL;
}
@Override
public void handleCommand(Message message, String sender, String command, String[] args) {
Member member = message.getMember();

View File

@ -27,6 +27,7 @@ public class DiscordMessageListener extends ListenerAdapter {
return;
if (event.isWebhookMessage())
return;
/*
if (event.getMessage().getChannel().getIdLong() == BotConfig.COMMAND_CHANNEL) {
String content = event.getMessage().getContentRaw();
if (content.startsWith(BotConfig.prefixMap.get(event.getGuild().getIdLong())) && content.length() > 1) {
@ -53,6 +54,24 @@ public class DiscordMessageListener extends ListenerAdapter {
.findFirst()
.ifPresent(discordCommand -> discordCommand.handleCommand(event.getMessage(), event.getAuthor().getName(), cmd, args));
}
*/
String content = event.getMessage().getContentRaw();
if (!BotConfig.prefixMap.containsKey(event.getGuild().getIdLong())) return; // early return
if (content.startsWith(BotConfig.prefixMap.get(event.getGuild().getIdLong())) && content.length() > 1) {
String[] split = content.split(" ");
String cmd = split[0].substring(1).toLowerCase();
String[] args = Arrays.copyOfRange(split, 1, split.length);
for (DiscordCommand command : DiscordCommand.getCommands()) {
if (!command.getCommand().equalsIgnoreCase(cmd))
continue;
if (!(event.getMessage().getChannel().getIdLong() == command.getChannel()))
continue;
if (command.getPermission() != null) {
// TODO permission check? do we need this?
}
command.handleCommand(event.getMessage(), event.getAuthor().getName(), cmd, args);
}
}
}
}

View File

@ -168,6 +168,7 @@ public class BotConfig {
public static String BOT_TOKEN = "unconfigured";
public static long COMMAND_CHANNEL = -1;
public static long STAFF_COMMAND_CHANNEL = -1;
public static long LINK_CHANNEL = -1;
public static long GUILD_ID = -1;
public static long EVIDENCE_CHANNEL_ID = -1;