NodeJS 如何让伙伴关系有价值?

blmhpbnm  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(81)

这其实是我的代码

const inviteLink = await message.content.match(/discord\.(gg|com)\/.+|discordapp\.(gg|com)\/.+|discordapp\.(gg|com)\/invite\/.+/)?.[0];

if (inviteLink) {
    setTimeout(async () => {
        try {
            let inviteL
            if (inviteLink.includes("discord.gg/") || inviteLink.includes("discord.com/")) {
                inviteL = inviteLink.slice(11)

            } else if (inviteLink.includes("discord.gg/invite") || inviteLink.includes("discord.com/invite")) { // && inviteLink.includes("discord.com/invite")) {
                inviteL = inviteLink.slice(18)

            } else if (inviteLink.includes("discordapp.gg/") || inviteLink.includes("discordapp.com/")) {
                inviteL = inviteLink.slice(14)

            } else if (inviteLink.includes("discordapp.gg/invite") || inviteLink.includes("discordapp.com/invite")) {
                inviteL = inviteLink.slice(21)

            } else {
                console.log("invalid")
            }

            console.log(`inviteLink: ${inviteLink} | inviteL: ${inviteL} `)

            const fetch = require('node-fetch');
            const response = await fetch(`https://discordapp.com/api/invites/${inviteL}`,);
            const dataLink = await response.json();

            if (dataLink.message === 'Unknown Invite') {
                return
            }

            await client.fetchInvite(dataLink.code)
                .then(async (invite) => {

                    // Code to add 1 to the database (Ignore that)

                    const embed = new Discord.MessageEmbed()
                        .setAuthor({ name: `${message.author.tag} | ${message.author.id}`, iconURL: message.author.displayAvatarURL({ size: 512, dynamic: true, format: 'png' }) })
                        .setDescription(message.translate(`${data.config.cat.b5}/partnercount:SETDESCRIPTION`, {
                            guild: `${invite.guild.name} | ${invite.guild.id}`,
                            amount: user.partnerInfo.partnersCount
                        }))

                    return message.reply({ embeds: [embed] })

                })
        } catch (err) {
            return
        }
    }, 500)

}

我希望它与Luminous bot中的一样
目前,bot处理以discord.gg/.com或discordapp.gg/.com等开头的所有内容。
我只想让它只接受代码,这样它就不会继续接受一行或多行代码
示例:discord.gg/codecode dasdsad(只进行编解码,不继续)

xam8gpfp

xam8gpfp1#

答案是

if (/(discord\.gg\/.+|discord\.com\/invite\/.+|discordapp\.com\/invite\/.+)/i.test(message.content)) {

    const inviteLink = await message.content.match(/discord\.gg\/.+|discord\.com\/invite\/.+|discordapp\.com\/invite\/.+/)?.[0];

    let userCode
    if (message.content.includes("discord.gg/")) {
        userCode = message.content.split("discord.gg/")[1].split(" ")[0].split("\n")[0];

    } else if (message.content.includes("discord.com/invite/")) {
        userCode = message.content.split("discord.com/invite/")[1].split(" ")[0].split("\n")[0];

    } else if (message.content.includes("discordapp.com/invite/")) {
        userCode = message.content.split("discordapp.com/invite/")[1].split(" ")[0].split("\n")[0];
    };

    if (inviteLink) {
        try {
            await client.fetchInvite(userCode)
                .then(async (invite) => {
                    // code
                }).catch(() => {
                    // error
                });

        } catch (err) {
            // error
        };
    };
};

相关问题