Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify listeners in config file #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Kevin-Mok
Copy link

@Kevin-Mok Kevin-Mok commented Aug 3, 2020

Desired Behavior

Each bot instance should have specified listeners for their specific purpose.

Current Behavior

Each bot instance includes all listeners.

Additional Info

I noticed that the current 4 bots all are using the same configuration that includes all the listeners, even though 3 of the bots have very niche purposes (games in their specific channel). For example, we don't need say !fortune for CountdownB0t1. As such, I thought it would be more efficient and make sense to be able to create different configurations for each bot instance so they only use the listeners they need.

Another use case I had in mind for this would be to setup a lightweight bot instance where its only job is to periodically says a command. For example, RepeatB0t1 could say !weather every hour in the #weatherTO channel to prompt WeatherB0t1 to output the weather (bots can't trigger their own commands). WeatherB0t1 would also only need one listener, and I thought it would be overkill to spawn these bots with every single listener.

Implementation

I refactored the part of the code that sets up each bot's configs. It now reads the listenersX property from vilebot.conf, which is a comma-separated list of class name values. An example list would be Karma,Weather or all. It currently only adds the all the admin handlers to each instance.

I also implemented the fix for this unwieldy line as suggested by @amisevsk, as well as using the addListeners method to make each line a much more reasonable length:

new Configuration.Builder().setName( ircNick ).setLogin( ircUser ).setRealName( ircRealName ).addServer( ircServerAddress,
ircPort ).addAutoJoinChannel( ircChannel ).setAutoReconnect( true ).addListener( new Vilebot() ).addListener( new AdminManagement() ).addListener( new AdminPing() ).addListener( new Auth() ).addListener( new DownOrJustMe() ).addListener( new GetLog() ).addListener( new com.oldterns.vilebot.handlers.admin.Help() ).addListener( new NickChange() ).addListener( new com.oldterns.vilebot.handlers.admin.Ops() ).addListener( new Quit() ).addListener( new AnswerQuestion() ).addListener( new Ascii() ).addListener( new ChatLogger() ).addListener( new Church() ).addListener( new Countdown() ).addListener( new Decide() ).addListener( new Excuses() ).addListener( new FakeNews() ).addListener( new Fortune() ).addListener( new GetInfoOn() ).addListener( new Help() ).addListener( new ImageToAscii() ).addListener( new Inspiration() ).addListener( new Jaziz() ).addListener( new Jokes() ).addListener( new Kaomoji() ).addListener( new Karma() ).addListener( new KarmaRoll() ).addListener( new KarmaTransfer() ).addListener( new LastMessageSed() ).addListener( new LastSeen() ).addListener( new Markov() ).addListener( new News() ).addListener( new Omgword() ).addListener( new Ops() ).addListener( new QuotesAndFacts() ).addListener( new RemindMe() ).addListener( new RockPaperScissors() ).addListener( new Summon() ).addListener( new Trivia() ).addListener( new Ttc() ).addListener( new TwitterCorrection() ).addListener( new UrlTitleAnnouncer() ).addListener( new UrlTweetAnnouncer() ).addListener( new Userlists() ).addListener( new UserPing() ).addListener( new Weather() ).buildConfiguration();

Tests

I setup 2 bot instances with the following config:

ircNick1=kmokVileBot1
listeners1=Karma
.
.
.
ircNick2=kmokVileBot2
listeners2=all

And tried the following commands with both of them in the same room:

2020-08-03 19:04:49	@kmok	!rank
2020-08-03 19:04:50	kmokVileBot1	kmok is ranked at #11 with 1581 points of karma.
2020-08-03 19:04:50	kmokVileBot2	kmok is ranked at #11 with 1581 points of karma.
2020-08-03 19:04:51	@kmok	!fortune
2020-08-03 19:04:51	kmokVileBot2	Never regret anything that made you smile
2020-08-03 19:04:54	@kmok	!excuse
2020-08-03 19:04:54	kmokVileBot2	I remember that IBM had a project to do that back in the 70s.

Next Steps

Dynamically generated !help commands for each bot instance.

@Kevin-Mok Kevin-Mok requested a review from cuijulian August 3, 2020 23:47
@Kevin-Mok
Copy link
Author

@Christopher-Chianelli, I can't actually request your review for whatever reason, but could I get your review on this as well?

Copy link
Contributor

@Christopher-Chianelli Christopher-Chianelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using Classes instead of Strings to make missing classes a compile time error + make renaming classes via refactoring easier.

An alternative approach would be to use Quarkus to run VileBot1, and each service would be @ApplicationScoped, then you don't need to specify the services in Vilebot.java.

The third approach is to containerize the services using PodIRCBot (which is what I will do, one day...).

@@ -79,6 +81,12 @@

private static Map<String, String> cfg = getConfigMap();

private static final String[] allListenerClasses = { "AnswerQuestion", "Ascii", "ChatLogger", "Church", "Countdown",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend this being a Class array instead of a String array, so that a missing class is a compile time error and to make it easier to rename classes when refactoring.

@Christopher-Chianelli
Copy link
Contributor

Christopher-Chianelli commented Aug 4, 2020

In regards to "Dynamically generated !help commands for each bot instance.", each service would need to implement an interface. Since help is separated into categories, we need to be able to identify what category to put it help section in.

Proposal:

interface ServiceInfo {
    String getCategory();
    String getHelp();
}

and then build a Map<String,String> using map.computeIfAbsent(category, category -> new ArrayList<>()).add(help);. This would be a one-to-one correspondence to the current way help works. With some additional methods, we could add more services to help. For instance,
!help jeopardy would tell you what jeopardy about. !help church would tell you what church about. !help roll would tell you about the roll command.

Copy link

@cuijulian cuijulian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @Kevin-Mok! Left some minor comments in the PR.

@@ -113,20 +121,73 @@ public static void main( String[] args )
String ircServerAddress = cfg.get( "ircServerAddress" + i );
int ircPort = Integer.parseInt( cfg.get( "ircPort" + i ) );
String ircChannel = cfg.get( "ircChannel" + i );
String listenerCsvString = cfg.get( "listeners" + i );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This additional cfg entry should also be added in vilebot/cfg/vilebot.conf.example (ie. listeners1=all).

}

botManager.start();
// Done
}

/**
* Returns list of Listener object based on a CSV string of Listener class names.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns list of Listener object based on a CSV string of Listener class names.
* Returns list of Listener objects based on a CSV string of Listener class names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants