package com.oldterns.vilebot.handlers.user;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oldterns.vilebot.Vilebot;
import com.oldterns.vilebot.util.BaseNick;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.types.GenericMessageEvent;

public class Summon
    extends ListenerAdapter
{
    private static final Pattern nounPattern = Pattern.compile( "\\S+" );

    private static final Pattern summonPattern = Pattern.compile( "^!summon (" + nounPattern + ")\\s*$" );

    private static ExecutorService timer = Executors.newScheduledThreadPool( 1 );

    @Override
    public void onGenericMessage( final GenericMessageEvent event )
    {
        String text = event.getMessage();

        Matcher summonMatcher = summonPattern.matcher( text );

        if ( summonMatcher.matches() )
            summonNick( event, summonMatcher.group( 1 ) );
    }

    private void summonNick( GenericMessageEvent event, String nick )
    {
        event.getBot().sendIRC().invite( nick, Vilebot.getConfig().get( "ircChannel1" ) );
        event.getBot().sendIRC().message( nick,
                                          "You are summoned by " + BaseNick.toBaseNick( event.getUser().getNick() )
                                              + " to join " + Vilebot.getConfig().get( "ircChannel1" ) );
        event.respond( nick + " was invited." );

    }

}

@@ -42,92 +42,8 @@
 r;%0A%0A
-import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A
 impo
@@ -194,42 +194,136 @@
 ort 
-org.pircbotx.hooks.ListenerAdapter
+com.oldterns.vilebot.util.LimitCommand;%0Aimport org.pircbotx.hooks.ListenerAdapter;%0Aimport org.pircbotx.hooks.events.MessageEvent
 ;%0Aim
@@ -612,30 +612,29 @@
 ;%0A%0A    p
-rivate
+ublic
  static 
 Executor
@@ -629,160 +629,393 @@
 tic 
-ExecutorService timer = Executors.newScheduledThreadPool( 1 );%0A%0A    @Override%0A    public void onGenericMessage( final GenericMessageEvent event )%0A    %7B%0A
+LimitCommand limitCommand = new LimitCommand();%0A%0A    private static final String RESTRICTED_CHANNEL = Vilebot.getConfig().get( %22ircChannel1%22 );%0A%0A    @Override%0A    public void onGenericMessage( final GenericMessageEvent event )%0A    %7B%0A%0A        if ( event instanceof MessageEvent%0A            && ( (MessageEvent) event ).getChannel().getName().equals( RESTRICTED_CHANNEL ) )%0A        %7B%0A    
     
@@ -1061,16 +1061,20 @@
         
+    
 Matcher 
@@ -1125,24 +1125,28 @@
 );%0A%0A        
+    
 if ( summonM
@@ -1180,54 +1180,691 @@
     
-summonNick( event, summonMatcher.group( 1 ) );
+%7B%0A                String toSummon = summonMatcher.group( 1 ).trim();%0A                if ( ( (MessageEvent) event ).getChannel().getUsers().stream().filter( u -%3E u.getNick().equals( toSummon ) ).findAny().isPresent() )%0A                %7B%0A                    event.respondWith( toSummon + %22 is already in the channel.%22 );%0A                %7D%0A                else%0A                %7B%0A                    String isLimit = limitCommand.addUse( event.getUser().getNick() );%0A                    if ( isLimit.isEmpty() )%0A                        summonNick( event, toSummon );%0A                    else%0A                        event.respondWith( isLimit );%0A                %7D%0A            %7D%0A        %7D
 %0A   
@@ -1994,48 +1994,26 @@
 ck, 
-Vilebot.getConfig().get( %22ircChannel1%22 )
+RESTRICTED_CHANNEL
  );%0A
@@ -2063,50 +2063,8 @@
 ick,
-%0A                                         
  %22Yo
@@ -2082,16 +2082,28 @@
 ned by %22
+%0A           
  + BaseN
@@ -2149,111 +2149,43 @@
 () )
-%0A                                              + %22 to join %22 + Vilebot.getConfig().get( %22ircChannel1%22 )
+ + %22 to join %22 + RESTRICTED_CHANNEL
  );%0A

Add limit command and ensure user not in channel