forked from wyozi/hash.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
163 lines (115 loc) · 3.3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var config = require( "./config" );
var sortedcommands = [] // Array of all the commands sorted alphabetically
//
// Connection Handler
//
bot.on( "Connected", function() {
this.setStatus( "Online" );
this.setChat( config.Group );
} );
//
// Default Command Functions
//
function commandlist(name, steamID, args, argstr, group) {
// Only populate the sortedcommands array once.
if ( sortedcommands.length < 1 ) {
// Copy the command data into the array for sorting
for ( var key in this.Commands ) {
if (this.Commands.hasOwnProperty(key)) { // Ignore inherited members
var cmdinfo = this.Commands[key];
var cmdhelp = cmdinfo.helptext;
sortedcommands.push({command: key, helptext: cmdhelp});
}
}
// Sort the commands alphabetically
sortedcommands.sort(function(a, b){
a = a.command.toLowerCase();
b = b.command.toLowerCase();
switch ( a == b ? 0 : a < b ){
case 0:
return 0;
break;
case true:
return -1;
break;
case false:
return 1;
break;
default:
return 0;
break;
}
});
}
var helptext = "Current Commands:\n";
for ( var key in sortedcommands ) {
var commandinfo = sortedcommands[key];
helptext += "\t." + commandinfo.command;
if ( commandinfo.helptext ) {
helptext += " : " + commandinfo.helptext;
}
helptext += "\n";
}
// TODO: Send multiple messages if helptext exceeds steam message limit
bot.sendMessage(helptext, steamID);
if (steamID != group) {
bot.sendMessage( "I have private messaged you the commands " + name + "." );
}
}
//
// Default Commands
//
bot.registerCommand( "update", function( name, steamID ) {
if ( bot.isAdmin( steamID ) )
process.exit( 1 );
}, "[ADMIN] Forces the bot to rejoin the group chat." );
bot.registerCommand( "add", function( name, steamID ) {
bot.addFriend( steamID );
}, "Makes the bot send you a friend request." );
bot.registerCommand( "chat", function( name, steamID ) {
var index = bot.Listeners.indexOf( steamID );
if ( index == -1 ) {
bot.Listeners.push( steamID );
bot.sendMessage( name + " entered chat." );
} else {
bot.sendMessage( name + " disconnected." );
bot.Listeners.splice( index, 1 );
}
}, "Join the group chat using the bot as a middleman." );
bot.registerCommand( "help", commandlist, "This help text" );
bot.registerCommand( "commands", commandlist, "This help text" );
//
// CLI Output
//
function print( str ) {
console.log( str.replace( /[\x00-\x09]/g, "" ) );
}
bot.on( "Connected", function() {
print( "Connected." );
} );
bot.on( "Disconnected", function() {
print( "Lost Connection." );
} );
bot.on( "Message", function( name, steamID, msg, group ) {
if ( steamID == group ) {
print( "[PM] " + name + ": " + msg );
} else {
print( name + ": " + msg ); // TODO: Group name
}
} );
bot.on( "UserConnected", function( name, steamID ) {
print( name + " (" + steamID + ") connected." );
} );
bot.on( "UserDisconnected", function( name, steamID ) {
print( name + " (" + steamID + ") disconnected." );
} );
bot.on( "UserKicked", function( target, _, actor ) {
print( target + " was kicked by " + actor + "." );
} );
bot.on( "UserBanned", function( target, _, actor ) {
print( target + " was banned by " + actor + "." );
} );
//
// Init connection!
//
bot.connect( config.User, config.Pass ); // Here we go!