Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
LoboMetalurgico committed Dec 6, 2020
2 parents 0186278 + bd569d9 commit 68005f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@twitchapis/twitch.js",
"version": "1.0.0-beta.4",
"version": "1.0.0-beta.5",
"description": "twitch.js is a powerful Node.js module that allows you to easily interact with the TwitchTV making easy the way to make a TwitchTV bot, for a custom chat overlay for you OBS, or a moderation bot for you chat, or you just want a easy interface to TwitchTV.",
"main": "./src/index",
"scripts": {
Expand Down
26 changes: 20 additions & 6 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class Client extends EventEmmiter {

/**
* Logs the client in, establishing a websocket connection to Twitch.
* @param {String} userName Username of the account to log in with
* @param {String} token Token of the account to log in with
* @param {String} [userName] Username of the account to log in with
* @param {String} [token] Token of the account to log in with
* @returns {Promise<Pending>}
* @example
* Client.login('userName', 'token')
Expand All @@ -113,7 +113,7 @@ class Client extends EventEmmiter {

/**
* Join the bot on the channel parsed
* @param {String} channelName The name of the channel the bot will connect
* @param {String} [channelName] The name of the channel the bot will connect
* @returns {Promise<Boolean>} true if the bot connect, false if it cannot connect
* @example
* client.join('channelName')
Expand All @@ -125,7 +125,7 @@ class Client extends EventEmmiter {

/**
* Leave the bot on the channel parsed
* @param {String} channelName The name of the channel the bot will disconnect
* @param {String} [channelName] The name of the channel the bot will disconnect
* @returns {Promise<Boolean>} true if the bot disconnect, false if it cannot disconnect
* @example
* client.join('channelName')
Expand All @@ -145,6 +145,19 @@ class Client extends EventEmmiter {
return this.sleept.methods.ping();
}

/**
* Send message into any connected channel
* @param {String} [channelName] The name of the channel the bot will send the message
* @param {String} [message] The message that will be sended
* @param {Array<optional>} [replacer] If the message contains %s, the array that will replace the %s in order
* @returns {Promise<Pending>}
* @example
* client.sendMessage('#channel', 'message', ['replacer', 'replacer2'])
*/
sendMessage(channelName, message, ...replacer) {
return this.sleept.methods.sendMessage(channelName, message, ...replacer);
}

/**
* Emit a event from client level
* @param {String} event the name of the event than will be sended
Expand Down Expand Up @@ -182,10 +195,11 @@ class Client extends EventEmmiter {
/**
* send a message on the same channel who send it
* @param {String} [message] the message than will be sended on the channel
* @param {Array<optional>} [replacer] If the message contains %s, the array that will replace the %s in order
* @return {Promise<Pending>} The message sended metadata
*/
send: (...message) => {
return this.sleept.methods.sendMessage(args[0].params[0], message);
send: (message, ...replacer) => {
return this.sleept.methods.sendMessage(args[0].params[0], message, replacer);
}
},
author: {
Expand Down
21 changes: 17 additions & 4 deletions src/sleept/SLEEPTMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,26 @@ class SLEEPTMethods {
});
}

sendMessage(channel, ...message) {
sendMessage(channel, message, ...replacer) {
return new Promise((resolve, reject) => {
if (!message || message === null || (typeof message === 'object' && message[0] === null)) {
if (typeof channel !== 'string') {
logger.warn('The channel must be a String');
return reject('The channel must be a String');
} else if (typeof message !== 'string') {
logger.warn('The message must be a String');
return reject('The message must be a String');
} else if (!message || message === null) {
logger.warn('Cannot send empty messages');
reject('Cannot send empty messages');
return reject('Cannot send empty messages');
}
if (replacer && replacer[0]) {
replacer.forEach((element) => {
message = message.replace('%s', element);
});
}
if (!channel.includes('#')) {
channel = '#' + channel;
}
message = message.join(' ');
resolve(this.ws.send(`PRIVMSG ${channel} :${message}`));
});
}
Expand Down

0 comments on commit 68005f4

Please sign in to comment.