|
1 | 1 | package io.github.jdiscordbots.command_framework;
|
2 | 2 |
|
3 |
| -import io.github.jdiscordbots.command_framework.command.CommandEvent; |
4 | 3 | import io.github.jdiscordbots.command_framework.command.ICommand;
|
5 | 4 | import io.github.jdiscordbots.command_framework.utils.PermissionUtils;
|
6 | 5 | import net.dv8tion.jda.api.EmbedBuilder;
|
7 |
| -import net.dv8tion.jda.api.Permission; |
8 |
| -import net.dv8tion.jda.api.entities.Member; |
9 | 6 | import net.dv8tion.jda.api.entities.TextChannel;
|
10 |
| -import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; |
11 | 7 | import org.slf4j.Logger;
|
12 | 8 | import org.slf4j.LoggerFactory;
|
13 | 9 |
|
14 | 10 | import java.awt.*;
|
15 |
| -import java.util.List; |
16 |
| -import java.util.*; |
17 |
| -import java.util.regex.Pattern; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
18 | 14 |
|
19 | 15 | /**
|
20 | 16 | * CommandHandler of Command system
|
21 | 17 | */
|
22 | 18 | final class CommandHandler {
|
23 |
| - private static final Map<String, ICommand> commands = new HashMap<>(); |
24 | 19 | private static final Logger LOG = LoggerFactory.getLogger(CommandHandler.class);
|
| 20 | + private static final Map<String, ICommand> commands = new HashMap<>(); |
25 | 21 |
|
26 | 22 | /**
|
27 | 23 | * Private constructor for utility class
|
@@ -52,9 +48,9 @@ static void addCommand(String name, ICommand command) {
|
52 | 48 | /**
|
53 | 49 | * handle commands
|
54 | 50 | *
|
55 |
| - * @param commandContainer {@link io.github.jdiscordbots.command_framework.CommandHandler.CommandParser CommandContainer} |
| 51 | + * @param commandContainer {@link io.github.jdiscordbots.command_framework.CommandParser.CommandContainer CommandContainer} |
56 | 52 | */
|
57 |
| - public static void handle(final CommandContainer commandContainer) { |
| 53 | + public static void handle(final CommandParser.CommandContainer commandContainer) { |
58 | 54 | final TextChannel channel = commandContainer.event.getChannel();
|
59 | 55 |
|
60 | 56 | if (commands.containsKey(commandContainer.invoke.toLowerCase())) {
|
@@ -85,88 +81,4 @@ public static void handle(final CommandContainer commandContainer) {
|
85 | 81 | }
|
86 | 82 | }
|
87 | 83 | }
|
88 |
| - |
89 |
| - /** |
90 |
| - * CommandParser |
91 |
| - */ |
92 |
| - static final class CommandParser { |
93 |
| - static final Pattern SPACE_PATTERN = Pattern.compile("\\s+"); |
94 |
| - |
95 |
| - /** |
96 |
| - * Private constructor for utility class |
97 |
| - */ |
98 |
| - private CommandParser() { |
99 |
| - /* Prevent instantiation */ |
100 |
| - } |
101 |
| - |
102 |
| - /** |
103 |
| - * Parse GuildMessageReceivedEvent and Prefix to CommandContainer |
104 |
| - * |
105 |
| - * @param event incomming {@link net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent GuildMessageReceivedEvent} |
106 |
| - * @param prefix prefix |
107 |
| - * @return {@link io.github.jdiscordbots.command_framework.CommandHandler.CommandContainer CommandContainer} |
108 |
| - */ |
109 |
| - static CommandContainer parse(final GuildMessageReceivedEvent event, final String prefix) { |
110 |
| - final Member member = event.getMember(); |
111 |
| - |
112 |
| - if (member == null) |
113 |
| - throw new IllegalStateException("Member is null"); |
114 |
| - |
115 |
| - String raw = event.getMessage().getContentRaw(); |
116 |
| - |
117 |
| - if (!member.hasPermission(event.getChannel(), Permission.MESSAGE_MENTION_EVERYONE)) |
118 |
| - raw = raw.replace("@everyone", "@\u200Beveryone").replace("@here", "@\u200Bhere"); |
119 |
| - |
120 |
| - final String beheaded = raw.replaceFirst(Pattern.quote(prefix), ""); |
121 |
| - |
122 |
| - final String[] splitBeheaded = SPACE_PATTERN.split(beheaded.trim()); |
123 |
| - final String invoke = splitBeheaded[0]; |
124 |
| - final List<String> split = new ArrayList<>(); |
125 |
| - boolean inQuote = false; |
126 |
| - |
127 |
| - for (int i = 1; i < splitBeheaded.length; i++) { |
128 |
| - String s = splitBeheaded[i]; |
129 |
| - |
130 |
| - if (inQuote) { |
131 |
| - if (s.endsWith("\"")) { |
132 |
| - inQuote = false; |
133 |
| - s = s.substring(0, s.length() - 1); |
134 |
| - } |
135 |
| - |
136 |
| - split.add(split.remove(split.size() - 1).concat(" ").concat(s)); |
137 |
| - } else { |
138 |
| - if (s.startsWith("\"") && !s.endsWith("\"")) { |
139 |
| - inQuote = true; |
140 |
| - s = s.substring(1); |
141 |
| - } |
142 |
| - |
143 |
| - split.add(s); |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| - final CommandEvent commandEvent = new CommandEvent(event, split); |
148 |
| - return new CommandContainer(invoke, commandEvent); |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - /** |
153 |
| - * CommandContainer |
154 |
| - */ |
155 |
| - public static final class CommandContainer { |
156 |
| - public final String invoke; |
157 |
| - public final List<String> args; |
158 |
| - public final CommandEvent event; |
159 |
| - |
160 |
| - /** |
161 |
| - * Construct a new Container by given command invoke and -event |
162 |
| - * |
163 |
| - * @param invoke name/invoke of command |
164 |
| - * @param event {@link io.github.jdiscordbots.command_framework.command.CommandEvent CommandEvent} |
165 |
| - */ |
166 |
| - public CommandContainer(String invoke, CommandEvent event) { |
167 |
| - this.invoke = invoke; |
168 |
| - this.args = event.getArgs(); |
169 |
| - this.event = event; |
170 |
| - } |
171 |
| - } |
172 | 84 | }
|
0 commit comments