diff --git a/server/plugins/irc-events/error.ts b/server/plugins/irc-events/error.ts index b5ae24e6..7140bf65 100644 --- a/server/plugins/irc-events/error.ts +++ b/server/plugins/irc-events/error.ts @@ -40,11 +40,9 @@ export default function (irc, network) { if (irc.connection.registered === false && !Config.values.public) { message += " An attempt to use it will be made when this nick quits."; - // Clients usually get nick in use on connect when reconnecting to a network - // after a network failure (like ping timeout), and as a result of that, - // TL will append a random number to the nick. - // keepNick will try to set the original nick name back if it sees a QUIT for that nick. - network.keepNick = irc.user.nick; + // Store the user's preferred nick in keepNick so the quit handler can reclaim it + // This is the user's actual preference from network.nick, not a fallback + network.keepNick = network.nick; } const lobby = network.getLobby(); @@ -57,20 +55,16 @@ export default function (irc, network) { if (irc.connection.registered === false) { const nickLen = parseInt(network.irc.network.options.NICKLEN, 10) || 16; - const random = (data.nick || irc.user.nick) + Math.floor(Math.random() * 10); // Safeguard nick changes up to allowed length // Some servers may send "nick in use" error even for randomly generated nicks if (random.length <= nickLen) { + // Only change the IRC session nick (irc.user.nick), not the user's preference (network.nick) + // This allows the quit handler to reclaim the preferred nick when it becomes available irc.changeNick(random); } } - - client.emit("nick", { - network: network.uuid, - nick: irc.user.nick, - }); }); irc.on("nick invalid", function (data) { diff --git a/server/plugins/irc-events/welcome.ts b/server/plugins/irc-events/welcome.ts index 039fac08..7c9ea2ac 100644 --- a/server/plugins/irc-events/welcome.ts +++ b/server/plugins/irc-events/welcome.ts @@ -6,7 +6,19 @@ export default function (irc, network) { const client = this; irc.on("registered", function (data) { - network.setNick(data.nick); + // Only update the user's preferred nick (network.nick) if we registered with it + // If we registered with a fallback nick (e.g., nick123), don't overwrite the preference + // This allows the existing quit handler to reclaim the preferred nick when available + if (data.nick === network.nick) { + // We got our preferred nick, clear keepNick if it was set + if (network.keepNick === data.nick) { + network.keepNick = null; + } + } else if (data.nick !== network.nick) { + // We registered with a fallback, don't call setNick which would overwrite the preference + // Just update the IRC session nick + irc.user.nick = data.nick; + } const lobby = network.getLobby(); const msg = new Msg({