Added o!tetrio-rooms

Can now view the TETR.IO room list
This commit is contained in:
Max Gorley 2020-09-30 02:26:06 -04:00 committed by GitHub
parent cdd25ba2ab
commit 0608f74d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1350 additions and 8 deletions

View File

@ -1,4 +1,5 @@
const Discord = require('discord.js');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const Constants = require('discord.js/src/util/Constants.js');
const fs = require('fs');
Constants.DefaultOptions.ws.properties.$browser='Discord Android';
@ -11,36 +12,42 @@ const bot = new Discord.Client({autoReconnect:true});
// Display console message when logged in
bot.once('ready', () => {
console.log('Connected');
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE',{type: 'PLAYING'});
console.log('O-Bot is ready!');
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE 2',{type: 'PLAYING'});
// Uncomment to set status to "idle"
/*bot.user.setPresence({
status: 'idle'
});*/
});
// Reconnecting
bot.on("reconnecting", () => {
console.log("[" + getTime() + "] Reconnecting!");
console.log("Reconnecting!");
});
// Resume
bot.on("resume", () => {
console.log("[" + getTime() + "] Connection restored!");
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE',{type: 'PLAYING'});
console.log("Connection restored!");
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE 2',{type: 'PLAYING'});
});
//bot.on('debug', console.log);
bot.on('message', message => {
bot.user.setActivity('Puyo Puyo\u2122Tetris\u00AE 2',{type: 'PLAYING'});
// Prefix is 'o!'
if (message.content.substring(0, 2) == 'o!') {
var args = message.content.substring(2).split(' ');
var cmd = args[0];
args = args.splice(1);
// Commands
switch(cmd) {
// o!help
case 'help':
message.channel.send("Commands:\n\n**General**\n``o!help``: Display this message\n``o!pi``: Make O speak\n\n**Voice**\n``o!voice``: Make O speak for real\n``o!altvoice``: Make O speak in his alternate voice\n``o!jvoice``: Play one of O's Japanese voice lines\n``o!jaltvoice``: Play one of O's alternate Japanese voice lines\n``o!listen``: O-Bot will record your voice and send it back to you! (These recordings are NOT stored locally.)\n``o!dc``: Make O-Bot leave the voice channel\n\n**Other**\n``o!twitch``: Get link for O-Bot creator's Twitch channel\n``o!donate``: Feed O-Bot");
message.channel.send("Commands:\n\n**General**\n``o!help``: Display this message\n``o!pi``: Make O speak\n\n**Voice**\n``o!voice``: Make O speak for real\n``o!altvoice``: Make O speak in his alternate voice\n``o!jvoice``: Play one of O's Japanese voice lines\n``o!jaltvoice``: Play one of O's alternate Japanese voice lines\n``o!listen``: O-Bot will record your voice and send it back to you! (These recordings are NOT stored locally.)\n``o!dc``: Make O-Bot leave the voice channel\n\n**Other**\n``o!tetrio-rooms``: Show all open rooms in TETR.IO\n``o!twitch``: Get link for O-Bot creator's Twitch channel\n``o!donate``: Feed O-Bot");
break;
// o!pi
@ -88,6 +95,17 @@ bot.on('message', message => {
message.member.voice.channel.leave();
}
break;
case 'leave':
if(!message.member.voice.channel){
message.channel.send("``o!leave`` can only be run while you are in a voice channel!");
}
else{
message.channel.send("Disconnecting!");
message.member.voice.channel.leave();
}
break;
// o!randomhex
case 'randomhex':
@ -103,6 +121,13 @@ bot.on('message', message => {
case 'listen':
listen(message);
break;
// o!tetrio-rooms
case 'tetrio-rooms':
message.channel.startTyping();
httpGet(message, "https://tetr.io/api/rooms");
//message.channel.send("Command temporarily unavailable");
break;
// Invalid command
default:
@ -269,6 +294,52 @@ async function listen(message){
}
function printRooms(message, jsonInput){
var additionalText = "";
var roomList = "";
var roomState = "";
roomStats = JSON.parse(jsonInput);
message.channel.send("Room list:");
for(i in roomStats.rooms){
additionalText = "";
if(roomStats.rooms[i].meta.userlimit>0){
additionalText = "/" + roomStats.rooms[i].meta.userlimit;
}
if(roomStats.rooms[i].playercount > roomStats.rooms[i].playingplayers){
additionalText = additionalText + " " + "+" + (roomStats.rooms[i].playercount - roomStats.rooms[i].playingplayers);
}
roomList = roomList + "\n" + roomStats.rooms[i].meta.name + " -- " + roomStats.rooms[i].playingplayers + additionalText + "\n" + roomStats.rooms[i].state + "\nhttps://tetr.io/#" + roomStats.rooms[i].id + "\n\n";
}
message.channel.send(roomList);
message.channel.stopTyping();
//console.log(jsonInput);
//console.log(roomStats.rooms);
}
function httpGet(message, URL)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", URL, true);
xmlHttp.setRequestHeader("Authorization", "Bearer " + auth.tetrio_token);
xmlHttp.onload = function(e){
if(xmlHttp.readyState === 4){
if(xmlHttp.status === 200){
printRooms(message, xmlHttp.responseText);
} else{
console.error(xmlHttp.statusText);
message.channel.stopTyping();
}
}
};
xmlHttp.onerror = function (e){
console.error(xmlHttp.statusText);
message.channel.stopTyping();
};
xmlHttp.send( null );
}
bot.login(auth.token);

1269
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,11 +8,13 @@
"@discordjs/opus": "^0.1.0",
"bufferutil": "^4.0.1",
"discord.js": "^12.0.2",
"dotenv": "^8.2.0",
"erlpack": "github:discordapp/erlpack",
"ffmpeg-static": "^3.0.0",
"install-peerdeps": "^2.0.1",
"install-peers": "^1.0.3",
"libsodium-wrappers": "^0.7.6",
"xmlhttprequest": "^1.8.0",
"zlib-sync": "^0.1.6"
}
}