Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cotton;
- import com.cotton.modules.*;
- import com.cotton.references.Reference;
- import net.dv8tion.jda.client.entities.Group;
- import net.dv8tion.jda.core.entities.*;
- import net.dv8tion.jda.core.events.ReadyEvent;
- import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent;
- import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
- import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent;
- import net.dv8tion.jda.core.events.message.react.MessageReactionRemoveEvent;
- import net.dv8tion.jda.core.hooks.ListenerAdapter;
- import org.jetbrains.annotations.NotNull;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.atomic.AtomicReference;
- public class Controller extends ListenerAdapter {
- private boolean debug;
- private Reference ref;
- private Help help;
- private Ping ping;
- private Music music;
- private E6 e6;
- private Info info;
- private final HashMap<ArrayList<String>, Object> availableCommand = new HashMap<>() {{
- put(new ArrayList<>(){{add("help");add("true");}}, help);
- put(new ArrayList<>(){{add("ping");add("true");}}, ping);
- put(new ArrayList<>(){{add("e6");add("true");}}, e6);
- put(new ArrayList<>(){{add("info");add("true");}}, info);
- put(new ArrayList<>(){{add("music");add("false");}}, music);
- }};
- Controller(boolean setDebug) {
- this.debug = setDebug;
- ref = new Reference();
- help = new Help();
- ping = new Ping();
- music = new Music();
- e6 = new E6();
- info = new Info();
- }
- private void debugPrint(Object obj){
- if(debug)
- if(obj instanceof String) {
- System.out.println(obj);
- }else{
- System.out.println(String.valueOf(obj));
- }
- }
- public void onReady(ReadyEvent event){
- debugPrint("Bot is now online!");
- debugPrint(true);
- event.getJDA().setAutoReconnect(true);
- event.getJDA().getPresence().setGame(Game.of(Game.GameType.WATCHING, "owo.help for help."));
- }
- public void onGuildMemberJoin(GuildMemberJoinEvent event){
- System.out.println("User joined!");
- TextChannel channel = event.getGuild().getTextChannelsByName("welcome",true).get(0);
- System.out.println("Channel name: " + channel.getName() + ", channel type: " + channel.getType() + " user name: " + event.getMember().getNickname() + ", user as mention: " + event.getMember().getAsMention());
- channel.sendMessage("Welcome " + event.getMember().getAsMention() + "! Please be kind, and follow the rules.").queue();
- }
- public void onMessageReactionAdd(MessageReactionAddEvent event) {
- }
- public void onMessageReactionRemove(MessageReactionRemoveEvent event) {
- }
- @SuppressWarnings("ResultOfMethodCallIgnored")
- public void onMessageReceived(MessageReceivedEvent event){
- logMessage(event);
- String msg = event.getMessage().getContentDisplay();
- if(!event.getAuthor().isBot() &&
- !event.getAuthor().isFake() &&
- event.isFromType(ChannelType.TEXT)){
- if(msg.length() >= ref.prefix.length()){
- if (msg.substring(0, msg.indexOf('.')).contains(ref.prefix)) {
- boolean isCommand = msg.substring(0, msg.indexOf('.')).contains(ref.prefix);
- Optional<String> optStr = Optional.of("foo");
- AtomicReference<String> result = new AtomicReference<>("");
- if (isCommand) {
- String command;
- if (msg.contains(" ")) {
- command = msg.substring(msg.indexOf('.') + 1, msg.indexOf(' '));
- optStr.ifPresent(s -> result.set(command));
- } else {
- command = msg.substring(msg.indexOf('.') + 1);
- optStr.ifPresent(s -> result.set(command));
- }
- }
- availableCommand.forEach((strings, o) -> {
- Method[] declaredMethod = new Method[0];
- String command = result.get();
- for (String s : strings){
- if(s.toLowerCase().contains(command.toLowerCase()))
- declaredMethod = s.getClass().getDeclaredMethods();
- for(Method method : declaredMethod){
- if (method.getName().equals("run")){
- if(strings.get(1).equals("true")){
- Thread thread = new Thread(() -> invoke(method, method.getClass(), event));
- thread.setDaemon(true);
- thread.start();
- }else{
- invoke(method, method.getClass(), event);
- }
- }
- }
- }
- });
- }
- }
- }
- }
- private void logMessage(MessageReceivedEvent event){
- Message message = event.getMessage();
- String msg = message.getContentDisplay();
- User author = event.getAuthor();
- Guild guild = event.getGuild();
- TextChannel textChannel = event.getTextChannel();
- Member member = event.getMember();
- String name;
- if(debug) {
- if (event.isFromType(ChannelType.TEXT)) {
- if (message.isWebhookMessage()) {
- name = author.getName();
- } else {
- name = member.getEffectiveName();
- }
- DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd || HH:mm:ss");
- Date date = new Date();
- if (event.getMessage().getAttachments().size() > 0) {
- for (Message.Attachment attch : event.getMessage().getAttachments()) {
- System.out.printf("(%s) |%s| [%s]<%s>: %s\n", guild.getName(), dateFormat.format(date), textChannel.getName(), name, attch.getProxyUrl());
- }
- } else {
- System.out.printf("(%s) |%s| [%s]<%s>: %s\n", guild.getName(), dateFormat.format(date), textChannel.getName(), name, msg);
- }
- } else if (event.isFromType(ChannelType.PRIVATE)) {
- System.out.printf("[PRIV]<%s>: %s\n", author.getName(), msg);
- } else if (event.isFromType(ChannelType.GROUP)) {
- Group group = event.getGroup();
- String groupName = group.getName() != null ? group.getName() : "";
- System.out.printf("[GRP: %s]<%s>: %s\n", groupName, author.getName(), msg);
- }
- }
- }
- private static void invoke(@NotNull Method declaredMethod, @NotNull Class<?> reflectiveClass, Object event){
- try {
- Object methodAsObject;
- methodAsObject = reflectiveClass.getDeclaredConstructor().newInstance();
- declaredMethod.invoke(methodAsObject, event);
- } catch (IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement