Skip to content

Commit

Permalink
Made some adjustments to commandhandler
Browse files Browse the repository at this point in the history
Finished expansioncommand
Added paginations
Added help command
added expansion list command
Added set prefix command
Added ecloud status command
  • Loading branch information
PiggyPiglet committed Nov 11, 2018
1 parent 9403986 commit a85b366
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.gradle/
.idea/
src/test
build/
build/
out/
62 changes: 58 additions & 4 deletions src/main/java/at/helpch/papibot/commands/ExpansionCommand.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package at.helpch.papibot.commands;

import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.handlers.misc.PaginationHandler;
import at.helpch.papibot.core.objects.PapiExpansion;
import at.helpch.papibot.core.objects.paginations.PaginationBuilder;
import at.helpch.papibot.core.objects.paginations.PaginationPage;
import at.helpch.papibot.core.utils.string.StringUtils;
import com.google.gson.Gson;
import com.google.inject.Inject;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
public final class ExpansionCommand extends Command {
@Inject private Gson gson;
@Inject private PaginationHandler paginationHandler;

public ExpansionCommand() {
super("*");
Expand All @@ -20,11 +34,51 @@ public ExpansionCommand() {
@Override
protected void execute(GuildMessageReceivedEvent e, String[] args) {
if (args.length >= 1) {
if (!args[0].equalsIgnoreCase("list")) {
e.getChannel().sendMessage(new PapiExpansion(gson).load(args[0]).getPlaceholders()).queue();
if (!StringUtils.equalsIgnoreCase(args[0], new String[]{"list", "help", "prefix", "status"})) {
PapiExpansion exp = new PapiExpansion(gson).load(args[0]);

if (exp.isSuccess()) {
String title = "Placeholders for " + args[0];
String[] footer = new String[]{"Version " + exp.getVersion() + " by " + exp.getAuthor(), "https://cdn.discordapp.com/avatars/510750938672136193/e6120b4adf0c99226064d78094348592.webp"};
MessageEmbed.Field command = new MessageEmbed.Field("Command:", "```/papi ecloud download " + args[0] + "\n/papi reload```", false);
List<String> placeholders = exp.getPlaceholders();

if (placeholders.size() == 1) {
MessageEmbed message = new EmbedBuilder()
.setTitle(title)
.addField("Placeholders: ", exp.getPlaceholders().get(0) + "\n\u200C", false)
.addField(command)
.setFooter(footer[0], footer[1])
.setTimestamp(ZonedDateTime.now())
.build();

e.getChannel().sendMessage(message).queue(m -> m.delete().queueAfter(5, TimeUnit.MINUTES));
} else {
List<String> unicodes = new ArrayList<>();

Stream.of(
"1\u20E3", "2\u20E3", "3\u20E3", "4\u20E3", "5\u20E3", "6\u20E3", "7\u20E3", "8\u20E3", "9\u20E3"
).forEach(unicodes::add);

AtomicInteger i = new AtomicInteger(0);
PaginationBuilder paginationBuilder = new PaginationBuilder();

placeholders.forEach(somePlaceholders -> {
EmbedBuilder embed = new EmbedBuilder()
.setTitle(title)
.setFooter(footer[0], footer[1])
.setTimestamp(ZonedDateTime.now());

PaginationPage page = new PaginationPage(embed.addField(new MessageEmbed.Field("Placeholders:", somePlaceholders + "\n\u200C", false)).addField(command).build(), unicodes.get(i.getAndIncrement()));
paginationBuilder.addPages(page);
});

paginationBuilder.build(e.getChannel(), paginationHandler);
}
} else {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " There was an error when trying to process your request, either the eCloud is down or the expansion you requested does not exist.").queue(s -> s.delete().queueAfter(15, TimeUnit.SECONDS));
}
}
} else {
e.getChannel().sendMessage("Please supply an expansion name.").queue();
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/at/helpch/papibot/commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package at.helpch.papibot.commands;

import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.utils.mysql.ServerUtils;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
public final class HelpCommand extends Command {
public HelpCommand() {
super("help");
}

@Override
protected void execute(GuildMessageReceivedEvent e, String[] args) {
String prefix = "**" + ServerUtils.getPrefix(e.getGuild().getIdLong()) + " ";

e.getChannel().sendMessage(new EmbedBuilder()
.setTitle("PlaceholderAPI Bot Help Menu", "https://github.com/help-chat/PlaceholderAPI-Discord-Bot")
.setThumbnail(e.getJDA().getSelfUser().getEffectiveAvatarUrl())
.setDescription(
prefix + "help** - This menu.\n" +
prefix + "list** - Expansion list.\n" +
prefix + "<expansion name>** - Get placeholders for a specific expansion.\n" +
prefix + "status** - Get the current status of the eCloud.\n" +
prefix + "prefix <prefix>** - Admin command to change the bot's prefix for this specific guild.")
.addField("Support: ", "Having trouble with PlaceholderAPI or any other plugin? Don't hesitate to ask for help in our help chat, https://helpch.at/discord", false)
.setTimestamp(ZonedDateTime.now())
.build()).queue(s -> s.delete().queueAfter(1, TimeUnit.MINUTES));
}
}
84 changes: 84 additions & 0 deletions src/main/java/at/helpch/papibot/commands/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package at.helpch.papibot.commands;

import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.handlers.misc.PaginationHandler;
import at.helpch.papibot.core.objects.paginations.PaginationBuilder;
import at.helpch.papibot.core.objects.paginations.PaginationPage;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.inject.Inject;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
public final class ListCommand extends Command {
@Inject private Gson gson;
@Inject private PaginationHandler paginationHandler;

public ListCommand() {
super("list");
}

@Override
@SuppressWarnings("unchecked")
protected void execute(GuildMessageReceivedEvent e, String[] args) {
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet("https://api.extendedclip.com/");
List<String> keys = new ArrayList<>();

try {
HttpResponse response = client.execute(get);
keys.addAll(gson.fromJson(EntityUtils.toString(response.getEntity()), LinkedTreeMap.class).keySet());
} catch (Exception ex) {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + "The eCloud is currently non responsive, please report this to staff in https://helpch.at/discord").queue();
}

keys = Arrays.asList(String.join("\n", keys).replaceAll("((.*\\s*\\n\\s*){25})", "$1-SEPARATOR-\n").split("-SEPARATOR-"));
String[] title = new String[] {"Expansions in the eCloud", "https://api.extendedclip.com/all"};

if (keys.size() == 1) {
e.getChannel().sendMessage(new EmbedBuilder()
.setTitle(title[0], title[1])
.addField("Expansions: ", keys.get(0), false)
.setTimestamp(ZonedDateTime.now())
.build()).queue(s -> s.delete().queueAfter(5, TimeUnit.MINUTES));
} else {
List<String> unicodes = new ArrayList<>();

Stream.of(
"1\u20E3", "2\u20E3", "3\u20E3", "4\u20E3", "5\u20E3", "6\u20E3", "7\u20E3", "8\u20E3", "9\u20E3"
).forEach(unicodes::add);

AtomicInteger i = new AtomicInteger(0);
PaginationBuilder paginationBuilder = new PaginationBuilder();

keys.forEach(someKeys -> {
EmbedBuilder embed = new EmbedBuilder()
.setTitle(title[0], title[1])
.setTimestamp(ZonedDateTime.now());

PaginationPage page = new PaginationPage(embed.addField(new MessageEmbed.Field("Placeholders:", someKeys, false)).build(), unicodes.get(i.getAndIncrement()));
paginationBuilder.addPages(page);
});

paginationBuilder.build(e.getChannel(), paginationHandler);
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/at/helpch/papibot/commands/PrefixCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package at.helpch.papibot.commands;

import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.utils.mysql.ServerUtils;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import java.util.concurrent.TimeUnit;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
public final class PrefixCommand extends Command {
public PrefixCommand() {
super("prefix");
}

@Override
protected void execute(GuildMessageReceivedEvent e, String[] args) {
if (e.getMember() == e.getGuild().getOwner() || e.getAuthor().getIdLong() == 181675431362035712L) {
if (args.length >= 1) {
ServerUtils.setPrefix(e.getGuild().getIdLong(), args[0]);
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " :white_check_mark: **Prefix successfully set to `" + args[0] + "`.**").queue(s -> s.delete().queueAfter(15, TimeUnit.SECONDS));
} else {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " :warning: **Please supply the prefix you want to set.**").queue(s -> s.delete().queueAfter(30, TimeUnit.SECONDS));
}
} else {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " :warning: **Only the server owner can use this command.**").queue(s -> s.delete().queueAfter(30, TimeUnit.SECONDS));
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/at/helpch/papibot/commands/StatusCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package at.helpch.papibot.commands;

import at.helpch.papibot.core.framework.Command;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
public final class StatusCommand extends Command {
public StatusCommand() {
super("status");
}

@Override
protected void execute(GuildMessageReceivedEvent e, String[] args) {
try {
HttpURLConnection con = (HttpURLConnection) new URL("https://api.extendedclip.com/").openConnection();

if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + ":warning: **The eCloud is currently non responsive, please report this to staff in https://helpch.at/discord**").queue(s -> s.delete().queueAfter(45, TimeUnit.SECONDS));
} else {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " :white_check_mark: **The eCloud is up and responsive.**").queue(s -> s.delete().queueAfter(15, TimeUnit.SECONDS));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package at.helpch.papibot.core.handlers.chat;

import at.helpch.papibot.commands.HelpCommand;
import at.helpch.papibot.core.framework.Command;
import at.helpch.papibot.core.handlers.GEvent;
import at.helpch.papibot.core.objects.enums.EventsEnum;
import at.helpch.papibot.core.utils.mysql.ServerUtils;
import at.helpch.papibot.core.utils.string.StringUtils;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import lombok.Getter;
import net.dv8tion.jda.core.events.Event;
Expand All @@ -13,14 +15,16 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.concurrent.TimeUnit;

// ------------------------------
// Copyright (c) PiggyPiglet 2018
// https://www.piggypiglet.me
// ------------------------------
@Singleton
public final class CommandHandler extends GEvent {
@Inject private HelpCommand helpCommand;

@Getter private final List<Command> commands = new ArrayList<>();

public CommandHandler() {
Expand All @@ -31,16 +35,32 @@ public CommandHandler() {
protected void execute(Event event) {
GuildMessageReceivedEvent e = (GuildMessageReceivedEvent) event;
String msg = e.getMessage().getContentRaw();
String prefix = ServerUtils.getPrefix(e.getGuild().getIdLong()) + " ";
String prefix = ServerUtils.getPrefix(e.getGuild().getIdLong());
boolean success = false;

if (StringUtils.startsWith(msg, new String[]{prefix})) {
if (msg.equalsIgnoreCase(prefix)) {
helpCommand.run(e, null);
e.getMessage().delete().queue();
return;
}

for (Command cmd : commands) {
String[] command = Arrays.stream(cmd.getCommand()).map(i -> prefix + i).toArray(String[]::new);
for (Command cmd : commands) {
String[] command = Arrays.stream(cmd.getCommand()).map(i -> prefix + " " + i).toArray(String[]::new);

if (StringUtils.startsWith(msg, command) || (StringUtils.startsWith(msg, new String[]{prefix}) && command[0].contains("*"))) {
String[] args = msg.toLowerCase().replace(prefix, "").trim().split("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
args = command[0].contains("*") ? args : args.length >= 1 ? Arrays.copyOfRange(args, 1, args.length) : args;
cmd.run(e, args);
if (StringUtils.startsWith(msg, command) || command[0].contains("*")) {
String[] args = msg.toLowerCase().replace(prefix, "").trim().split("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
args = command[0].contains("*") ? args : args.length >= 1 ? Arrays.copyOfRange(args, 1, args.length) : args;
cmd.run(e, args);
success = true;
}
}

if (!success) {
e.getChannel().sendMessage(e.getAuthor().getAsMention() + " Unknown command, type `?papi help` for help.").queue(s -> s.delete().queueAfter(15, TimeUnit.SECONDS));
}

e.getMessage().delete().queue();
}
}
}
Loading

0 comments on commit a85b366

Please sign in to comment.