Skip to content

Commit 6f6ec64

Browse files
author
Quinella
committed
Initial commit 2: adding the files
1 parent 129d885 commit 6f6ec64

9 files changed

+650
-2
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/node_modules
2+
/node_modules/*
3+
npm-debug.log
4+
config.js
5+
6+
.hg
7+
.bzr
8+
.svn

LICENSE

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013 Robin
3+
Copyright (c) 2013 Robin Vandenbrande and other contributors
4+
5+
For some parts of the code taken from Pokemon Showdown:
6+
Copyright (c) 2013 Guangcong Luo and other Pokemon Showdown contributors
47

58
Permission is hereby granted, free of charge, to any person obtaining a copy of
69
this software and associated documentation files (the "Software"), to deal in

README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
Pokemon-Showdown-Bot
22
====================
33

4-
A simple chat bot for Pokémon Showdown
4+
A simple and very basic chat bot for [Pokémon Showdown][1].
5+
6+
[1]: http://www.pokemonshowdown.com/
7+
8+
9+
Installation
10+
------------
11+
12+
**This is not recommended for beginners!**
13+
Why? Because I'm too lazy to explain everything in detail.
14+
15+
Pokémon Showdown Bot requires [node.js][2] to run.
16+
This bot has not been tested on every `node.js` version possible, but has the same version requirements as [Pokémon Showdown][3]: either v0.6.3 through v0.8.22, or v0.10.2 and up.
17+
Install `node.js` if you don't have it yet, try the last stable version.
18+
19+
Next up is cloning this bot. This can be done in two ways: cloning it via `git` or downloading as ZIP.
20+
Downloading it as ZIP is the easy and lazy way, but is a lot less handy to update than cloning this repository.
21+
22+
To install dependencies, run:
23+
24+
npm install
25+
26+
Copy `config-example.js` to `config.js` and edit the needed variables.
27+
To change the commands that the bot responds to, edit `commands.js`.
28+
29+
Now, to start the bot, use:
30+
31+
npm main.js
32+
33+
Some information will be shown, and will automatically join the room(s) you specified if no error occurs.
34+
35+
[2]: http://nodejs.org/
36+
[3]: https://github.com/Zarel/Pokemon-Showdown
37+
38+
Development
39+
-----------
40+
41+
Everyone is more than welcome to contribute to the bot.
42+
However, please refrain from adding more example commands.
43+
44+
License
45+
-------
46+
47+
Pokémon Showdown Bot is distributed under the terms of the [MIT License][4].
48+
49+
[4]: https://github.com/Quinella/Pokemon-Showdown-Bot/blob/master/LICENSE

commands.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* This is the file where the bot commands are located
3+
*
4+
* @license MIT license
5+
*/
6+
7+
var http = require('http');
8+
var sys = require('sys');
9+
10+
exports.commands = {
11+
/**
12+
* It is recommended that you keep the following functions
13+
*/
14+
about: function(arg, by, room, con) {
15+
var text = '**Pokémon Showdown Bot** by: Quinella';
16+
this.say(con, room, text);
17+
},
18+
reload: function(arg, by) {
19+
if (!this.hasRank(by, '#~')) return false;
20+
try {
21+
this.uncacheTree('./commands.js');
22+
Commands = require('./commands.js').commands;
23+
} catch (e) {
24+
error('failed to reload: ' + sys.inspect(e));
25+
}
26+
},
27+
28+
/**
29+
* Example commands below.
30+
* Feel free to remove them and add your own.
31+
*/
32+
tell: 'say',
33+
say: function(arg, by, room, con) {
34+
if (!this.hasRank(by, '+%@&#~')) return false;
35+
this.say(con, room, arg + ' (' + by + ' said this)');
36+
},
37+
38+
joke: function(arg, by, room, con) {
39+
var self = this;
40+
41+
var reqOpt = {
42+
hostname: 'api.icndb.com',
43+
path: '/jokes/random',
44+
method: 'GET'
45+
};
46+
var req = http.request(reqOpt, function(res) {
47+
res.on('data', function(chunk) {
48+
try {
49+
var data = JSON.parse(chunk);
50+
self.say(con, room, data.value.joke);
51+
} catch (e) {
52+
self.say(con, room, 'Sorry, couldn\'t fetch a random joke... :(');
53+
}
54+
});
55+
});
56+
req.end();
57+
}
58+
};

config-example.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// The WEBSOCKET server and port the bot should connect to.
2+
// Most of the time this isn't the same as the URL, check the `Request URL` of
3+
// the websocket.
4+
// If you really don't know how to do this... Run `node getserver.js URL`.
5+
// Fill in the URL of the client where `URL` is.
6+
// For example: `node getserver.js http://example-server.psim.us/`
7+
exports.server = 'sim.psim.us';
8+
exports.port = 8000;
9+
10+
// This is the server id.
11+
// To know this one, you should check where the AJAX call 'goes' to when you
12+
// log in.
13+
// For example, on the Smogon server, it will say somewhere in the URL
14+
// ~~showdown, meaning that the server id is 'showdown'.
15+
// If you really don't know how to check this... run the said script above.
16+
exports.serverid = 'showdown';
17+
18+
// The nick and password to log in with
19+
// If no password is required, leave pass empty
20+
exports.nick = 'Example bot nick';
21+
exports.pass = '';
22+
23+
// The rooms that should be joined.
24+
// Joining Smogon's Showdown's Lobby is not allowed.
25+
exports.rooms = ['example room name', 'another example'];
26+
27+
// The character text should start with to be seen as a command.
28+
// Note that using / and ! might be 'dangerous' since these are used in
29+
// Showdown itself.
30+
// Using only alphanumeric characters and spaces is not allowed.
31+
exports.commandcharacter = '.';
32+
33+
// Whether this file should be watched for changes or not.
34+
// If you change this option, the server has to be restarted in order for it to
35+
// take effect.
36+
exports.watchconfig = false;
37+
38+
// Secondary websocket protocols should be defined here, however, Showdown
39+
// doesn't support that yet, so it's best to leave this empty.
40+
exports.secprotocols = [];
41+
42+
// What should be logged?
43+
// 0 = error, ok, info, debug, recv, send
44+
// 1 = error, ok, info, debug (recommended for development)
45+
// 2 = error, ok, info (recommended for production)
46+
// 3 = error, ok
47+
// 4 = error
48+
exports.debuglevel = 2;

getserver.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var sys = require('sys');
2+
var url = require('url');
3+
var http = require('http');
4+
5+
if (process.argv[2]) {
6+
var args = process.argv.slice(2);
7+
var serverUrl = args.join(' ');
8+
if (serverUrl.indexOf('://') !== -1) {
9+
serverUrl = url.parse(serverUrl).host;
10+
}
11+
if (serverUrl.slice(-1) === '/') {
12+
serverUrl = serverUrl.slice(0, -1);
13+
}
14+
15+
console.log('Getting data for ' + serverUrl + '...');
16+
console.log('This may take some time, depending on Showdown\'s speed.');
17+
18+
var received = false;
19+
var requestOptions = {
20+
hostname: 'play.pokemonshowdown.com',
21+
port: 80,
22+
path: '/crossdomain.php?host=' + serverUrl + '&path=',
23+
method: 'GET'
24+
};
25+
var req = http.request(requestOptions, function(res) {
26+
res.setEncoding('utf8');
27+
res.on('data', function(chunk) {
28+
if (received) {
29+
return;
30+
}
31+
received = true;
32+
33+
var search = 'var config = ';
34+
var index = chunk.indexOf(search);
35+
if (index !== -1) {
36+
var data = chunk.substr(index);
37+
data = data.substr(search.length, data.indexOf(';') - search.length);
38+
data = JSON.parse(data);
39+
console.log('---------------');
40+
console.log('host: ' + data.host);
41+
console.log('port: ' + data.port);
42+
console.log('serverid: ' + data.id);
43+
} else {
44+
console.log('ERROR: failed to get data!');
45+
}
46+
});
47+
});
48+
49+
req.on('error', function (err) {
50+
console.log('ERROR: ' + sys.inspect(err));
51+
});
52+
53+
req.end();
54+
} else {
55+
console.log('ERROR: no URL specified!');
56+
}

0 commit comments

Comments
 (0)