创建一个不和谐的频道.js



我正在尝试做一个创建频道的不和谐机器人,但我无法实现它,因为我的代码应该创建一个频道,但是......我尝试了很多东西,这是两个没有给我错误的东西:(我用 DONOTLOOK 替换了机器人的令牌,我不会有问题......

选项 1:

const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('DONOTLOOK');
bot.on('ready', () => {
console.log('Logged in as ${bot.user.tag}!');
});
bot.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
var server = msg.guild;
bot.channels.add("anewchannel", {type: 0});
}
});

选项 2:

const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('DONOTLOOK');
bot.on('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
});
bot.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
var server = msg.guild;
const channel = bot.channels.add("newchannel", {type: 0});
}
});

这没有给我任何节点.js控制台上的错误,机器人回答了我,但尚未创建通道。我还查看了两个参考(https://discord.com/developers/docs/resources/guild#create-guild-channel,https://discord.js.org/#/docs/main/stable/class/GuildChannelManager?scrollTo=create(,没有人的解决方案有效,discord.com 一个也给了我节点.js错误。上面的两个选项取自 discord.js 文件,它们没有给出节点.js错误,但没有创建通道。如果有人能帮我请。

不和谐.js v13 改变了一些东西。

更新版本:

bot.on("messageCreate", message => { // instead of 'message', it's now 'messageCreate'
if (message.content === "channel") {
message.guild.channels.create("channel name", { 
type: "GUILD_TEXT", // syntax has changed a bit
permissionOverwrites: [{ // same as before
id: message.guild.id,
allow: ["VIEW_CHANNEL"],
}]
});
message.channel.send("Channel Created!");
})

我还建议使用bot.guilds.cache.get(message.guildId).这样就无需使用 API 获取用户。

下面的代码说明了如何从命令创建文本通道。

bot.on('message', msg => { //Message event listener
if (msg.content === 'channel') { //If the message contained the command
message.guild.channels.create('Text', { //Create a channel
type: 'text', //Make sure the channel is a text channel
permissionOverwrites: [{ //Set permission overwrites
id: message.guild.id,
allow: ['VIEW_CHANNEL'],
}]
});
message.channel.send("Channel Created!); //Let the user know that the channel was created
}
});

如果你不想使用消息来创建,你可以使用..

var bot = client.guilds.cache.get("<GUILD-ID>");
server.channels.create("<CHANNEL-TEXT>", "<CHANNEL-TYPE>").then(channel => {
var cat = server.channels.cache.find(c => c.name == "BUSINESSES" && c.type == "category");
if (!cat) { console.log("category does not exist"); return; }
channel.setParent(cat.id);
})

最新更新