package com.blamejared.mcbot;

import com.blamejared.mcbot.commands.api.CommandRegistrar;
import com.blamejared.mcbot.listeners.ChannelListener;
import com.blamejared.mcbot.mcp.DataDownloader;

import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageDeleteEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageUpdateEvent;
import sx.blah.discord.handle.obj.*;

public class MCBot {
    
    public static IDiscordClient instance;
    
    public static void main(String[] args) {
        instance = new ClientBuilder().withToken(args[0]).login();

        CommandRegistrar.INSTANCE.slurpCommands();
        
        DataDownloader.INSTANCE.start();
        
        instance.getDispatcher().registerListener(new MCBot());
        instance.getDispatcher().registerListener(new ChannelListener());
    }
    
    public static IChannel getChannel(String name) {
        final IChannel[] channel = new IChannel[1];
        instance.getGuilds().forEach(guild -> guild.getChannels().forEach(chan -> {
            if(chan.getName().equalsIgnoreCase(name)) {
                channel[0] = chan;
            }
        }));
        return channel[0];
    }
    
    public static IChannel getChannel(IGuild guild, String name) {
        final IChannel[] channel = new IChannel[1];
        guild.getChannels().forEach(chan -> {
            if(chan.getName().equalsIgnoreCase(name)) {
                channel[0] = chan;
            }
        });
        return channel[0];
    }
    
    @EventSubscriber
    public void onMessageDeleted(MessageDeleteEvent event) {
        IChannel botLog = getChannel(event.getGuild(), "bot-log");
        if(event.getChannel().getName().equalsIgnoreCase("bot-log")) {
            return;
        }
        if(botLog!=null){
            getChannel(event.getGuild(),"bot-log").sendMessage(event.getAuthor().getName() + " Deleted message : ```" + event.getMessage().getContent().replaceAll("```", "") + "``` from channel: " + event.getChannel().getName());
        }
    }
    
    @EventSubscriber
    public void onMessageEdited(MessageUpdateEvent event) {
    
        IChannel botLog = getChannel(event.getGuild(), "bot-log");
        if(event.getChannel().getName().equalsIgnoreCase("bot-log")) {
            return;
        }
        if(botLog!=null){
            getChannel(event.getGuild(),"bot-log").sendMessage(event.getAuthor().getName() + " Edited message: ```" + event.getOldMessage().getContent().replaceAll("```", "") + "``` -> ```" + event.getNewMessage().getContent().replaceAll("```", "") + "``` from channel: " + event.getChannel().getName());
        }
    }
    
    @EventSubscriber
    public void onMessageRecieved(MessageReceivedEvent event) {
        if(event.getGuild().getName().equals("Modders Corner")) {
            if(event.getMessage().getChannel().getName().equals("general-discussion")) {
                boolean isGif = false;
                if(event.getMessage().getContent().contains(".gif")) {
                    isGif = true;
                }
                if(event.getMessage().getContent().contains("https://giphy.com")) {
                    isGif = true;
                }
                if(event.getMessage().getContent().contains("https://tenor.co")) {
                    isGif = true;
                }
        
                if(isGif) {
                    event.getMessage().delete();
                    event.getChannel().sendMessage("Sorry! GIFs are not allowed in this chat! Head to <#235949539138338816>");
                }
            }
        }
    }
}

@@ -1800,32 +1800,128 @@
 eEvent event) %7B%0A
+        if(instance.getOurUser().getName().equalsIgnoreCase(event.getAuthor().getName())) %7B%0A    
         IChannel
@@ -1971,32 +1971,36 @@
 -log%22);%0A        
+   
+ 
 if(event.getChan
@@ -2054,32 +2054,36 @@
             
+    
 return;%0A
         %7D%0A  
@@ -2062,32 +2062,36 @@
         return;%0A
+    
         %7D%0A      
@@ -2084,32 +2084,36 @@
       %7D%0A        
+    
 if(botLog!=null)
@@ -2097,39 +2097,42 @@
        if(botLog
+ 
 !=
+ 
 null)
+ 
 %7B%0A            ge
@@ -2121,32 +2121,36 @@
 ) %7B%0A            
+ 
+   
 getChannel(event
@@ -2153,32 +2153,33 @@
 vent.getGuild(),
+ 
 %22bot-log%22).sendM
@@ -2344,32 +2344,46 @@
 l().getName());%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -2472,25 +2472,114 @@
 ) %7B%0A    
-%0A
+    if(instance.getOurUser().getName().equalsIgnoreCase(event.getAuthor().getName())) %7B%0A  
         
 IChannel
@@ -2562,32 +2562,34 @@
 ))) %7B%0A          
+  
 IChannel botLog 
@@ -2623,32 +2623,36 @@
 (), %22bot-log%22);%0A
+    
         if(event
@@ -2710,32 +2710,36 @@
 ) %7B%0A            
+  
+  
 return;%0A        
@@ -2738,26 +2738,33 @@
         
-%7D%0A
+    %7D%0A   
         
 if(botLo
@@ -2747,32 +2747,33 @@
    %7D%0A           
+ 
 if(botLog!=null)
@@ -2769,17 +2769,24 @@
 tLog
+ 
 !=
+ 
 null)
+ 
 %7B%0A
+    
     
@@ -2821,16 +2821,17 @@
 Guild(),
+ 
 %22bot-log
@@ -3079,32 +3079,46 @@
 l().getName());%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix messed up messages