【问题标题】:how to separate words in arguments (discord.js)如何分隔参数中的单词(discord.js)
【发布时间】:2023-03-20 21:45:01
【问题描述】:
client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(' ');
    const command = args.shift().toLowerCase();

    if (command === 'say') {
        if (!args.length) {
            return message.channel.send(`Please tell the bot what to say, ${message.author}`);
        }
    
        const { Client, MessageEmbed } = require('discord.js');
        const embed = new MessageEmbed()
            .setTitle(`${args}`)
            .setColor('RED')
        message.channel.send(embed);
    }
})

但每当我输入!say subscribe today 时,它就会显示为subscribe,today 有人可以告诉我一种分隔论点的方法,以便逗号不存在并且它不止一个词吗?

【问题讨论】:

  • args 是一个字符串数组,您可以使用例如 text = args.join(' '); 从它创建一个字符串
  • 我会把这个放在哪里?

标签: javascript node.js arrays discord discord.js


【解决方案1】:
if (command === 'say') {
        if (!args.length) {
            return message.channel.send(`Please tell the bot what to say, ${message.author}`);
        }
        let text = args.join(' '); //Join the array of strings with a space to create a text to send
        const { Client, MessageEmbed } = require('discord.js');
        const embed = new MessageEmbed()
            .setTitle(text)
            .setColor('RED')
        message.channel.send(embed);
    }

更多关于 array.join() 方法here

【讨论】:

    【解决方案2】:
    const Discord = require('discord.js');
    const client = new Discord.Client();
    const prefix = '!';
    client.on('message', (message) => {
        let args = message.content.substring(0, prefix.length).split(' ');
        let command = args.shift();
    });
    

    【讨论】:

    • 我很困惑,我不明白这有什么帮助
    • args 它的命令参数,命令它的第一个书面世界
    猜你喜欢
    • 2021-10-04
    • 2021-02-06
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多