试图从消息中获取 channelId

时间:2021-04-18 19:58:14

标签: javascript discord.js

我正在尝试从 channelId 获取 message 但我的这行代码

if (msg.guild.channels.cache.get(channelid) === suggestionsChannelId && !author.bot)

抛出这个错误:

<块引用>

ReferenceError: channelid 未定义

我的代码如下:

const Discord = require("discord.js") 

const { suggestionsChannelId, suggestionsWebhookUrl } = require(__dirname + "/../config/servers/818776716218335262.json")

const webhookClient = new Discord.WebhookClient(suggestionsWebhookUrl);

module.exports = {
    name: "message",

    run(msg) {
        const { author } = msg

        const embed = new Discord.MessageEmbed()
            .setDescription(msg)

        if (msg.guild.channels.cache.get(channelId) === suggestionsChannelId && !author.bot) {
            webhookClient.send({
                username: author.username,
                avatarURL: author.displayAvatarURL(),
                embeds: [embed],
            })
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为您从未定义 variable channelid。您可以像这样从 channel ID 获取 message

const channelId = msg.channel.id

现在您可以获得实际的GuildChannel

const channel = msg.guild.channels.cache.get(channelId)