-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscord-ext.js
More file actions
83 lines (76 loc) · 2.79 KB
/
discord-ext.js
File metadata and controls
83 lines (76 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class DiscordAuth {
constructor() {
this.userData = null;
}
getInfo() {
return {
id: 'discordAuth',
name: 'Discord Auth',
blocks: [
{
opcode: 'auth',
blockType: Scratch.BlockType.COMMAND,
text: 'authenticate user'
},
{
opcode: 'getUsername',
blockType: Scratch.BlockType.REPORTER,
text: 'username'
},
{
opcode: 'getProfilePicture',
blockType: Scratch.BlockType.REPORTER,
text: 'profile picture'
},
{
opcode: 'isAuthenticated',
blockType: Scratch.BlockType.BOOLEAN,
text: 'authenticated?'
}
]
};
}
async auth({ width, height }) {
return new Promise((resolve, reject) => {
let top = (screen.height - height) / 2;
let left = (screen.width - width) / 2;
const authWindow = window.open(
`https://opensnail.onrender.com/auth/discord`,
'Discord Auth',
`scrollbars=1,resizable=no,width=${width},height=${height},top=${top},left=${left}`
);
const intervalId = setInterval(async () => {
if (authWindow.closed) {
clearInterval(intervalId);
try {
// Fetch user data from the server
const response = await fetch('https://opensnail.onrender.com/api/userdata');
if (!response.ok) {
throw new Error('Failed to fetch user data');
}
// Extract username and profile picture from the response JSON
this.userData = await response.json();
// Resolve with the extracted data
resolve(this.userData);
} catch (error) {
// Reject with the error if fetching or parsing fails
reject(error);
}
}
}, 1000);
});
}
getUsername() {
// Reporter block to get the username
return this.userData ? this.userData.username : 'Please Authenticate';
}
getProfilePicture() {
// Reporter block to get the profile picture
return this.userData ? this.userData.profile_picture : '';
}
isAuthenticated() {
// Boolean block to check if the user is authenticated
return this.userData !== null;
}
}
Scratch.extensions.register(new DiscordAuth());