mirror of https://github.com/bvn13/JIrcBot.git
implemented admin commands from channel and refactored bots autoreconnections
parent
58ed9cb297
commit
0b521d693c
|
@ -3,7 +3,10 @@
|
|||
<component name="FacetManager">
|
||||
<facet type="Spring" name="Spring">
|
||||
<configuration>
|
||||
<fileset id="fileset" name="Spring Application Context" removed="false" />
|
||||
<fileset id="fileset2" name="Spring Application Context" removed="false">
|
||||
<file>file://$MODULE_DIR$/src/main/java/ru/bvn13/jircbot/MainApp.java</file>
|
||||
<file>file://$MODULE_DIR$/src/main/java/ru/bvn13/jircbot/config/JircBotConfiguration.java</file>
|
||||
</fileset>
|
||||
</configuration>
|
||||
</facet>
|
||||
<facet type="web" name="Web">
|
||||
|
|
|
@ -5,7 +5,7 @@ After=network.target
|
|||
[Service]
|
||||
User=bvn13
|
||||
WorkingDirectory=/srv/jircbot
|
||||
ExecStart=/usr/bin/java -jar /srv/jircbot/jircbot-2.0.1.jar
|
||||
ExecStart=/usr/bin/java -jar /srv/jircbot/jircbot-2.0.3.jar
|
||||
SuccessExitStatus=143
|
||||
|
||||
[Install]
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -18,7 +18,7 @@
|
|||
|
||||
|
||||
<properties>
|
||||
<bot.version>2.0.2</bot.version>
|
||||
<bot.version>2.0.3</bot.version>
|
||||
|
||||
<java.version>1.8</java.version>
|
||||
|
||||
|
|
|
@ -111,10 +111,10 @@ public class JircBot extends ListenerAdapter {
|
|||
this.executorService.scheduleAtFixedRate(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
//logger.warn("check");
|
||||
logger.debug("check");
|
||||
checkBots();
|
||||
}
|
||||
}, 30, 5, TimeUnit.SECONDS);
|
||||
}, 15, 5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
|
@ -190,17 +190,35 @@ public class JircBot extends ListenerAdapter {
|
|||
|
||||
private void checkBots() {
|
||||
|
||||
this.manager.getBots().forEach(bot -> {
|
||||
if (bot.getState().equals(PircBotX.State.DISCONNECTED)) {
|
||||
try {
|
||||
bot.startBot();
|
||||
} catch (IOException e) {
|
||||
logger.error("Could not start bot at "+bot.getServerHostname(), e);
|
||||
} catch (IrcException e) {
|
||||
logger.error("IrcException while starting bot at "+bot.getServerHostname(), e);
|
||||
if (this.manager.getBots().size() < this.bots.size()) {
|
||||
logger.warn("CHECKING BOTS");
|
||||
logger.debug("BOTS COUNT: " + this.manager.getBots().size());
|
||||
}
|
||||
|
||||
this.bots.forEach((id, bot) -> {
|
||||
if (!this.manager.getBots().contains(bot)) {
|
||||
logger.warn("RECONNECTION BOT "+bot.getServerHostname());
|
||||
bot.stopBotReconnect();
|
||||
this.manager.addBot(bot);
|
||||
} else {
|
||||
if (bot.getState().equals(PircBotX.State.DISCONNECTED)) {
|
||||
logger.warn("RECONNECTION BOT "+bot.getServerHostname());
|
||||
bot.stopBotReconnect();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void startBot(PircBotX bot) {
|
||||
logger.warn("BOT "+bot.getServerHostname()+" STATUS "+bot.getState().toString());
|
||||
try {
|
||||
bot.startBot();
|
||||
} catch (IOException e) {
|
||||
logger.error("Could not start bot at "+bot.getServerHostname(), e);
|
||||
} catch (IrcException e) {
|
||||
logger.error("IrcException while starting bot at "+bot.getServerHostname(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String extractServer(String s) {
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ru.bvn13.jircbot.listeners;
|
||||
|
||||
import org.pircbotx.hooks.events.JoinEvent;
|
||||
import org.pircbotx.hooks.events.MessageEvent;
|
||||
import org.pircbotx.hooks.events.PrivateMessageEvent;
|
||||
import org.pircbotx.hooks.types.GenericEvent;
|
||||
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -45,25 +48,72 @@ public class AdminListener extends ImprovedListenerAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onPrivateMessage(PrivateMessageEvent event) throws Exception {
|
||||
|
||||
AtomicReference<Config> aConfig = new AtomicReference<>(null);
|
||||
|
||||
configuration.getConnections().forEach(c -> {
|
||||
|
||||
if (sameServer(event.getBot().getServerHostname(), c.getServer())) {
|
||||
aConfig.set(c);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
if (aConfig.get() == null) {
|
||||
event.respondPrivateMessage("sorry, bot not found!");
|
||||
public void onMessage(MessageEvent event) throws Exception {
|
||||
Config config = getBotConfig(event);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Config config = aConfig.get();
|
||||
if (event.getUser().isVerified()
|
||||
&& !config.getMasterNick().isEmpty()
|
||||
&& config.getMasterNick().equals(event.getUser().getNick())) {
|
||||
|
||||
if (event.getMessage().startsWith(COMMAND)) {
|
||||
String command = event.getMessage().substring(COMMAND.length());
|
||||
String commands[] = command.trim().split(" ", 2);
|
||||
|
||||
if (commands[0].startsWith("+") || commands[0].startsWith("-")) {
|
||||
boolean isApply = commands[0].startsWith("+");
|
||||
command = commands[0].substring(1);
|
||||
|
||||
switch (command.toLowerCase()) {
|
||||
case "op" :
|
||||
if (commands.length == 1) {
|
||||
event.getBot().sendRaw().rawLine("PRIVMSG chanserv :" + (!isApply ? "deop" : "op") + " " + event.getChannel().getName() + " " + event.getUser().getNick());
|
||||
} else {
|
||||
event.getBot().sendRaw().rawLine("PRIVMSG chanserv :" + (!isApply ? "deop" : "op") + " " + event.getChannel().getName() + " " + commands[1]);
|
||||
}
|
||||
break;
|
||||
case "v" :
|
||||
case "voice":
|
||||
if (commands.length == 1) {
|
||||
event.getBot().sendRaw().rawLine("MODE " + event.getChannel().getName() + " " + (isApply ? "+" : "-") + "v " + event.getUser().getNick());
|
||||
} else {
|
||||
event.getBot().sendRaw().rawLine("MODE " + event.getChannel().getName() + " " + (isApply ? "+" : "-") + "v " + commands[1]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (command.toLowerCase()) {
|
||||
case "inv" :
|
||||
case "invite" :
|
||||
if (command.length() > 1) {
|
||||
event.getBot().sendRaw().rawLine("INVITE " + event.getChannel().getName() + " " + commands[1]);
|
||||
}
|
||||
break;
|
||||
case "kick" :
|
||||
if (command.length() > 1) {
|
||||
event.getBot().sendRaw().rawLine("KICK " + event.getChannel().getName() + " " + commands[1] + (commands.length > 2 ? " "+commands[2] : ""));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrivateMessage(PrivateMessageEvent event) throws Exception {
|
||||
|
||||
Config config = getBotConfig(event);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getUser().isVerified()
|
||||
&& !config.getMasterNick().isEmpty()
|
||||
|
@ -151,7 +201,25 @@ public class AdminListener extends ImprovedListenerAdapter {
|
|||
return false;
|
||||
}
|
||||
|
||||
private Config getBotConfig(GenericMessageEvent event) {
|
||||
AtomicReference<Config> aConfig = new AtomicReference<>(null);
|
||||
|
||||
configuration.getConnections().forEach(c -> {
|
||||
|
||||
if (sameServer(event.getBot().getServerHostname(), c.getServer())) {
|
||||
aConfig.set(c);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
if (aConfig.get() == null) {
|
||||
event.respondPrivateMessage("sorry, bot not found!");
|
||||
return null;
|
||||
}
|
||||
|
||||
Config config = aConfig.get();
|
||||
return config;
|
||||
}
|
||||
|
||||
private void changeSettings(String serverHost, String channelName, String set, String modeStr) {
|
||||
if (set.equals("hello-message") || set.equals("hello-msg")) {
|
||||
|
|
|
@ -6,7 +6,10 @@ config=config.json
|
|||
|
||||
spring.profiles.active=production
|
||||
|
||||
spring.banner.location=banner.txt
|
||||
|
||||
server.port=8002
|
||||
server.address=127.0.0.1
|
||||
|
||||
# for PostgreSQL
|
||||
spring.datasource.driverClassName=org.postgresql.Driver
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
__ _ ___ _
|
||||
\ \(_)_ __ ___ / __\ ___ | |_
|
||||
\ \ | '__/ __|/__\/// _ \| __|
|
||||
/\_/ / | | | (__/ \/ \ (_) | |_
|
||||
\___/|_|_| \___\_____/\___/ \__|
|
|
@ -13,7 +13,7 @@
|
|||
<jmxConfigurator />
|
||||
|
||||
|
||||
<logger name="ru.bvn13.jircbot" level="ALL"/>
|
||||
<logger name="ru.bvn13.jircbot" level="WARN"/>
|
||||
<logger name="ru.bvn13.jircbot.listeners" level="ALL"/>
|
||||
<logger name="ru.bvn13.jircbot.listeners.calculator" level="ALL"/>
|
||||
<logger name="org.pircbotx" level="WARN"/>
|
||||
|
|
Loading…
Reference in New Issue