Merge pull request #9 from BigBotNetwork/greg-dev

Greg dev
This commit is contained in:
Skidder 2019-10-27 15:27:03 +01:00 committed by GitHub
commit 4a6946a73b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 144 additions and 15 deletions

View file

@ -1,6 +1,8 @@
package com.bbn.hadder;
import com.bbn.hadder.commands.TestCommand;
import com.bbn.hadder.commands.fun.GifCommand;
import com.bbn.hadder.commands.general.PingCommand;
import com.bbn.hadder.commands.moderation.BanCommand;
import com.bbn.hadder.commands.moderation.KickCommand;
import com.bbn.hadder.commands.owner.ShutdownCommand;
@ -17,6 +19,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class Hadder {
@ -44,12 +47,8 @@ public class Hadder {
builder.setActivity(Activity.streaming("auf dem BigBotNetwork", "https://twitch.tv/BigBotNetwork"));
builder.setToken(config.getString("Token"));
CommandHandler.cmdlist.put("test", new TestCommand());
CommandHandler.cmdlist.put("ban", new BanCommand());
CommandHandler.cmdlist.put("prefix", new PrefixCommand());
CommandHandler.cmdlist.put("stop", new ShutdownCommand());
CommandHandler.cmdlist.put("shutdown", new ShutdownCommand());
CommandHandler.cmdlist.put("kick", new KickCommand());
CommandHandler.cmdlist.addAll(List.of(new TestCommand(), new BanCommand(), new PrefixCommand(), new ShutdownCommand(), new KickCommand(), new PingCommand(), new GifCommand()));
builder.addEventListeners(
new MentionListener(),

View file

@ -8,4 +8,5 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public interface Command {
void executed(String[] args, MessageReceivedEvent event);
String[] labels();
}

View file

@ -11,4 +11,9 @@ public class TestCommand implements Command {
public void executed(String[] args, MessageReceivedEvent event) {
event.getTextChannel().sendMessage("TEST my friends").queue();
}
@Override
public String[] labels() {
return new String[]{"test"};
}
}

View file

@ -0,0 +1,62 @@
package com.bbn.hadder.commands.fun;
/*
* @author Skidder / GregTCLTK
*/
import com.bbn.hadder.commands.Command;
import com.bbn.hadder.utils.MessageEditor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
public class GifCommand implements Command {
@Override
public void executed(String[] args, MessageReceivedEvent event) {
String url;
JSONArray array;
String query = "";
for(String arg : args) {
query += arg.toLowerCase() + "+";
query = query.substring(0, query.length()-1);
}
File configfile = new File("./config.json");
JSONObject config = null;
try {
config = new JSONObject(new String(Files.readAllBytes(Paths.get(configfile.toURI()))));
} catch (IOException e) {
e.printStackTrace();
}
OkHttpClient caller = new OkHttpClient();
Request request = new Request.Builder().url("http://api.giphy.com/v1/gifs/search?q=" + query + "&api_key=" + config.getString("Giphy")).build();
try {
Random rand = new Random();
Response response = caller.newCall(request).execute();
JSONObject json = new JSONObject(response.body().string());
array = json.getJSONArray("data");
int gifIndex = rand.nextInt(array.length());
url = (String) array.getJSONObject(gifIndex).get("url");
event.getTextChannel().sendMessage(url).queue();
} catch (Exception e) {
EmbedBuilder builder = new EmbedBuilder();
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.ERROR, builder).setTitle("Error").setDescription("Please try again with another term.").build()).queue();
}
}
@Override
public String[] labels() {
return new String[]{"gif"};
}
}

View file

@ -0,0 +1,26 @@
package com.bbn.hadder.commands.general;
/*
* @author Skidder / GregTCLTK
*/
import com.bbn.hadder.commands.Command;
import com.bbn.hadder.utils.MessageEditor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
public class PingCommand implements Command {
@Override
public void executed(String[] args, MessageReceivedEvent event) {
EmbedBuilder builder = new EmbedBuilder();
event.getJDA().getRestPing().queue(ping -> {
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.INFO, builder).setTitle("Ping").setDescription(String.valueOf(ping)).build()).queue();
});
}
@Override
public String[] labels() {
return new String[]{"ping"};
}
}

View file

@ -42,10 +42,14 @@ public class BanCommand implements Command {
EmbedBuilder builder = new EmbedBuilder();
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.WARNING, builder).setDescription("We will be adding multiple banning within a command in the future.").build()).queue();
}
} else {
EmbedBuilder builder = new EmbedBuilder();
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.NO_PERMISSION, builder).build()).queue();
}
}
@Override
public String[] labels() {
return new String[]{"ban"};
}
}

View file

@ -18,7 +18,7 @@ public class KickCommand implements Command {
if (event.getMessage().getMentionedMembers().size() == 1) {
Member victim = event.getMessage().getMentionedMembers().get(0);
if (!event.getAuthor().getId().equals(victim.getId())) {
if (event.getJDA().getSelfUser().getId().equals(victim.getId())) {
if (!event.getJDA().getSelfUser().getId().equals(victim.getId())) {
if (event.getGuild().getSelfMember().canInteract(victim)) {
event.getGuild().kick(victim, "Kicked by " + event.getAuthor().getAsTag()).queue();
EmbedBuilder builder = new EmbedBuilder();
@ -44,4 +44,9 @@ public class KickCommand implements Command {
}
}
}
@Override
public String[] labels() {
return new String[]{"kick"};
}
}

View file

@ -4,11 +4,25 @@ package com.bbn.hadder.commands.owner;
* @author Skidder / GregTCLTK
*/
import com.bbn.hadder.Hadder;
import com.bbn.hadder.Rethink;
import com.bbn.hadder.commands.Command;
import com.bbn.hadder.utils.MessageEditor;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.json.JSONObject;
import javax.annotation.Nonnull;
import javax.security.auth.login.LoginException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ShutdownCommand implements Command {
@Override
@ -16,11 +30,17 @@ public class ShutdownCommand implements Command {
if (event.getAuthor().getId().equals("477141528981012511") || event.getAuthor().getId().equals("261083609148948488")) {
EmbedBuilder builder = new EmbedBuilder();
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.INFO, builder).setTitle("Shutdown").build()).queue();
event.getJDA().shutdown();
Rethink.disconnect();
event.getJDA().getShardManager().shutdown();
System.out.println("Bot shut down via Command...");
System.exit(0);
} else {
EmbedBuilder builder = new EmbedBuilder();
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.NO_PERMISSION, builder).build()).queue();
}
}
@Override
public String[] labels() {
return new String[]{"shutdown"};
}
}

View file

@ -36,4 +36,9 @@ public class PrefixCommand implements Command {
event.getTextChannel().sendMessage(new MessageEditor().setDefaultSettings(MessageEditor.Messagetype.NO_PERMISSION, builder).build()).queue();
}
}
@Override
public String[] labels() {
return new String[]{"prefix"};
}
}

View file

@ -6,16 +6,18 @@ package com.bbn.hadder.core;
import com.bbn.hadder.commands.Command;
import java.util.HashMap;
import java.util.ArrayList;
public class CommandHandler {
public static HashMap<String, Command> cmdlist = new HashMap<>();
public static ArrayList<Command> cmdlist = new ArrayList<>();
public static void handleCommand(CommandParser.commandContainer cmd) {
if(cmdlist.containsKey(cmd.invoke)) {
cmdlist.get(cmd.invoke).executed(cmd.args, cmd.event);
for (Command command : cmdlist) {
for (String label : command.labels()) {
if (label.equals(cmd.invoke)) command.executed(cmd.args, cmd.event);
}
}
}
}