多个 args.split().join(" ") discord.js

Multiple args.split().join(" ") discord.js

所以我正在使用 Discord.js 制作一个不和谐的机器人。在机器人中,有一个命令,您可以在其中执行赠品 ping,机器人将发送带有赞助商、奖品和消息的嵌入。但是我需要所有这些不同的参数才能具有 args.slice().join(" ").


    const Discord = require('discord.js');
    module.exports = {
        name: 'giveaway',
        description: 'giveaway embed',
        execute(message, args) {

            if(!message.member.hasPermission("MENTION_EVERYONE")) return message.channel.send('You do not have permission to do this!');
        
            let sponsor = args[0];
            if(!sponsor) {
                sponsor = 'Me';
            }

            let prize = args.slice(1).split('/').join(" ");
            if(!prize) return message.reply('You need to put a prize for the giveaway! `-giveaway <sponsor> <prize> <message>`');

            let userMessage = args.slice(2).join(' ')
            if(!userMessage) {
                userMessage = 'none';
            }
                const giveawayPingEmbed = new Discord.MessageEmbed()
                .setTitle(':tada: GIVEAWAY :tada:')
                .setDescription(`Sponsor: ${sponsor} \nPrize: ${prize} \nMessage: ${userMessage}`)

                
            
        },
    };

您可以使用迭代消息收集器来填写表单 - 因为在您的情况下很难决定何时停止一条消息而另一条消息开始。

let input = []; // declare input as empty array
let setupQ = ['Sponsor:', 'Prize', 'Message'];

async function giveaway(){
   for (i = 0; i < 3; i++) {
     message.channel.send(setupQ[i]);   

     const filter = (user) => {
         return user.author.id === message.author.id //only collects messages from the user who sent the command
     };

     try{
     let collected = await message.channel.awaitMessages(filter, { max: 1, time: 15000, errors: ['time'] });
        input[i] = collected.first().content; //takes user input and saves it
      }
     catch(e)
      {
         return message.channel.send(`:x: Setup cancelled - 0 messages were collected in the time limit, please try again`).then(m => m.delete({ timeout: 4000 }));
      };              
   };
};

giveaway(); //calls the async function to start the giveaway form

此代码将递增地发送 inputQ 个问题,并将响应保存到 input 数组。如果你不熟悉async/await,请看here

请查看消息收集器语法和函数here

正如你所要求的另一种方式,这里是:

var X = args.join(" ") //X is your message arguments - change it depending on how you run them
if (!X) return message.channel.send("Error - No arguments");

var sponsor = X.replace(new RegExp('.*' + "sponsor:"), ''); //uses regex to remove text after the word "sponsor:"
var prize = sponsor.replace(new RegExp('.*' + "prize:"), '');
var messageUser = sponsor.replace(new RegExp('.*' + "message:"), '');
sponsor = sponsor.replace(prize, "").replace("prize:", "")
prize = prize.replace(messageUser, "").replace("message:", "") //removes parts you dont want

message.channel.send(sponsor);
message.channel.send(prize);
message.channel.send(messageUser);

请注意我没有输入验证

语法必须是 <prefix><command name> sponsor: <sponsor name> prize: <prize> message: <message>

例如:

!giveaway sponsor: @user prize: epic prize message: epic message