lampdiscordbot/vrchat.js
2022-04-01 23:05:40 -07:00

98 lines
3.1 KiB
JavaScript

var vrchat = require("vrchat");
var client = require("./client");
var config = require("./config");
var fs = require("fs");
var configuration = new vrchat.Configuration(config.vrchat_configuration);
var authenticationApi = new vrchat.AuthenticationApi(configuration);
authenticationApi.getCurrentUser().then(console.log);
var usersApi = new vrchat.UsersApi(configuration);
var worldsApi = new vrchat.WorldsApi(configuration);
var status2icon = {
"join me": '🔵',
"active": '🟢',
"ask me": '🟠',
"busy": '🔴',
"offline": '⚫'
}
try {
var vrcul = JSON.parse(fs.readFileSync("data/vrcul.json"), "utf8");
} catch(error) {
var vrcul = [];
}
async function updateUserStatuses() {
for (let x of vrcul) {
try {
let user = (await usersApi.getUser(x.userId)).data;
let status_icon = user.state == "online" ? status2icon[user.status] : '⚫';
let nn = `${status_icon} ${user.displayName}`;
var channel = client.channels.resolve(x.channel);
if (!channel) {
channel = await client.channels.resolve(config.vrchat_status_category).createChannel(nn, {type: "GUILD_VOICE"});
x.channel = channel.id;
fs.writeFileSync("data/vrcul.json", JSON.stringify(vrcul));
} else if (nn != channel.name) {
await channel.setName(nn);
}
continue;
let belowChannel = client.channels.resolve(config.vrchat_status_category).children.find(x => x.position == channel.position + 1);
if (user.worldId && user.worldId != "offline") {
let bcn = `${await getWorldNameForId(user.worldId)}`;
if (belowChannel && belowChannel.name.startsWith('┗') && belowChannel.name != bcn) { //todo debug
await belowChannel.setName(bcn);
} else {
let ch = await client.channels.resolve(config.vrchat_status_category).createChannel(`${await getWorldNameForId(user.worldId)}`, {type: "GUILD_VOICE", /*position: belowChannel.position*/});
// position option is erratic
await ch.setPosition(channel.position + 1);
}
} else if (belowChannel?.name.startsWith('┗')) {
await belowChannel.delete();
}
} catch (error) {
console.error("vrcus", error.stack);
}
}
}
module.exports.interval = setInterval(updateUserStatuses, 1000*60*5);
client.on("interactionCreate", async i => {
if (i.commandName == "addvru") {
try {
await i.deferReply();
let u = i.options.getString("user");
let user = (await usersApi[u.startsWith("usr_") ? 'getUser' : 'getUserByName'](u)).data;
let status_icon = user.state == "online" ? status2icon[user.status] : '⚫';
let nn = `${status_icon} ${user.displayName}`;
let channel = await client.channels.resolve(config.vrchat_status_category).createChannel(nn, {type: "GUILD_VOICE"});
vrcul.push({channel: channel.id, userId: user.id});
fs.writeFileSync("data/vrcul.json", JSON.stringify(vrcul));
await i.editReply(channel.toString());
} catch (error) {
i.reply(error.message);
}
}
});
var worldnamecache = {};
async function getWorldNameForId(worldId) {
if (worldId == "private") return "Private World";
return worldnamecache[worldId] = worldnamecache[worldId] || (await worldsApi.getWorld(worldId)).data.name;
}