我正在尝试使用一堆以用户命名的文本通道来设置服务器,以便可以将其DM读取到机器人。我不知道如何找到带有该人标签的文本通道,并且我不确定这是否是执行此操作的正确代码。这是我要使用的代码:
try{
var txtChannel = client.guilds.cache.get(dmServerID).channels.find(channel => channel.name === (mesage.author.tag.replace('#','-')) && channel.type === "text");
}
catch(e){
client.guilds.cache.get(dmServerID).channels.create(message.author.tag.replace('#', '-'), 'text');
}
txtChannel.send(message.author.tag + ": " + message.content);
运行此代码后,它给我一个错误,显示为:无法读取未定义的属性“发送”。我还想知道我是否能够在类别内创建文本通道。
首先,使用let
over var
。
该错误是由以下事实引起的:您在中声明了txtChannel,try
因此在该代码块之外不可用。
另外,您忘记了channels.cache
(如果使用的是discord.js v12)。
代码如下所示:
let txtChannel = null;
// Already get the guild from the cache since we are gonna use it a bit
const guild = client.guilds.cache.get(guildID);
// Find a text channel that includes in the name the tag of the user
txtChannel = GUILD.channels.cache.find(c => c.type === "text" && c.name.includes(message.author.tag.replace('#','-'));
// If the channel doesn't exist we create the channel, using await to wait until the channel gets successfully created
if(!txtChannel) txtChannel = await GUILD.channels.create(message.author.tag);
// At the end we send the message in the gotten / created channel
txtChannels.send("the message to send");
请注意,此代码必须位于异步函数中,您可以在此处了解更多信息:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句