|
| 1 | +/* eslint-disable no-console, antfu/no-top-level-await */ |
| 2 | +import KeyvFile from "keyv-file"; |
| 3 | +import prompts from "prompts"; |
| 4 | +import { VRChat } from "vrchat"; |
| 5 | + |
| 6 | +const vrchat = new VRChat({ |
| 7 | + /** |
| 8 | + * When using the VRChat API, you must provide an application name, version, and contact information. |
| 9 | + * This is used to identify your application to VRChat, and to provide support if needed. |
| 10 | + */ |
| 11 | + application: { |
| 12 | + name: "Example", |
| 13 | + version: "1.5.1", |
| 14 | + /** |
| 15 | + * An email, or a URL to a support page. |
| 16 | + */ |
| 17 | + contact: "timmy@examaple.com" |
| 18 | + }, |
| 19 | + /** |
| 20 | + * Save cookies to a file, so you can use them later. |
| 21 | + * |
| 22 | + * This is optional, but recommended for persistent sessions. |
| 23 | + * If you don't provide a `keyv` instance, it will use an in-memory store. |
| 24 | + * |
| 25 | + * You can use any Keyv-compatible adapter for this, such as `keyv-file`, `@keyv/redis`, or `@keyv/postgres`. |
| 26 | + * See https://keyv.org/docs/keyv/#official-storage-adapters for more information. |
| 27 | + */ |
| 28 | + keyv: new KeyvFile({ filename: "./data.json" }), |
| 29 | +}); |
| 30 | + |
| 31 | +const { data: user } = await vrchat |
| 32 | + .getCurrentUser({ throwOnError: true }) |
| 33 | + // Since we've passed `throwOnError: true`, this will throw an error if the user is not logged in. |
| 34 | + // We can catch that error and prompt the user for their credentials. |
| 35 | + .catch(async () => { |
| 36 | + /** |
| 37 | + * Ask the user for their VRChat username and password. |
| 38 | + * You can also use environment variables, or any other method to get the credentials. |
| 39 | + */ |
| 40 | + const { username, password } = await prompts([ |
| 41 | + { |
| 42 | + name: "username", |
| 43 | + type: "text", |
| 44 | + message: "VRChat username", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "password", |
| 48 | + type: "password", |
| 49 | + message: "VRChat password", |
| 50 | + }, |
| 51 | + ]); |
| 52 | + |
| 53 | + return vrchat.login({ |
| 54 | + username, |
| 55 | + password, |
| 56 | + /** |
| 57 | + * This function will be called when two-factor authentication is required. |
| 58 | + * Accepts any code, such as from an authenticator app or a recovery code. |
| 59 | + */ |
| 60 | + twoFactorCode: async () => { |
| 61 | + const { code } = await prompts({ |
| 62 | + name: "code", |
| 63 | + type: "text", |
| 64 | + message: "Two-factor authentication code", |
| 65 | + }); |
| 66 | + |
| 67 | + return code; |
| 68 | + }, |
| 69 | + /** |
| 70 | + * Or, automatically generate a code from a secret. |
| 71 | + * Useful for service accounts & automated workflows. |
| 72 | + * |
| 73 | + * If this is a user-initiated login, don't use this. |
| 74 | + */ |
| 75 | + // totpSecret: "", |
| 76 | + throwOnError: true, |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | +console.log(`Logged in as ${user.displayName} (id: ${user.id}).`); |
| 81 | + |
| 82 | +/** |
| 83 | + * Get 10 online friends, you can change the `n` parameter to get more or less friends. |
| 84 | + * Exercise: You can also use the `offline` query parameter to include offline friends. |
| 85 | + */ |
| 86 | +const { data: friends } = await vrchat.getFriends({ query: { n: 10 }, throwOnError: true }); |
| 87 | + |
| 88 | +console.log( |
| 89 | + friends.length > 0 |
| 90 | + ? "Your friends:" |
| 91 | + : "Aw, you don't have any online friends." |
| 92 | +); |
| 93 | +for (const friend of friends) { |
| 94 | + console.log(`- ${friend.displayName} (id: ${friend.id})`); |
| 95 | +} |
| 96 | + |
| 97 | +const tupperId = "usr_c1644b5b-3ca4-45b4-97c6-a2a0de70d469"; |
| 98 | + |
| 99 | +await vrchat |
| 100 | + // Add Tupper as a friend, he's a nice guy. |
| 101 | + .friend({ path: { userId: tupperId }, throwOnError: true }) |
| 102 | + // If we've already sent one, we'll catch the error and ignore it. |
| 103 | + .catch(() => { }); |
| 104 | + |
| 105 | +const { data: { outgoingRequest } } = await vrchat |
| 106 | + .getFriendStatus({ path: { userId: tupperId }, throwOnError: true }); |
| 107 | + |
| 108 | +// Tupper ghosted our friend request, so let's delete it. |
| 109 | +if (outgoingRequest) await vrchat.deleteFriendRequest({ path: { userId: tupperId }, throwOnError: true }); |
0 commit comments