Skip to content

Instantly share code, notes, and snippets.

@Efreak Efreak/irc-steam-relay.js

Last active Aug 29, 2015
Embed
What would you like to do?
seishun/steam-irc-relay hacked to work with node-steam-chat-bot
/*
To use:
1. 'npm install steam irc' in your main bot directory
2. add the following to the end of your config.js file
var relay = require('./irc-steam-relay.js');
3. Below that, add the relay({ code FAQ at https://github.com/seishun/irc-steam-relay
- leave out the username, password, and authCode fields.
- Add another field, steamClient: myBot (where myBot is the name of your bot object, as seen in https://github.com/efreak/node-steam-chat-bot/blob/master/example.js#L6)
*/
var Steam = require('steam');
module.exports = function(details) {
var msgFormat = details.msgFormat || '\u000302%s\u000f: %s';
var emoteFormat = details.emoteFormat || '\u000302%s %s';
var msgFormatGame = details.msgFormatGame || details.msgFormat || '\u000303%s\u000f: %s';
var emoteFormatGame = details.emoteFormatGame || details.emoteFormat || '\u000303%s %s';
var queue = [];
function sendMessage(msg) {
if (steam.loggedOn) {
steam.sendMessage(details.chatroom, msg);
} else {
queue.push(msg);
}
}
var irc = new (require('irc')).Client(details.server, details.nick, {
channels: [details.channel]
});
irc.on('error', function(err) {
console.log('IRC error: ', err);
});
irc.on('message' + details.channel, function(from, message) {
sendMessage('<' + from + '> ' + message);
if (!steam.loggedOn)
return;
var parts = message.match(/(\S+)\s+(.*\S)/);
var triggers = {
'.k': 'kick',
'.kb': 'ban',
'.unban': 'unban'
};
if (parts && parts[1] in triggers) {
irc.whois(from, function(info) {
if (info.channels.indexOf('@' + details.channel) == -1)
return; // not OP, go away
Object.keys(steam.users).filter(function(steamID) {
return steam.users[steamID].playerName == parts[2];
}).forEach(function(steamID) {
steam[triggers[parts[1]]](details.chatroom, steamID);
});
});
} else if (message.trim() == '.userlist') {
Object.keys(steam.chatRooms[details.chatroom]).forEach(function(steamID) {
irc.notice(from, steam.users[steamID].playerName + ' http://steamcommunity.com/profiles/' + steamID);
});
}
});
irc.on('action', function(from, to, message) {
if (to == details.channel) {
sendMessage(from + ' ' + message);
}
});
irc.on('+mode', function(channel, by, mode, argument, message) {
if (channel == details.channel && mode == 'b') {
sendMessage(by + ' sets ban on ' + argument);
}
});
irc.on('-mode', function(channel, by, mode, argument, message) {
if (channel == details.channel && mode == 'b') {
sendMessage(by + ' removes ban on ' + argument);
}
});
irc.on('kick' + details.channel, function(nick, by, reason, message) {
sendMessage(by + ' has kicked ' + nick + ' from ' + details.channel + ' (' + reason + ')');
});
irc.on('join' + details.channel, function(nick) {
sendMessage(nick + ' has joined ' + details.channel);
});
irc.on('part' + details.channel, function(nick) {
sendMessage(nick + ' has left ' + details.channel);
});
irc.on('quit', function(nick, reason) {
sendMessage(nick + ' has quit (' + reason + ')');
});
var steam = details.steamClient;
steam.on('loggedOn', function(result) {
steam.joinChat(details.chatroom);
queue.forEach(sendMessage);
queue = [];
});
steam.on('chatMsg', function(chatRoom, message, msgType, chatter) {
var game = steam.users[chatter].gameName;
var name = steam.users[chatter].playerName;
if (msgType == Steam.EChatEntryType.ChatMsg) {
irc.say(details.channel, require('util').format(game ? msgFormatGame : msgFormat, name, message));
} else if (msgType == Steam.EChatEntryType.Emote) {
irc.say(details.channel, require('util').format(game ? emoteFormatGame : emoteFormat, name, message));
}
var parts = message.split(/\s+/);
var permissions = steam.chatRooms[chatRoom][chatter].permissions;
if (parts[0] == '.k' && permissions & Steam.EChatPermission.Kick) {
irc.send('KICK', details.channel, parts[1], 'requested by ' + name);
} else if (parts[0] == '.kb' && permissions & Steam.EChatPermission.Ban) {
irc.send('MODE', details.channel, '+b', parts[1]);
irc.send('KICK', details.channel, parts[1], 'requested by ' + name);
} else if (parts[0] == '.unban' && permissions & Steam.EChatPermission.Ban) {
irc.send('MODE', details.channel, '-b', parts[1]);
} else if (parts[0] == '.userlist') {
irc.send('NAMES', details.channel);
irc.once('names' + details.channel, function(nicks) {
steam.sendMessage(chatter, 'Users in ' + details.channel + ':\n' + Object.keys(nicks).map(function(key) {
return nicks[key] + key;
}).join('\n'));
});
}
});
steam.on('chatStateChange', function(stateChange, chatterActedOn, chat, chatterActedBy) {
var name = steam.users[chatterActedOn].playerName + ' (http://steamcommunity.com/profiles/' + chatterActedOn + ')';
switch (stateChange) {
case Steam.EChatMemberStateChange.Entered:
irc.say(details.channel, name + ' entered chat.');
break;
case Steam.EChatMemberStateChange.Left:
irc.say(details.channel, name + ' left chat.');
break;
case Steam.EChatMemberStateChange.Disconnected:
irc.say(details.channel, name + ' disconnected.');
break;
case Steam.EChatMemberStateChange.Kicked:
irc.say(details.channel, name + ' was kicked by ' + steam.users[chatterActedBy].playerName + '.');
break;
case Steam.EChatMemberStateChange.Banned:
irc.say(details.channel, name + ' was banned by ' + steam.users[chatterActedBy].playerName + '.');
}
});
steam.on('debug', console.log);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.