首页 > 解决方案 > guild.createChannel 不起作用,我不知道为什么

问题描述

我已经尝试了一段时间,但我无法弄清楚这个。所以我想做的是使用机器人创建一个频道,这样一个人就可以打开一张票,基本上只有用户和特定角色可以查看和写入。我还没有完成第二部分,但这已经够糟糕了。

这是我可怜的尝试:

         case 'report':
            guild.createChannel('ticket-' + makeid, {
                type: 'text',
                permissionOverwrites: [{
                    id: guild.id,
                    deny: ['MANAGE_MESSAGES'],
                    allow: ['SEND_MESSAGES']
                }]
            })
                .then(console.log)
                .catch(console.error);
            break;

在这种情况下,makeid是由函数生成的随机字符串。错误是:

ReferenceError: guild is not defined

标签: discord.js

解决方案


您需要定义变量guild。有多种方法可以做到这一点Discord.js,所以这里它们是:

使用用户消息:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', (message) => {
  let guild = message.guild;  //Get guild from the message
});

或者通过使用 GuildID:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on("ready", () => {
  let guild = client.guilds.get("GUILD-ID");  //Replace "GUILD-ID" with the id of the guild you want
});

推荐阅读