48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
/**
|
|
* robloxConfig.js
|
|
* Per-server Roblox integration config fetched from Supabase.
|
|
* Returns null if the server hasn't configured Roblox integration.
|
|
*/
|
|
|
|
async function getRobloxConfig(supabase, guildId) {
|
|
if (!supabase) return null;
|
|
const { data } = await supabase
|
|
.from('roblox_server_configs')
|
|
.select('universe_id, open_cloud_key, place_id, staff_role_ids')
|
|
.eq('guild_id', guildId)
|
|
.single();
|
|
return data || null;
|
|
}
|
|
|
|
async function setRobloxConfig(supabase, guildId, config) {
|
|
if (!supabase) return false;
|
|
const { error } = await supabase
|
|
.from('roblox_server_configs')
|
|
.upsert({ guild_id: guildId, ...config }, { onConflict: 'guild_id' });
|
|
return !error;
|
|
}
|
|
|
|
// Resolve Roblox user ID from username
|
|
async function resolveRobloxUser(username) {
|
|
try {
|
|
const res = await fetch('https://users.roblox.com/v1/usernames/users', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ usernames: [username], excludeBannedUsers: false }),
|
|
});
|
|
const data = await res.json();
|
|
return data?.data?.[0] || null; // { id, name, displayName }
|
|
} catch { return null; }
|
|
}
|
|
|
|
// Get Roblox group role for a user
|
|
async function getRobloxGroupRole(robloxUserId, groupId) {
|
|
try {
|
|
const res = await fetch(`https://groups.roblox.com/v2/users/${robloxUserId}/groups/roles`);
|
|
const data = await res.json();
|
|
const membership = data?.data?.find(g => g.group?.id === parseInt(groupId));
|
|
return membership?.role || null;
|
|
} catch { return null; }
|
|
}
|
|
|
|
module.exports = { getRobloxConfig, setRobloxConfig, resolveRobloxUser, getRobloxGroupRole };
|