Add nick timer to try and switch back to primary nick.

This commit is contained in:
cnaude 2018-06-09 10:07:46 -07:00
parent fb2e12fd55
commit 8466a84b1d
3 changed files with 81 additions and 3 deletions

View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import org.bukkit.scheduler.BukkitTask;
/**
*
* @author Chris Naude Poll the command queue and dispatch to Bukkit
*/
public class NickWatcher {
private final PurpleIRC plugin;
private final PurpleBot ircBot;
private BukkitTask bt = null;
/**
*
* @param ircBot
* @param plugin
*/
public NickWatcher(final PurpleBot ircBot, final PurpleIRC plugin) {
this.ircBot = ircBot;
this.plugin = plugin;
startWatcher();
}
private void startWatcher() {
plugin.logDebug("Starting nick watcher");
bt = this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, new Runnable() {
@Override
public void run() {
checkNick();
}
}, ircBot.nickTimer, ircBot.nickTimer);
}
private void checkNick() {
if (ircBot.bot == null) {
return;
}
if (!ircBot.bot.isConnected()) {
return;
}
if (!ircBot.bot.getNick().equals(ircBot.nick)) {
plugin.logDebug("My nick is wrong. Attempting to change...");
// we don't want to continusouly cycle thorugh our nicks
ircBot.nickIndex = 0;
ircBot.bot.sendIRC().changeNick(ircBot.nick);
}
}
public void cancel() {
this.bt.cancel();
}
}

View File

@ -119,7 +119,8 @@ public final class PurpleBot {
public String nick;
public String botNick;
public List<String> altNicks;
private int nickIndex = 0;
public int nickTimer;
public int nickIndex = 0;
public String botLogin;
public String botRealName;
public int ircMaxLineLength;
@ -186,6 +187,7 @@ public final class PurpleBot {
private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue;
public FloodChecker floodChecker;
public NickWatcher nickWatcher;
protected boolean floodControlEnabled;
protected int floodControlMaxMessages;
protected long floodControlTimeInterval;
@ -316,6 +318,9 @@ public final class PurpleBot {
messageQueue = new IRCMessageQueueWatcher(this, plugin);
floodChecker = new FloodChecker(this, plugin);
if (nickTimer > 0) {
nickWatcher = new NickWatcher(this, plugin);
}
}
@ -792,6 +797,7 @@ public final class PurpleBot {
nick = config.getString("nick", "");
botNick = nick;
altNicks = config.getStringList("alt-nicks");
nickTimer = config.getInt("alt-nick-timer", 1200);
plugin.loadTemplates(config, botNick, "message-format");
botLogin = config.getString("login", "PircBot");
botRealName = config.getString("realname", "");
@ -3357,7 +3363,7 @@ public final class PurpleBot {
if (isMessageEnabled(channel, TemplateName.IRC_HERO_PART)) {
String hChannel = heroChannelMap.get(channel.getName());
String template = plugin.getMessageTemplate(botNick, channelName, TemplateName.IRC_HERO_PART);
String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template,hChannel);
String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, hChannel);
plugin.herochatHook.sendHeroMessage(hChannel, message);
}
@ -3435,7 +3441,7 @@ public final class PurpleBot {
if (isMessageEnabled(channel, TemplateName.IRC_HERO_TOPIC)) {
String hChannel = heroChannelMap.get(channel.getName());
String template = plugin.getMessageTemplate(botNick, channelName, TemplateName.IRC_HERO_TOPIC);
String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, topic,hChannel);
String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, topic, hChannel);
plugin.herochatHook.sendHeroMessage(hChannel, message);
}

View File

@ -9,6 +9,8 @@ nick: AwesomeBot
alt-nicks:
- '%NICK%_'
- '%NICK%__'
# tick interval for attempting to switch back to primary nick. set to 0 to disable.
alt-nick-timer: 1200
# login - Your bot's login name
login: AwesomeName
# realname