discord.js 没有发送到自定义频道

Posted

技术标签:

【中文标题】discord.js 没有发送到自定义频道【英文标题】:discord.js not sending to custom channel 【发布时间】:2021-05-21 15:55:21 【问题描述】:

我做了一个命令,允许用户设置审核频道。设置频道命令起作用并将公会 ID 和频道 ID 发送到 JSON 文件中,如下所示:

"781631298749726730":"channel":"781719843770466355"

但在我的 unban 命令中,我似乎无法将其发送到在 moderationChannel.json 文件中创建的频道,它只是返回:

modChannel.send is not a function

这是unban.js文件中的代码:

let channels = JSON.parse(fs.readFileSync("././database/moderationChannel.json", "utf8"))
  
  let modChannel = channels[message.guild.id].channel;

    if(isNaN(args[0])) return message.channel.send(`$switchc You need to provide an ID.`)
    let bannedMember = await bot.user.fetch(args[0]) || message.guild.members.cache.get(args[0])
        if(!bannedMember) return message.channel.send(`$switchc Please provide a user id to unban someone!`)

    let reason = args.slice(1).join(" ")
        if(!reason) reason = `$switchc No reason given!`

    if(!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send(`$switchc I dont have permission to perform this command!`)|
    message.delete()
    try 
        message.guild.members.unban(bannedMember, reason)
        const bEmbed = new MessageEmbed()
.setColor("RANDOM")
.setDescription(`**$bannedMember.tag** has been unbanned!`)

    message.channel.send(bEmbed)
     catch(e) 
        console.log(e.message)
    
let mEmbed = new MessageEmbed()
  .setTitle(`Member Unbanned`)
  .setDescription("I WORK!")
  modChannel.send(mEmbed)
    

我使用了我的前缀命令中的频道代码,您可以在其中设置公会的前缀。我不知道为什么在某个公会设置的频道中发送消息不起作用。

如果您需要我们设置频道的代码,代码如下。

  let channels = JSON.parse(fs.readFileSync("././database/moderationChannel.json"));
  
  channels[message.guild.id] = 
    channel: message.channel.id
  
  
  fs.writeFile("././database/moderationChannel.json", JSON.stringify(channels), (err) => 
    if(err) console.log(err)
  );
  
  let sEmbed = new MessageEmbed()
  
  .setColor(aqua)
  .setTitle(`$message.guild.name Channel Set`)
  .setDescription(`Set to $message.channel.name`);
  message.channel.send(sEmbed);
  

【问题讨论】:

【参考方案1】:

问题在于modChannelmessage.channel.id,一个简单的字符串,是频道的ID,而不是拥有send() 方法的频道。

您可以将let modChannel = channels[message.guild.id].channel 替换为:

let channelId = channels[message.guild.id].channel;
let modChannel = client.channels.cache.get(channelId);

if (!modChannel) 
  return console.log(`No moderation channel found with ID $channelId`);

【讨论】:

以上是关于discord.js 没有发送到自定义频道的主要内容,如果未能解决你的问题,请参考以下文章

无法读取 discord.js 中未定义的属性“发送”

Discord.js 频道未定义欢迎消息

Discord.js票务系统错误发送未定义错误

使用 Discord.js 跨渠道发送消息会产生“未定义”错误

discord.js 发送自定义消息而不是镜像消息

Discord.js v12.0.1 频道未定义