简体   繁体   中英

How to display or use variables in Discord.RichEmbed()

I am not able to include variables within a richembed on my Discord bot.

I'm an absolute noob to Javascript and Discord bots. I have tried putting the code within the richembed (which doesn't work at all..) and placing the embeds at the bottom of the javascript file, amongst other things.

  // Get information on models
  if (msg.content.startsWith('!info')) {
    // Get first (category) argument and 2nd (model name) category
    const args = msg.content.slice('!info'.length).split(' ');
    const command = args.shift().toLowerCase();
    // Set arguments as variables
    const cat = args[0]
    const model = args[1]
    // Check if the model exists in the category
    if (fs.existsSync(dirPath + cat + "/" + model)) {
      const modelInfo = fs.readFileSync(dirPath + cat + "/" +model, "utf8");
      msg.channel.send(embedModelInfo);
    } else {
      // If it doesn't, display an error
      msg.channel.send('ERROR: ' + cat + ' is not a valid category and/or model. Run "!listcat" for a list of categories and to list models within said categories.');
    }
    msg.channel.send('_ _');
    msg.channel.send('READY.');
  }
var embedModelInfo = new Discord.RichEmbed()
  .setColor('777575')
  .setTitle('Model Information')
  .setAuthor('Printbot', 'https://raw.githubusercontent.com/sparrdrem/printbot/master/_previmg.png')
  .setThumbnail('https://raw.githubusercontent.com/sparrdrem/printbot/master/_previmg.png')
  .addField(modelInfo, '_ _')
  .addField('_ _', '_ _')
  .addField('READY.', '_ _')

I'd expect the embedded page to print the modelInfo variable, however Node.JS crashes saying "modelInfo is not defined", even though it is defined as shown above.

The embedModelInfo definition needs to go in between the modelInfo definition and the message.channel.send(embedModelInfo)

if (fs.existsSync(dirPath + cat + "/" + model)) {
  const modelInfo = fs.readFileSync(dirPath + cat + "/" +model, "utf8");
  var embedModelInfo = new Discord.RichEmbed()
    .setColor('777575')
    .setTitle('Model Information')
    .setAuthor('Printbot', 'https://raw.githubusercontent.com/sparrdrem/printbot/master/_previmg.png')
   .setThumbnail('https://raw.githubusercontent.com/sparrdrem/printbot/master/_previmg.png')
    .addField(modelInfo, '_ _')
    .addField('_ _', '_ _')
    .addField('READY.', '_ _');
  msg.channel.send(embedModelInfo);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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