首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取通道,然后创建invite

获取通道,然后创建invite
EN

Stack Overflow用户
提问于 2020-06-09 00:27:24
回答 1查看 718关注 0票数 0
代码语言:javascript
复制
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}')))
    }
}

错误:

代码语言:javascript
复制
'TypeError: Cannot read property 'channels' of undefined'

所以我试着找到频道(痛苦日志),一旦找到频道,它就会创建一个对频道的邀请,但我一直收到这个错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-09 02:23:12

client.guild不存在,而client.guilds确实存在,所以如果您想从特定的行会获取特定的通道,您必须这样做:

代码语言:javascript
复制
//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

代码语言:javascript
复制
//might need to fetch it if it's not in the cache
const channel = client.channels.find(c => c.name === "pain-logs");

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

代码语言:javascript
复制
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");
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62266774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档