javascript - 我正在尝试创建一个名为 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)
}
}
}
解决方案
该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, { ... })
推荐阅读
- git - 从远程 GitHub 存储库替代 git lfs clone 的更快?
- google-cloud-firestore - Firestore 中的交易与 FieldValue.increment(1)?
- android - 滑入对话框片段
- excel - 如何在excel中为相同的数据创建两个水平轴标签?
- haskell - 如何测试 GHC 插件?
- python - 在 python 中,有没有比这更好的方法来处理跨平台斜线?
- javascript - 我一直在尝试将来自两个不同输入的数字修复为两个数组的数组,使其看起来像这样 ["[13, 4]", "[1, 2, 3, 6, 14]"]
- hibernate - 休眠多个表的单个外键
- oracle - 如何使用 SET 运算符从 oracle 集合中查找不区分大小写的重复项
- java - 为什么 switch-case 部分和运行时多态性不起作用?