Skip to content

Commit 170e312

Browse files
committed
wip: v2
0 parents  commit 170e312

19 files changed

+6242
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
src/generated
3+
dist

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

eslint.config.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { configure } from "@ariesclark/eslint-config";
2+
3+
export default configure({
4+
type: "lib",
5+
ignores: [
6+
"dist",
7+
"src/generated",
8+
"package.json",
9+
]
10+
});

example/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VRCHAT_USERNAME=
2+
VRCHAT_PASSWORD=
3+
VRCHAT_TOTP_SECRET=

example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env*
2+
!.env.example
3+
data.json

example/index.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 });

example/package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"dotenv": "^16.5.0",
14+
"enquirer": "^2.4.1",
15+
"keyv": "^5.3.4",
16+
"keyv-file": "^5.1.2",
17+
"prompts": "^2.4.2",
18+
"vrchat": "^1.19.4"
19+
},
20+
"devDependencies": {
21+
"@types/prompts": "^2.4.9"
22+
}
23+
}

example/pnpm-lock.yaml

Lines changed: 184 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openapi-ts.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineConfig } from "@hey-api/openapi-ts";
2+
3+
export default defineConfig({
4+
input: "https://vrchat.community/openapi.yaml",
5+
output: "src/generated",
6+
plugins: [
7+
{
8+
name: "@hey-api/typescript",
9+
exportFromIndex: false,
10+
},
11+
{
12+
name: "@hey-api/client-fetch",
13+
exportFromIndex: false,
14+
},
15+
{
16+
name: "@hey-api/transformers",
17+
exportFromIndex: false,
18+
},
19+
{
20+
name: "@hey-api/sdk",
21+
exportFromIndex: false,
22+
auth: false,
23+
asClass: true,
24+
instance: true,
25+
transformer: true,
26+
classNameBuilder: () => "_VRChat",
27+
},
28+
],
29+
});

0 commit comments

Comments
 (0)