Skip to content

Instantly share code, notes, and snippets.

@AlcaDesign
Last active January 14, 2019 10:31
Embed
What would you like to do?
//...
client.on('message', (channel, user, message, fromSelf) => {
// Ignore bot's messages.
if(fromSelf) {
return false;
}
// The name of the user. (Will default to the lower case if the display name
// is missing from the user object.)
let name = user['display-name'] || user.username,
// A pre-formatted string to go at the beginning of command outputs.
greeting = `@${name},`
// The text in the message split by spaces for easy use.
params = message
// Remove extra whitespace on ends.
.trim()
// Replace multiple spaces with 1.
.replace(/\s{2,}/g, ' ')
// Split the string at spaces.
.split(' '),
// Remove and return the first element.
command = params.shift(),
// Are there reamining parameters?
hasParams = params.length > 0,
// Default to the user if there's no first parameter.
userParam = hasParams ? params[0] : user.username;
// Is it a command?
if(command[0] === '!') {
// Remove the exclamation mark
command = command.replace(/^!+/, '');
}
// Wasn't a command, ignore this normal message.
else {
return false;
}
// Check that the command was !logs
if(command === 'logs') {
// Make the reply to `channel` using the greeting, the link, and first
// parameter or the username of the user if there's no parameter
client.say(channel,
`${greeting} http://logsplaceholder.com/userlogs/${userParam}`
// Comes out like this:
// @<name>, http://logsplaceholder.com/userlogs/<param[0] || name>
);
}
});
'use strict';
//...
client.on('message', function (channel, user, message, fromSelf) {
// Ignore bot's messages.
if (fromSelf) {
return false;
}
// The name of the user. (Will default to the lower case if the display name
// is missing from the user object.)
var name = user['display-name'] || user.username,
// A pre-formatted string to go at the beginning of command outputs.
greeting = '@' + name + ',';
// The text in the message split by spaces for easy use.
params = message
// Remove extra whitespace on ends.
.trim()
// Replace multiple spaces with 1.
.replace(/\s{2,}/g, ' ')
// Split the string at spaces.
.split(' '),
// Remove and return the first element.
command = params.shift(),
// Are there reamining parameters?
hasParams = params.length > 0,
// Default to the user if there's no first parameter.
userParam = hasParams ? params[0] : user.username;
// Is it a command?
if (command[0] === '!') {
// Remove the exclamation mark
command = command.replace(/^!+/, '');
}
// Wasn't a command, ignore this normal message.
else {
return false;
}
// Check that the command was !logs
if (command === 'logs') {
// Make the reply to `channel` using the greeting, the link, and first
// parameter or the username of the user if there's no parameter
client.say(channel,
greeting + ' http://logsplaceholder.com/userlogs/' + userParam
// Comes out like this:
// @<name>, http://logsplaceholder.com/userlogs/<param[0] || name>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment