[英]How do i send a message in a specific channel in discord using discord.js
我试过用
<client>.channels.cache.get('1234567890').send('Hello world.');
和
<guild>.channels.cache.find(ch => ch.name === 'general').send('Hello world.');
但都没有奏效。 这是我拥有的代码:
module.exports = {
commands: 'strike',
minArgs: 2,
expectedArgs: "<Target user's @> <Role>",
permissions: 'MANAGE_ROLES',
callback: (message, args, client) => {
const targetUser = message.mentions.users.first()
if (!targetUser) {
message.reply('Please specify someone to strike.')
return
}
args.shift()
const roleName = args.join(' ')
const { guild } = message
const role = guild.roles.cache.find((role) => {
return role.name === roleName
})
if (!role) {
message.reply(`There is no role with the name "${roleName}"`)
return
}
const member = guild.members.cache.get(targetUser.id)
member.roles.add(role)
client.channels.cache.get('870165929282138153').send(`<@${member.user.id}> has been striked.`)
},
}
关于我如何做到这一点的任何想法?
要使用其 id 向特定频道发送消息,您可以使用:
message.guild.channels.cache.get(ID).send(MESSAGE);
channels
集合来自guild
对象, .cache.get()
返回一个频道对象。
你可以在这里了解更多信息: https : //discord.js.org/#/docs/main/stable/class/Guild?scrollTo=channels
按名称查找频道:
const channel = client.channels.cache.get(channel => channel.name === "channelName")
channel.send(message)
通过 ID 查找频道:
const channel = client.channels.cache.get(channel => channel.id === "ID")
channel.send(message)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.