首页 > 解决方案 > 我正在尝试创建一个名为 user args 的频道。[DISCORD.JS V12]

问题描述

它只是给我一个错误,该函数message.guild.channels.create不起作用,因为它不是正确的名称。

我的目的是创建一个命令,在该命令中将询问您要创建的频道如何命名。所以它问你这个。在此之后,您发送想要的频道名称。现在,机器人应该命名频道。(抱歉英语不好和编码技能低,我是初学者)

module.exports = {
  name: "setreport",
  description: "a command to setup to send reports or bugs into a specific channel.",
  execute(message, args) {
    const Discord = require('discord.js')
    

const cantCreate = new Discord.MessageEmbed()
    .setColor('#f07a76')
.setDescription(`Can't create channel.`)



    const hasPerm = message.member.hasPermission("ADMINISTRATOR");

    const permFail = new Discord.MessageEmbed()
    .setColor('#f07a76')
.setDescription(`${message.author}, you don't have the permission to execute this command. Ask an Admin.`)
    
    if (!hasPerm)  {
     message.channel.send(permFail);
    }

else if (hasPerm) {
const askName = new Discord.MessageEmbed()
    .setColor(' #4f6abf')
.setDescription(`How should the channel be called?`)

   message.channel.send(askName);
        const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { max: 1, time: 10000 });
        console.log(collector)
         var array = message.content.split(' ');
array.shift();
let channelName = array.join(' ');
        collector.on('collect', message => {
         
            
            const created = new Discord.MessageEmbed()
    .setColor('#16b47e')
.setDescription(`Channel has been created.`)
            
message.guild.channels.create(channelName, {
        type: "text", 
        permissionOverwrites: [
           {
             id: message.guild.roles.everyone,
             allow: ['VIEW_CHANNEL','READ_MESSAGE_HISTORY'],
             deny: ['SEND_MESSAGES']
           }
        ],
      })
      .catch(message.channel.send(cantCreate))
  
        })

}
else {
  message.channel.send(created)
}



       }
        }

标签: javascriptdiscorddiscord.js

解决方案


message对象当前指的是用户发布的原始消息。您不会以其他方式声明它,尤其是看到您在为新频道名称定义新定义/变量之前没有等待收集消息。

注意:在下面的代码中,我将使用awaitMessages()(消息收集器,但依赖于承诺),因为我认为它更适合这种情况(因为您很可能不希望它是异步的)并且可以清理代码一点点。

const filter = m => m.author.id === message.author.id
let name // This variable will later be used to define our channel's name using the user's input.

// Starting our collector below:
try {
  const collected = await message.channel.awaitMessages(filter, {
    max: 1,
    time: 30000,
    errors: ['time']
  })
  name = collected.first().content /* Getting the collected message and declaring it as the variable 'name' */
} catch (err) { console.error(err) }

await message.guild.channels.create(name, { ... })

推荐阅读