javascript - discord.js 没有发送到自定义频道
问题描述
我做了一个命令,允许用户设置审核频道。set channel 命令起作用并将公会 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);
}
}
解决方案
问题是modChannel
,message.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}`);
}
推荐阅读
- angular - Amazon Build 失败并出现构建命令需要在 Angular 项目中运行,但找不到项目定义
- amazon-web-services - 是否可以将标签添加到使用 Packer 创建的包含过滤后的 source_ami 名称的 AWS AMI?
- javascript - jQuery:随机放置的对象应始终完全保留在父对象内
- google-apps-script - 如何使单个脚本在同一工作表中的多个选项卡上工作
- reactjs - 使用 react、express、node mongodb/postgress 实时更改
- html - 圆形边框CSS上的渐变
- firebase - 使用 nvm 在 IntelliJ IDE 中运行 firebase 命令/脚本
- php - 构造 XML 数据时如何使用 FOREACH?
- typescript - Typescript ChildNode:`parentElement`和/或`parentNode`为空?
- express - 将 mqtt 订阅绑定到节点 ejs 和 alpine 状态