-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbot.js
More file actions
129 lines (129 loc) · 5.38 KB
/
bot.js
File metadata and controls
129 lines (129 loc) · 5.38 KB
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
'use strict';
let http = require('http'), SDClient = require('./client.js'), Discord = require('discord.js'), globaljs = require('./global.js'), options = {serverid: config.serverid, loginServer: 'https://play.pokemonshowdown.com/~~' + config.serverid +'/action.php', nickName: config.nickName, pass: config.pass, avatar: (config.avatar)?config.avatar:null, status: (config.status)?config.status:null, autoJoin: config.autoJoin};
global.Bot = new SDClient(config.server, config.port, options);
Bot.connect();
if (config.token) {
let client = new Discord.client();
client.login(config.token);
client.on("ready", () => {console.log(`Connected to Discord.`); if (config.discordStatus) client.user.setActivity(config.discordStatus)});
}
else {
let client = null;
}
let standard_input = process.stdin; standard_input.setEncoding('utf-8'); standard_input.on('data', function(data) { try {console.log(eval(data));} catch(e) {console.log(e);}});
if (config.token) {
client.on('message', async message => {});
}
Bot.on('chat', function (room, time, by, message) {
if (message === Bot.status.nickName + '?') return Bot.pm(by, `Hi, I'm ${Bot.status.nickName}! I'm a Bot. My prefix is \`\`${prefix}\`\`. For more information, use \`\`${prefix}help\`\`.`);
if (!message.startsWith(prefix)) return;
if (ignoreRooms.includes(room)) return;
if (message === ',eval 1') return Bot.say(room, '1');
let args = message.substr(prefix.length).split(' ');
let commandName = tools.commandAlias(args.shift().toLowerCase());
tools.grantPseudo(by);
if (['eval','output'].includes(commandName)) {
if (!tools.hasPermission(toId(by), 'admin')) return Bot.say(room, 'Access denied.');
try {
let outp = eval(args.join(' '));
switch (typeof outp) {
case 'object':
outp = JSON.stringify(outp);
break;
case 'function':
outp = outp.toString();
break;
}
if (commandName === 'eval' || (!String(outp).split('\n')[1] && String(outp.length > 300))) Bot.say(room, outp);
else Bot.say(room, '!code ' + outp)
}
catch (e) {
console.log(e);
Bot.say(room, e.message);
}
return;
}
fs.readdir('./commands/global', (e, files) => {
if (e) return console.log(e);
if (!files.includes(commandName + '.js')) {
fs.readdir('./commands', (err, rfiles) => {
if (err) return console.log(err);
if (rfiles.includes(room)) {
fs.readdir('./commands/' + room, (error, roomfiles) => {
if (!roomfiles.includes(commandName + '.js')) return Bot.say(room, 'It doesn\'t look like that command exists.');
let commandRequire = require('./commands/' + room + '/' + commandName + '.js');
if (!tools.hasPermission(toId(by), commandRequire.permissions)) {
tools.spliceRank(by);
return Bot.say(room, 'Access denied.');
}
try {
if (cooldownObject[room] && cooldownObject[room][commandName]) return Bot.pm(by, 'Cooling down.');
commandRequire.commandFunction(Bot, room, time, by, args, client);
tools.setCooldown(commandName, room, commandRequire);
tools.spliceRank(by);
return;
}
catch (e) {
if (e) console.log(e);
}
});
}
});
return;
}
else {
let commandRequire = require('./commands/global/' + commandName + '.js');
if (!tools.hasPermission(toId(by), commandRequire.permissions)) {
tools.spliceRank(by);
return Bot.say(room, 'Access denied.');
}
try {
if (cooldownObject[room] && cooldownObject[room][commandName]) return Bot.pm(by, 'Cooling down.');
commandRequire.commandFunction(Bot, room, time, by, args, client);
tools.setCooldown(commandName, room, commandRequire);
tools.spliceRank(by);
return;
}
catch (e) {
if (e) console.log(e);
}
}
});
});
Bot.on('chaterror', (room, message, isIntro) => {if (!isIntro) console.log('ERROR: ' + room + '> ' + message);});
Bot.on('popup', (message) => {console.log('POPUP: ' + message);});
Bot.on('querydetails', (message) => {Bot.say(queryRoom, message);});
Bot.on('chatsuccess', (room, time, by, message) => Bot.rooms[room].rank = by.charAt(0));
Bot.on('pm', (by, message) => {
if (!message.startsWith(prefix)) {
Bot.pm(config.owner, by + ': ' + message);
return Bot.pm(by, `Hi, I'm ${Bot.status.nickName}! I'm a Bot. My prefix is \`\`${prefix}\`\`.`);
}
let args = message.substr(prefix.length).split(' ');
let commandName = tools.commandAlias(args.shift().toLowerCase());
tools.grantPseudo(by);
if (commandName === 'eval') {
if (!tools.hasPermission(toId(by), 'admin')) return Bot.pm(by, 'Access denied.');
try {
let outp = eval(args.join(' '));
switch (typeof outp) {
case 'object':
Bot.pm(by, JSON.stringify(outp));
break;
case 'function':
Bot.pm(by, outp.toString());
break;
default:
Bot.pm(by, outp);
break;
}
}
catch (e) {
console.log(e);
Bot.pm(by, e.message);
}
return;
}
if (commandName === 'help') return Bot.pm(by, 'I\'m a Bot by ' + config.owner + '. If you have any issues regarding me, please contact them. To see my usable commands, use the ``' + prefix + 'commands`` in a chatroom.');
tools.spliceRank(by);
});