Fix FriendInvites (#802)

Co-authored-by: Vendicated <vendicated@riseup.net>
This commit is contained in:
Dziurwa 2023-04-05 23:01:11 +02:00 committed by GitHub
parent 814302e272
commit c9fd404012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 14 deletions

@ -19,12 +19,16 @@
import { ApplicationCommandInputType, sendBotMessage } from "@api/Commands";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { findByProps } from "@webpack";
import { findByPropsLazy } from "@webpack";
import { RestAPI, UserStore } from "@webpack/common";
const FriendInvites = findByPropsLazy("createFriendInvite");
const uuid = findByPropsLazy("v4", "v1");
export default definePlugin({
name: "FriendInvites",
description: "Create and manage friend invite links via slash commands (/create friend invite, /view friend invites, /revoke friend invites).",
authors: [Devs.afn],
authors: [Devs.afn, Devs.Dziurwa],
dependencies: ["CommandsAPI"],
commands: [
{
@ -32,14 +36,31 @@ export default definePlugin({
description: "Generates a friend invite link.",
inputType: ApplicationCommandInputType.BOT,
execute: async (_, ctx) => {
const friendInvites = findByProps("createFriendInvite");
const createInvite = await friendInvites.createFriendInvite();
if (!UserStore.getCurrentUser().phone)
return sendBotMessage(ctx.channel.id, {
content: "You need to have a phone number connected to your account to create a friend invite!"
});
return void sendBotMessage(ctx.channel.id, {
const random = uuid.v4();
const invite = await RestAPI.post({
url: "/friend-finder/find-friends",
body: {
modified_contacts: {
[random]: [1, "", ""]
}
}
}).then(res =>
FriendInvites.createFriendInvite({
code: res.body.invite_suggestions[0][3],
recipient_phone_number_or_email: random
})
);
sendBotMessage(ctx.channel.id, {
content: `
discord.gg/${createInvite.code} ·
Expires: <t:${new Date(createInvite.expires_at).getTime() / 1000}:R> ·
Max uses: \`${createInvite.max_uses}\`
discord.gg/${invite.code} ·
Expires: <t:${new Date(invite.expires_at).getTime() / 1000}:R> ·
Max uses: \`${invite.max_uses}\`
`.trim().replace(/\s+/g, " ")
});
},
@ -49,15 +70,16 @@ export default definePlugin({
description: "View a list of all generated friend invites.",
inputType: ApplicationCommandInputType.BOT,
execute: async (_, ctx) => {
const friendInvites = findByProps("createFriendInvite");
const invites = await friendInvites.getAllFriendInvites();
const invites = await FriendInvites.getAllFriendInvites();
const friendInviteList = invites.map(i =>
`_discord.gg/${i.code}_ ·
`
_discord.gg/${i.code}_ ·
Expires: <t:${new Date(i.expires_at).getTime() / 1000}:R> ·
Times used: \`${i.uses}/${i.max_uses}\``.trim().replace(/\s+/g, " ")
Times used: \`${i.uses}/${i.max_uses}\`
`.trim().replace(/\s+/g, " ")
);
return void sendBotMessage(ctx.channel.id, {
sendBotMessage(ctx.channel.id, {
content: friendInviteList.join("\n") || "You have no active friend invites!"
});
},
@ -67,7 +89,7 @@ export default definePlugin({
description: "Revokes all generated friend invites.",
inputType: ApplicationCommandInputType.BOT,
execute: async (_, ctx) => {
await findByProps("createFriendInvite").revokeFriendInvites();
await FriendInvites.revokeFriendInvites();
return void sendBotMessage(ctx.channel.id, {
content: "All friend invites have been revoked."

@ -241,5 +241,9 @@ export const Devs = /* #__PURE__*/ Object.freeze({
skyevg: {
name: "skyevg",
id: 1090310844283363348n
},
Dziurwa: {
name: "Dziurwa",
id: 787017887877169173n
}
});