From f51e1d1d66926bd145165bacce09f780a186a974 Mon Sep 17 00:00:00 2001 From: ItsOnlyBinary Date: Wed, 21 Aug 2024 15:18:10 +0100 Subject: [PATCH] Make setTopic() remove the topic if newTopic is falsy (#367) * Make setTopic() remove the topic if newTopic is falsy * add clearTopic function * Improve documentation for setTopic --- docs/clientapi.md | 5 ++++- src/client.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/clientapi.md b/docs/clientapi.md index 914e01f..9a1eec9 100644 --- a/docs/clientapi.md +++ b/docs/clientapi.md @@ -109,7 +109,10 @@ Join a channel, optionally with a key/password. Part/leave a channel with an optional parting message. ##### `.setTopic(channel, newTopic)` -Set the topic of a channel +Set the topic of a channel, if newTopic is falsy or only whitespace then `.clearTopic()` will be called. + +##### `.clearTopic(channel)` +Remove the topic of a channel. ##### `.ctcpRequest(target, type [, paramN])` Send a CTCP request to target with any number of parameters. diff --git a/src/client.js b/src/client.js index 33ed8b7..41dc30d 100644 --- a/src/client.js +++ b/src/client.js @@ -611,9 +611,23 @@ module.exports = class IrcClient extends EventEmitter { } setTopic(channel, newTopic) { + if (!newTopic || !newTopic.trim()) { + // If newTopic is undefined or empty, remove the existing topic + // this check is to prevent unexpectedly requesting the current topic + // when trying to clear the topic + this.clearTopic(channel); + return; + } + this.raw('TOPIC', channel, newTopic); } + clearTopic(channel) { + // The trailing `:` is required otherwise it would be requesting the topic + // and not clearing it + this.raw(`TOPIC ${channel} :`); + } + ctcpRequest(target, type /*, paramN */) { const params = Array.prototype.slice.call(arguments, 1);