use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by duncte123.
the class CommandManager method runCustomCommand.
private void runCustomCommand(ICommand cmd, String invoke, List<String> args, GuildMessageReceivedEvent event) {
final CustomCommand cusomCommand = (CustomCommand) cmd;
if (cusomCommand.getGuildId() != event.getGuild().getIdLong()) {
return;
}
try {
MDC.put("command.custom.message", cusomCommand.getMessage());
final Parser parser = CommandUtils.getParser(new CommandContext(invoke, args, event, variables));
final String message = parser.parse(cusomCommand.getMessage());
final MessageConfig.Builder messageBuilder = MessageConfig.Builder.fromEvent(event);
final DataObject object = parser.get("embed");
boolean hasContent = false;
if (!message.isEmpty()) {
messageBuilder.setMessage("\u200B" + message);
hasContent = true;
}
if (object != null) {
final JDAImpl jda = (JDAImpl) event.getJDA();
final EmbedBuilder embed = new EmbedBuilder(jda.getEntityBuilder().createMessageEmbed(object));
messageBuilder.addEmbed(true, embed);
hasContent = true;
}
if (hasContent) {
sendMsg(messageBuilder.build());
}
parser.clear();
} catch (Exception e) {
sendMsg(MessageConfig.Builder.fromEvent(event).setMessage("Error with parsing custom command: " + e.getMessage()).build());
Sentry.captureException(e);
}
}
use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by duncte123.
the class MessageListener method invokeAutoResponse.
private boolean invokeAutoResponse(List<CustomCommand> autoResponses, String[] split, GuildMessageReceivedEvent event) {
final String stripped = event.getMessage().getContentStripped().toLowerCase();
final Optional<CustomCommand> match = autoResponses.stream().filter((cmd) -> stripped.contains(cmd.getName().toLowerCase())).findFirst();
if (match.isPresent()) {
final CustomCommand cmd = match.get();
commandManager.dispatchCommand(cmd, "", Arrays.asList(split).subList(1, split.length), event);
return true;
}
return false;
}
use of ml.duncte123.skybot.objects.command.custom.CustomCommand in project SkyBot by DuncteBot.
the class MessageListener method handleMessageEventChecked.
private void handleMessageEventChecked(String raw, Guild guild, GuildMessageReceivedEvent event) {
final GuildSetting settings = GuildSettingsUtils.getGuild(guild.getIdLong(), this.variables);
final String customPrefix = settings.getCustomPrefix();
final Message message = event.getMessage();
if (settings.isMessageLogging()) {
final MessageData data = MessageData.from(message);
this.redis.storeMessage(data, isGuildPatron(guild));
}
if (!commandManager.isCommand(customPrefix, raw) && doAutoModChecks(event, settings, raw)) {
return;
}
final User selfUser = event.getJDA().getSelfUser();
final long selfId = selfUser.getIdLong();
final String selfRegex = "<@!?" + selfId + '>';
if (raw.matches(selfRegex)) {
sendMsg(new MessageConfig.Builder().setChannel(event.getChannel()).setMessageFormat("Hey %s, try `%shelp` for a list of commands. If it doesn't work scream at _duncte123#1245_", event.getAuthor(), customPrefix).build());
return;
}
final String[] split = raw.replaceFirst(Pattern.quote(Settings.PREFIX), "").split("\\s+");
final List<CustomCommand> autoResponses = commandManager.getAutoResponses(guild.getIdLong());
if (!autoResponses.isEmpty() && invokeAutoResponse(autoResponses, split, event)) {
return;
}
if (doesNotStartWithPrefix(selfId, raw, customPrefix) || !canRunCommands(raw, customPrefix, event)) {
return;
}
if (raw.matches(selfRegex + "(.*)")) {
// Handle the chat command
Objects.requireNonNull(commandManager.getCommand("chat")).executeCommand(new CommandContext("chat", Arrays.asList(split).subList(1, split.length), event, variables));
} else {
// Handle the command
commandManager.runCommand(event, customPrefix);
}
}
Aggregations