Skip to content

Commit

Permalink
Make setTopic() remove the topic if newTopic is falsy (#367)
Browse files Browse the repository at this point in the history
* Make setTopic() remove the topic if newTopic is falsy

* add clearTopic function

* Improve documentation for setTopic
  • Loading branch information
ItsOnlyBinary committed Aug 21, 2024
1 parent 7bab15d commit f51e1d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/clientapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit f51e1d1

Please sign in to comment.