javascript - 参数在第一个单词后被切片 | Discord.js

标签 javascript bots discord

enter image description here

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

    const user = args[0];
    const text = args[1]

    // 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.

    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 });

/* * * * */

正如您在 gif 中看到的,在这种情况下,第一个单词 (it) 之后的任何内容都会被切片。我不知道为什么,我不确定这是否是由于 const text = args[1] 引起的。

我认为 const text = args.join ("") 根本不起作用,我也没有尝试过,但相当肯定它不会起作用。

抱歉,我是不熟悉的新手,我是discord.js的新手:/

最佳答案

这肯定是由于 args[1] 造成的,假设 args 是消息中使用的单词数组,按空格分隔,1 位置将始终只包含第二个单词。

你想要的是这样的:

const [user, ...restArgs] = args;
const text = restArgs.join(' ');

这会将第一个元素作为user,并使用数组的其余部分作为文本,而不仅仅是第二个元素。

关于javascript - 参数在第一个单词后被切片 | Discord.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52687265/

相关文章:

javascript - 提取网页的一部分?

javascript - 从组件内更改 vue 实例变量

python - 如何通过单击同一个按钮来给予和接受角色?

javascript - 过滤器后reduce的目的以及为什么过滤器内部调用map?

javascript - 从 Iframe 创建一个新的 chrome 选项卡/窗口

python - 带有 python : xmpp. 协议(protocol)的 xmpp.InvalidFrom : (u'invalid-from', '' )

node.js - Telegram 机器人 - telegraf vs telebot

c# - 如何将用户的消息存储在表存储中? (微软机器人框架 SDK4 和 C#)

Java Discord Api 移动用户

javascript - 如何检查和打印不一致的时间戳 react ?