Discord.js将位置设置为文本通道

时间:2020-08-31 19:17:34

标签: discord.js

discord.js出现问题:我想为频道设置位置:

const chanName = message.channel.name;
    let categoryId = message.channel.parentID
    let position = message.channel.position
    console.dir(position)
    const catego = message.guild.channels.cache.find(c => c.id == categoryId && c.type == "category")
    message.channel.delete().catch();
    
    const chan = message.guild.channels.create(chanName, {type: 'text'}).then((channel) => channel.setParent(catego) && channel.setPosition(position) && channel.send(`Succesfully nuked \`${chanName}\`\nhttps://imgur.com/LIyGeCR`))
    chan

但是当我执行命令时,通道未设置在该位置。

我还想保存将其放回去的权限

1 个答案:

答案 0 :(得分:0)

问题出在then回调中:

channel.setParent(catego) && channel.setPosition(position) && channel.send(`Succesfully nuked \`${chanName}\`\nhttps://imgur.com/LIyGeCR`)

&&是逻辑AND运算符。 channels.setParent(catego)返回一个Promise,这是正确的,因为它是一个对象。由于&&短路,因此它不求值其他表达式channel.setPosition(...)channel.send(...),因为该表达式无论如何都会求值到该Promise

您想改为执行多个语句:

const chan = message.guild.channels.create(chanName, {type: 'text'}).then(channel => {
  channel.setParent(catego)
  channel.setPosition(position)
  channel.send(`Succesfully nuked \`${chanName}\`\nhttps://imgur.com/LIyGeCR`)
})

请务必记住,channel => ...只是普通的arrow function,并且以下各项都是等效的:

const f1 = channel => channel.setParent(catego)
const f2 = channel => {
  return channel.setParent(catego)
}
const f3 = function (channel) {
  return channel.setParent(catego)
}
function f4(channel) {
  return channel.setParent(catego)
}

但是,在这种情况下,您可以在创建通道时设置其父级和位置:

const chan = message.guild.channels.create(chanName, {
  type: 'text',
  parent: catego,
  position
})
  .then(channel =>
    channel.send(`Succesfully nuked \`${chanName}\`\nhttps://imgur.com/LIyGeCR`)
  )

您可以在the documentation for GuildChannelManager#create中看到其他选项。