简体   繁体   English

需要帮助使用 discord bot 命令分配变量

[英]Need help for assigning a variable with discord bot command

so I am trying to build my own discord bot with discord.js.所以我试图用 discord.js 构建我自己的不和谐机器人。 There is one really important thing that I want to achieve.我想实现一件非常重要的事情。 I want to reassign a variable through a bot command.我想通过机器人命令重新分配一个变量。 My current code looks like this:我当前的代码如下所示:

client.on("message", (msg) => {
  if (msg.content.startsWith(config.prefix + "boosting")) {
    const embed = new Discord.MessageEmbed()
      .setColor("0xd08d11")
      .setTitle("**Random Title**")
      .setDescription(Some description)
      .addFields(
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: "Person 0", value: 'Personvalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Price', value: 'Pricevalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Person 1', value: '40k', inline: true },
        { name: 'Person 2', value: '40k', inline: true },
        { name: 'Person 3', value: '40k', inline: true },
        { name: 'Person 4', value: '40k', inline: true },
      )
      .setThumbnail(logo)
      .setTimestamp()
      .setFooter('created by me or something like that');
    client.channels.cache.get(`here_is_my_channel_id`).send(embed); 
  }
});

client.login(config.token)

Okay, so right now I can type .boosting to get an embed message from my bot, but the thing is, that i want to type .boosting Variable1 Variable2 and so on to give new values to the attributes of the embed message.好的,现在我可以输入 .boosting 来从我的机器人获取嵌入消息,但问题是,我想输入 .boosting Variable1 Variable2 等等来为嵌入消息的属性提供新值。 And with attributes I mean something like the description or the fields name or values.对于属性,我的意思是诸如描述或字段名称或值之类的东西。 I tried something like this:我试过这样的事情:

let Variable = "Test";

client.on("message", (msg) => {
  if (msg.content.startsWith(config.prefix + "boosting" + " " + Variable)) {
    const embed = new Discord.MessageEmbed()
      .setColor("0xd08d11")
      .setTitle("**Random Title**")
      .setDescription(Variable)
      .addFields(
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: "Person 0", value: 'Personvalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Price', value: 'Pricevalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Person 1', value: '40k', inline: true },
        { name: 'Person 2', value: '40k', inline: true },
        { name: 'Person 3', value: '40k', inline: true },
        { name: 'Person 4', value: '40k', inline: true },
      )
      .setThumbnail(logo)
      .setTimestamp()
      .setFooter('created by me or something like that');
    client.channels.cache.get(`here_is_my_channel_id`).send(embed); 
  }
});

client.login(config.token)

Now I can write .boosting Test and the description of this embed message will have this as it's value.现在我可以编写 .boosting Test 并且这个嵌入消息的描述将作为它的价值。 I know that I need to define the variable, but is there any possibility to reassign it with a bot command?我知道我需要定义变量,但是有没有可能用 bot 命令重新分配它? So it can change from the value "Test" to "something" for example.例如,它可以从值“Test”更改为“something”。 Appreciate every help!感谢每一个帮助!

You can split your the message into an array of words:您可以将消息拆分为一组单词:

const args = message.content.slice(config.prefix.length).split(/ +/).slice(1)

// example: `.boosting hello people of the world`
// returns: ['hello', 'people', 'of', 'the', 'world']

So, if you'd like to get the first argument (word), you can use args[0] .因此,如果您想获得第一个参数(单词),可以使用args[0] For the second, args[1] , and so on.对于第二个, args[1] ,依此类推。

You updated code:您更新了代码:

client.on("message", (msg) => {
  if (msg.content.startsWith(config.prefix + "boosting" + " " + Variable)) {
    const args = message.content.slice(config.prefix.length).split(/ +/).slice(1)
    const embed = new Discord.MessageEmbed()
      .setColor("0xd08d11")
      .setTitle("**Random Title**")
      .setDescription(args[0])
      .addFields(
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: "Person 0", value: 'Personvalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Price', value: 'Pricevalue', inline: true },
        { name: '\u200B', value: '\u200B' }, //spacer
        { name: 'Person 1', value: '40k', inline: true },
        { name: 'Person 2', value: '40k', inline: true },
        { name: 'Person 3', value: '40k', inline: true },
        { name: 'Person 4', value: '40k', inline: true },
      )
      .setThumbnail(logo)
      .setTimestamp()
      .setFooter('created by me or something like that');
    client.channels.cache.get(`here_is_my_channel_id`).send(embed); 
  }
});

client.login(config.token)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM