获取频道,然后创建邀请

时间:2020-06-08 16:27:24

标签: javascript node.js discord.js

echo 'Welcome To Here' | tr '[:space:]' '\t'

错误:

if (command === 'guilds') {
    if (message.author.id === ownerID) {
        let guilding = client.guild.channels.cache.find(channel => channel.name === "pain-logs")
        if (!guild.me.hasPermission("CREATE_INSTANT_INVITE")) {
            return message.channel.send(
                'I cannot Create Invites for ${guild.me.name}.'
            );
        }
        if (!guild.me.hasPermission("VIEW_CHANNEL")) {
            return message.channel.send(
                'I Cannot View Channels In ${guild.me.name}'
            );
        }
        guilding.createInvite().then(inv => console.log('${guild.name} | ${inv.url}', message.channel.send('${guild.name} | ${inv.url}')))
    }
}

因此,我尝试查找频道(痛苦日志),然后一旦找到频道,它将创建该频道的邀请,但我一直收到该错误

1 个答案:

答案 0 :(得分:0)

client.guild不存在,client.guilds确实存在,因此,如果您想从特定的公会获得特定的频道,则必须这样做:

//or some other way of getting it like `.get(id)`
const guild = client.guilds.cache.find(g => g.name === "name");
if(!guild || !guild.available) return message.channel.send("Can't find guild");
const channel = guild.channels.cache.find(c => c.name === "pain-logs");

如果您没有该漫游器所在的其他服务器,或者您不在乎该机器人来自哪个服务器,则可以使用client.channels

//might need to fetch it if it's not in the cache
const channel = client.channels.find(c => c.name === "pain-logs");

之后,只需检查频道是否可见并创建

if(!channel) return message.channel.send("No channel found");
if(!channel.viewable) return message.channel.send("Can't view the channel");
channel.createInvite()
.then(inv => {
  //guild.name will only work if you used the first method
  console.log(`${guild.name} | ${inv.url}`);
  message.channel.send(`${guild.name} | ${inv.url}`);
})
.catch(err => {
    console.error(err);
    message.channel.send("Don't have permission");
});