【问题标题】:Username and args connecting? \\ Discord.js用户名和参数连接? \\ Discord.js
【发布时间】:2019-02-07 14:43:18
【问题描述】:

[text] 与[username] 结合使用(您可以将任何用户名放在“realdonaldtrump”所在的位置。之后的任何内容都是实际消息。

我已经尝试了很多东西,我唯一需要做的就是让 trump 成为默认推文并且无法更改它,所以它应该是 >tweet [message] 而不是 >tweet [username] [message] 但我想要拥有自定义用户名。有关如何从 [text] 中删除 [username] 的任何线索

这是代码

exports.exec = async (client, message, args, level, settings, texts) => {

    const user = args[0];

    // Fires Error message that the command wasn't ran correctly.
    if (!user) {
        return client.emit('commandUsage', message, this.help);
    }
    // Fires Error message that the command wasn't ran correctly.

    const text = args.join(" ");

    // Below is a self-deletion message prior to image sending when it's fetching the actual image.
    message.channel.send({
        embed: {
            color: 0,
            description: `${message.author} Generating image.`
        }
    }).then(msg => {
        msg.delete(5000).catch(() => { });
    }).catch(e => {
    });
    // Above is a self-deletion message prior to image sending when it's fetching the actual image.

    try {
        const { body } = await snekfetch.get(`https://nekobot.xyz/api/imagegen?type=${user.toLowerCase() === "realdonaldtrump" ? "trumptweet" : "tweet"}&username=${user.startsWith("@") ? user.slice(1) : user}&text=${encodeURIComponent(text)}`);
        message.channel.send("", { file: body.message });

        // Below is a automatic logger
    } catch (err) {
        const errorlogs = client.channels.get('480735959944527886')
        const embed = new discord.RichEmbed()
            .setAuthor("ERROR", "https://i.imgur.com/Omg7uJV.png")
            .setDescription(`${message.author} An error has occured using this command, this has automatically be logged and sent to ${client.channels.get('480735959944527886')} to be reviewed.`)
            .setColor(0)
            .setTimestamp()
        message.channel.send({ embed });
        errorlogs.send(`Error with \`$tweet\` command!\n\nError:\n\n ${err}`)
    }
    // Above is a automatic logger
};

【问题讨论】:

  • 你能告诉我们整个代码吗?您正在使用等待,但异步部分是什么?
  • 对此表示歉意,重新编辑代码块以包含除导出选项外的整个代码,因为它们都可以工作:)

标签: javascript bots discord


【解决方案1】:

您正在连接您的 args 以设置您的 text 变量

const text = args.join(" ");

但在您的示例中,args 的值是 ["realdonaltrump", "yeeet"],因此 text 的值是 "realdonaldtrump yeet"。

就像对 uservariable 所做的那样:

const text = args[1]

您可能需要验证参数的值。

【讨论】:

  • 哇。我花了一个多小时盯着这个,我在一个阶段有const text = args[0]。简直不敢相信就是这样……非常感谢
【解决方案2】:

您可以使用string.replace 方法,它将用任何内容替换用户名:

const text = args.join(" ").replace(new RegExp(`${user}`), "")

希望对你有帮助!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2012-08-27
    • 2021-08-25
    • 2017-10-30
    • 2021-08-15
    • 2019-09-03
    • 2016-11-18
    相关资源
    最近更新 更多