fix createinvite

This commit is contained in:
kmmiio99o 2025-10-07 12:32:13 +02:00
parent e8793f437e
commit e51bf9e007

View file

@ -25,30 +25,77 @@ export const friendInviteCreateCommand = {
send(ctx, "You need to have a phone number connected to your account to create a friend invite!");
return { type: 4 };
}
const uuid = uuidv4();
const res = await api.post({
url: "/friend-finder/find-friends",
body: { modified_contacts: { [uuid]: [1, "", ""] }, phone_contact_methods_count: 1 },
});
const code = res.body?.invite_suggestions?.[0]?.[3];
if (!code) {
send(ctx, "Failed to generate invite code.");
return { type: 4 };
// Try the primary method first
try {
const uuid = uuidv4();
const res = await api.post({
url: "/friend-finder/find-friends",
body: { modified_contacts: { [uuid]: [1, "", ""] }, phone_contact_methods_count: 1 },
});
// Check if the Find Friends feature is available
if (res.status === 503 || res.body?.code === 40077) {
throw new Error("Find Friends temporarily unavailable");
}
const code = res.body?.invite_suggestions?.[0]?.[3];
if (!code) {
throw new Error("No invite code generated");
}
const createInvite = await inviteModule.createFriendInvite({
code,
recipient_phone_number_or_email: uuid,
contact_visibility: 1,
filter_visibilities: [],
filtered_invite_suggestions_index: 1,
});
const expires = Math.floor(new Date(createInvite.expires_at).getTime() / 1000);
const message = `https://discord.gg/${createInvite.code} · Expires: <t:${expires}:R>`;
send(ctx, message);
} catch (primaryError) {
console.error("[FriendInvite] Primary method failed:", primaryError);
// Fallback method: Try to create invite directly without Find Friends
send(ctx, "⚠️ Find Friends is temporarily unavailable. Trying alternative method...");
try {
// Alternative: Create a friend invite using a different endpoint
const fallbackInvite = await inviteModule.createFriendInvite({
code: null, // Let Discord generate the code
recipient_phone_number_or_email: null,
contact_visibility: 0,
filter_visibilities: [],
filtered_invite_suggestions_index: 0,
});
if (fallbackInvite?.code) {
const expires = Math.floor(new Date(fallbackInvite.expires_at).getTime() / 1000);
const message = `https://discord.gg/${fallbackInvite.code} · Expires: <t:${expires}:R>`;
send(ctx, `✅ Fallback method successful!\n${message}`);
} else {
throw new Error("Fallback method also failed");
}
} catch (fallbackError) {
console.error("[FriendInvite] Fallback method failed:", fallbackError);
send(ctx, "❌ Both methods failed. The Find Friends feature is currently unavailable. Please try again later or use Discord's built-in friend invite system.");
}
}
const createInvite = await inviteModule.createFriendInvite({
code,
recipient_phone_number_or_email: uuid,
contact_visibility: 1,
filter_visibilities: [],
filtered_invite_suggestions_index: 1,
});
const expires = Math.floor(new Date(createInvite.expires_at).getTime() / 1000);
const message = `https://discord.gg/${createInvite.code} · Expires: <t:${expires}:R>`;
send(ctx, message);
return { type: 4 };
} catch (e) {
send(ctx, "Error creating invite.");
console.error("[FriendInvite] create error:", e);
console.error("[FriendInvite] Create error:", e);
if (e?.body?.code === 40077 || e?.message?.includes("temporarily unavailable")) {
send(ctx, "❌ The Find Friends feature is temporarily unavailable. Please try again later or use Discord's built-in friend invite system.");
} else {
send(ctx, "❌ Error creating friend invite. Please try again later.");
}
return { type: 4 };
}
},
@ -68,15 +115,17 @@ export const friendInviteViewCommand = {
send(ctx, "You have no active friend invites!");
return { type: 4 };
}
const friendInviteList = invites.map(i => {
const expires = Math.floor(new Date(i.expires_at).getTime() / 1000);
return `_https://discord.gg/${i.code}_ · Expires: <t:${expires}:R> · Uses: \`${i.uses}/${i.max_uses}\``;
});
send(ctx, friendInviteList.join("\n"));
send(ctx, `**Your Active Friend Invites:**\n${friendInviteList.join("\n")}`);
return { type: 4 };
} catch (e) {
send(ctx, "Error viewing invites.");
console.error("[FriendInvite] view error:", e);
console.error("[FriendInvite] View error:", e);
send(ctx, "❌ Error viewing your friend invites. Please try again later.");
return { type: 4 };
}
},
@ -92,12 +141,28 @@ export const friendInviteRevokeCommand = {
inputType: 1,
execute: async (_, ctx) => {
try {
const invitesBefore = await inviteModule.getAllFriendInvites();
if (!invitesBefore?.length) {
send(ctx, "You have no active friend invites to revoke!");
return { type: 4 };
}
await inviteModule.revokeFriendInvites();
send(ctx, "All friend invites have been revoked.");
// Verify revocation worked
const invitesAfter = await inviteModule.getAllFriendInvites();
if (invitesAfter.length === 0) {
send(ctx, `✅ Successfully revoked all ${invitesBefore.length} friend invite(s)!`);
} else {
send(ctx, `⚠️ Partially revoked invites. ${invitesAfter.length} invite(s) remain active.`);
}
return { type: 4 };
} catch (e) {
send(ctx, "Error revoking invites.");
console.error("[FriendInvite] revoke error:", e);
console.error("[FriendInvite] Revoke error:", e);
send(ctx, "❌ Error revoking friend invites. Please try again later.");
return { type: 4 };
}
},