auto-reconnect
This commit is contained in:
parent
4eb2630424
commit
5386b869d1
154
index.js
154
index.js
@ -7,6 +7,7 @@ const config = require("./config");
|
||||
|
||||
var discordBot = new Discord.Client({disableEveryone:true});
|
||||
discordBot.login(config.discord_token);
|
||||
discordBot.on("error", console.error);
|
||||
var banlist;
|
||||
|
||||
var bridgemap = config.bridges; // owop channel to bridge info (discord channel id, password)
|
||||
@ -20,86 +21,84 @@ discordBot.once("ready", function(){
|
||||
banlist = require("./banlist")(discordBot);
|
||||
});
|
||||
|
||||
|
||||
function createOWOPbridge(owopWorld, discordChannelIDs, password) {
|
||||
|
||||
var discordChannels = discordChannelIDs.map(discordChannelID => discordBot.channels.get(discordChannelID));
|
||||
var discordChannels = discordChannelIDs.map(discordChannelID => discordBot.channels.get(discordChannelID)).filter(x => x);
|
||||
if (!discordChannels.length) return console.error("Could not find any of the discord channels:", discordChannelIDs);
|
||||
|
||||
var botId = 0;
|
||||
var owopSocket = new WebSocket("wss://ourworldofpixels.com", {
|
||||
origin: "https://ourworldofpixels.com/"
|
||||
});
|
||||
|
||||
owopSocket.on("open", function () {
|
||||
console.log("owop", owopWorld, "open");
|
||||
});
|
||||
|
||||
owopSocket.on("message", function (data) {
|
||||
|
||||
if (typeof data == "string") {
|
||||
console.debug(`[${owopWorld}]`, data);
|
||||
|
||||
if (data.startsWith(botId) || data.startsWith(`[${botId}]`)) return; // ignore self if regular user
|
||||
if (data.startsWith("[D]")) return; // ignore self if special discord user
|
||||
if (data.startsWith("DEV")) return;
|
||||
if (data.startsWith("Nickname")) return; // ignore nickname change messages
|
||||
if (data.startsWith("User: ")) return;
|
||||
if (data.startsWith("<")) return; // ignore HTML greeting
|
||||
if (data == "Server: You are now a moderator. Do /help for a list of commands.") return; // ignore that
|
||||
if (data.startsWith("[Server]")) return; // ignore [Server] messages
|
||||
if (data.startsWith("->")) return; // ignore direct messages because spam
|
||||
|
||||
let msg = data.replace(/<@/g, "<\\@"); // filter mentions
|
||||
if (owopWorld == "main") msg = require('./antiswear')(msg);
|
||||
for (let discordChannel of discordChannels) {
|
||||
let lastMessage = discordChannel.messages.last();
|
||||
if (lastMessage && lastMessage.originalMsg && lastMessage.originalMsg == msg && lastMessage.author.id == discordBot.user.id) {
|
||||
// if this owop message is same as source of last message and last message was sent by this bot
|
||||
// edit last message with incremented number of repetitions
|
||||
let postfix = ` [x${++lastMessage.repetitions}]`;
|
||||
lastMessage.edit(msg.substr(0, 2000-postfix.length) + postfix).catch(error => console.error(`Could not edit message ${lastMessage.id}`, error.message));
|
||||
lastMessage.realmsg = msg; // attach actual message to Message object because now the message content has been edited
|
||||
} else {
|
||||
// send new message
|
||||
discordChannel.send(msg, { split: { char: '' } }).then(message => {
|
||||
message.originalMsg = msg; // attach original owop message to Message object so we can edit for repetitions
|
||||
message.repetitions = 1; // keep track of number of repetitions
|
||||
}).catch(error => console.error(`Failed to send OWOP message to discordChannel ${[discordChannel.id, '#'+discordChannel.name, discordChannel.guild.name]}:`, error.message));
|
||||
var botId = 0, owopSocket;
|
||||
(function connect() {
|
||||
owopSocket = new WebSocket("wss://ourworldofpixels.com", {
|
||||
origin: "https://ourworldofpixels.com/"
|
||||
});
|
||||
owopSocket.on("open", function () {
|
||||
console.log("owop", owopWorld, "open");
|
||||
});
|
||||
owopSocket.on("message", function (data) {
|
||||
if (typeof data == "string") {
|
||||
console.log(`[${owopWorld}]`, data);
|
||||
if (data.startsWith(botId) || data.startsWith(`[${botId}]`)) return; // ignore self if regular user
|
||||
if (data.startsWith("[D]")) return; // ignore self if special discord user
|
||||
if (data.startsWith("DEV")) return;
|
||||
if (data.startsWith("Nickname")) return; // ignore nickname change messages
|
||||
if (data.startsWith("User: ")) return;
|
||||
if (data.startsWith("<")) return; // ignore HTML greeting
|
||||
if (data == "Server: You are now a moderator. Do /help for a list of commands.") return; // ignore that
|
||||
if (data.startsWith("[Server]")) return; // ignore [Server] messages
|
||||
if (data.startsWith("->")) return; // ignore direct messages because spam
|
||||
let msg = data.replace(/<@/g, "<\\@"); // filter mentions
|
||||
if (owopWorld == "main") msg = require('./antiswear')(msg);
|
||||
for (let discordChannel of discordChannels) {
|
||||
let lastMessage = discordChannel.messages.last();
|
||||
if (lastMessage && lastMessage.originalMsg && lastMessage.originalMsg == msg && lastMessage.author.id == discordBot.user.id) {
|
||||
// if this owop message is same as source of last message and last message was sent by this bot
|
||||
// edit last message with incremented number of repetitions
|
||||
let postfix = ` [x${++lastMessage.repetitions}]`;
|
||||
lastMessage.edit(msg.substr(0, 2000-postfix.length) + postfix).catch(error => console.error(`Could not edit message ${lastMessage.id}`, error.message));
|
||||
lastMessage.realmsg = msg; // attach actual message to Message object because now the message content has been edited
|
||||
} else {
|
||||
// send new message
|
||||
discordChannel.send(msg, { split: { char: '' } }).then(message => {
|
||||
message.originalMsg = msg; // attach original owop message to Message object so we can edit for repetitions
|
||||
message.repetitions = 1; // keep track of number of repetitions
|
||||
}).catch(error => console.error(`Failed to send OWOP message to discordChannel ${[discordChannel.id, '#'+discordChannel.name, discordChannel.guild.name]}:`, error.message));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch (data.readUInt8(0)) {
|
||||
case 0: // Get id
|
||||
botId = data.readUInt32LE(1);
|
||||
console.log("owop", owopWorld, "ready");
|
||||
if (password) owopSocket.send("/pass " + password + String.fromCharCode(10));
|
||||
sendMove();
|
||||
break;
|
||||
case 5: // Captcha
|
||||
switch (data.readUInt8(1)) {
|
||||
case 0:
|
||||
owopSocket.send("CaptchALETMEINPLZ" + config.captcha_password);
|
||||
break;
|
||||
case 3:
|
||||
joinWorld(owopWorld);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (data.readUInt8(0)) {
|
||||
case 0: // Get id
|
||||
botId = data.readUInt32LE(1);
|
||||
console.log("owop", owopWorld, "ready");
|
||||
if (password) owopSocket.send("/pass " + password + String.fromCharCode(10));
|
||||
sendMove();
|
||||
setInterval(sendMove, 600000);
|
||||
break;
|
||||
case 5: // Captcha
|
||||
switch (data.readUInt8(1)) {
|
||||
case 0:
|
||||
owopSocket.send("CaptchALETMEINPLZ" + config.captcha_password);
|
||||
break;
|
||||
case 3:
|
||||
joinWorld(owopWorld);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
owopSocket.on("close", function () {
|
||||
console.log("owop", owopWorld, "close");
|
||||
});
|
||||
owopSocket.on("error", function (error) {
|
||||
console.error("owop", owopWorld, "error:", error);
|
||||
});
|
||||
});
|
||||
owopSocket.on("close", function () {
|
||||
console.log("owop", owopWorld, "close");
|
||||
setTimeout(()=>{
|
||||
connect();
|
||||
}, 5000);
|
||||
});
|
||||
owopSocket.on("error", function (error) {
|
||||
console.error("owop", owopWorld, "error:", error);
|
||||
});
|
||||
})();
|
||||
|
||||
function sendMove() {
|
||||
owopSocket.send(new Buffer([127, 255, 255, 255, 127, 255, 255, 255, 0, 0, 0, 0]));
|
||||
if (owopSocket.readyState == WebSocket.OPEN) owopSocket.send(new Buffer([127, 255, 255, 255, 127, 255, 255, 255, 0, 0, 0, 0]));
|
||||
}
|
||||
setInterval(sendMove, 600000);
|
||||
function joinWorld(name) {
|
||||
var nstr = stoi(name, 24/*OldProtocol.maxWorldNameLength*/);
|
||||
//_global.eventSys.emit(_conf.EVENTS.net.world.joining, name);
|
||||
@ -109,7 +108,7 @@ function createOWOPbridge(owopWorld, discordChannelIDs, password) {
|
||||
dv.setUint8(i, nstr[0][i]);
|
||||
}
|
||||
dv.setUint16(nstr[0].length, 4321/*OldProtocol.misc.worldVerification*/, true);
|
||||
owopSocket.send(array);
|
||||
if (owopSocket.readyState == WebSocket.OPEN) owopSocket.send(array);
|
||||
return nstr[1];
|
||||
}
|
||||
function stoi(string, max) {
|
||||
@ -126,7 +125,6 @@ function createOWOPbridge(owopWorld, discordChannelIDs, password) {
|
||||
return [ints, fstring];
|
||||
}
|
||||
|
||||
|
||||
discordBot.on("message", function (message) {
|
||||
if (!discordChannelIDs.includes(message.channel.id)) return;
|
||||
if (message.author.id == discordBot.user.id) return;
|
||||
@ -162,14 +160,12 @@ function createOWOPbridge(owopWorld, discordChannelIDs, password) {
|
||||
let msg = prefix + message.cleanContent;
|
||||
if (msg.length > 128) msg = msg.substr(0,127) + '…';
|
||||
owopSocket.send(msg + String.fromCharCode(10));
|
||||
|
||||
});
|
||||
|
||||
|
||||
console.log("bridged owop world", owopWorld, "to discord channels", discordChannels.map(d => [d.id, d.name, d.guild.name]));
|
||||
return {owopSocket, discordChannels}
|
||||
|
||||
}
|
||||
|
||||
require("./commands")(discordBot);
|
||||
|
||||
if (config.enable_commands) require("./commands")(discordBot);
|
||||
if (config.reddit) require("./pixelart2reddit&facebook")(discordBot);
|
||||
|
Loading…
x
Reference in New Issue
Block a user