Javascript Discord 使用 bot 创建频道
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43514065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Discord make channel using bot
提问by Jim Knee
I'm making a discord bot, and I'm trying to make use of the createChannel function shown herein the documentation. For some reason, I am getting the following error:
我正在做一个不和谐机器人,和我想利用所示的createChannel功能在这里的文档。出于某种原因,我收到以下错误:
TypeError: bot.createChannel is not a function.
类型错误:bot.createChannel 不是函数。
My code is within a function which I pass a message to, and I have been able to create roles and add users to roles within the same function. It's just the createChannel function that's not working. Below is the relevant portions of the code.
我的代码位于我向其传递消息的函数中,并且我已经能够创建角色并将用户添加到同一函数中的角色。只是 createChannel 函数不起作用。下面是代码的相关部分。
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createRole(data);
var newrole = server.roles.find("name", name);
message.author.addrole(newrole);
/* The above 3 lines all work perfectly */
bot.createChannel(server,name);
}
I have also tried bot.addChannel, and bot.ChannelCreate, since ChannelCreate.js is the name of the file which contains the code for this command. Also, I have attempted specifying channel type and assigning a callback function as well, but the main issue is the TypeError saying that this isn't a function at all. Any idea what I'm doing wrong?
我还尝试了 bot.addChannel 和 bot.ChannelCreate,因为 ChannelCreate.js 是包含此命令代码的文件的名称。此外,我还尝试指定通道类型并分配回调函数,但主要问题是 TypeError 表示这根本不是函数。知道我做错了什么吗?
Additionally, I plan to use ServerChannel.update() at some point in the future, so any advice on getting that to work once the previous problem is resolved would be greatly appreciated.
此外,我计划在未来的某个时候使用 ServerChannel.update(),因此,在解决上一个问题后,任何有关使其工作的建议都将不胜感激。
回答by Jim Knee
Alright, after a few days of trying things and going through the docs, I have discovered the solution. I am using a more recent version of Discord than the docs I was reading were written for. In the newer version, channels are created with a method in the server, not a client method. so, the code should be:
好吧,经过几天的尝试和阅读文档,我找到了解决方案。我使用的 Discord 版本比我阅读的文档要更新。在较新的版本中,通道是使用服务器中的方法创建的,而不是客户端方法。所以,代码应该是:
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createChannel(name, "text");
}
The "text" value is the type of channel you are making. Can be text or voice.
“文本”值是您正在制作的频道类型。可以是文字或语音。
I'll post a link to the most recent documentation for anyone else who encounters this problem here.
我将在此处为遇到此问题的任何其他人发布指向最新文档的链接。
回答by OfftheCode
The answer should update documentation link to the GuildChannelManagerwhich is now responsible for creating new channel.
答案应该更新GuildChannelManager现在负责创建新频道的文档链接。
(Example from docs)
(来自文档的示例)
// Create a new text channel
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(console.log)
.catch(console.error);
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager
回答by turnip
I think you have not logged in with your bot.
我认为您还没有使用您的机器人登录。
From the docs:
从文档:
const Discord = require('discord.js');
var client = new Discord.Client();
client.login('[email protected]', 'password', output); // you seem to be missing this
function output(error, token) {
if (error) {
console.log(`There was an error logging in: ${error}`);
return;
} else
console.log(`Logged in. Token: ${token}`);
}
Alternatively, you can also login with a token instead. See the docs for the example.
或者,您也可以使用令牌登录。有关示例,请参阅文档。

