問題描述
Discord.js ReferenceError:嵌入未定義 (Discord.js ReferenceError: embed is not defined)
我是編碼新手,所以我決定製作一個從 Reddit 發送模因的不和諧機器人。但是每當我發送命令時,它都會給我錯誤並使機器人崩潰。我做錯了什麼以及如何解決它
這是所有不和諧機器人
const Discord = require('discord.js');
const client = new Discord.Client();
const randomPuppy = require('random‑puppy');
const prefix = '%'
client.once('ready', () => {
console.log('CounterOutplayer is READY!');
});
module.exports = {
name: "meme",
description: "Random image from a specific subreddit",
async run (client, message, args){
const subReddits = ["memes", "dankmemes"]
const random = subReddits[Math.floor(Math.random() * subReddits.length)]
const img = await randomPuppy(random);
const embed = new Discord.MessageEmbed()
.setColor("Green")
.setImage(img)
.setTitle(`memes ‑ ${random}`)
.setURL(`https://reddit.com/r/${random}`)
message.channel.send(embed);
}
}
client.login('token')
參考解法
方法 1:
If you're sending just a string, you can just use message.channel.send('TEXT')
. For an embed, that belongs in the "options" parameter of the .send()
function; so, you'll have to send it as an object. In other words, use message.channel.send({ embed: embed })
.
方法 2:
I believe .setColor("Green")
is making the embed go undefined. According to the discord.js docs, color options are either a number, a hex code like #00ff00
, an RGB array like [0, 255, 0]
, or a color string. You have "Green" in your code, but the docs list out valid colors and all of them are caps. .setColor("GREEN")
should work just fine.
(by WalkingHero、Chiitoi、Colin Hanes)