(Discord.js)尝试根据频道名称获取频道ID,并在其中发布消息

真子3

我正在尝试使用一堆以用户命名的文本通道来设置服务器,以便可以将其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);

运行此代码后,它给我一个错误,显示为:无法读取未定义的属性“发送”。我还想知道我是否能够在类别内创建文本通道。

朱留op

首先,使用letover 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用Discord.js中的频道ID以字符串格式获取频道名称

来自分类Dev

如何获取频道上的所有消息并发布到hastebin?discord.js

来自分类Dev

频道创建名称Discord.js

来自分类Dev

如何通过漫游器获取频道ID-Discord.js

来自分类Dev

检查用户是否可以在特定频道中发送消息discord.js

来自分类Dev

Discord.js:验证频道

来自分类Dev

Discord bot在特定频道中发送味精

来自分类Dev

尝试使用Discord.js使用Discord机器人将消息发送到特定频道

来自分类Dev

Discord.js搜索特定的频道名称以发送消息到(日志)

来自分类Dev

discord.js将消息发送到特定频道

来自分类Dev

未定义Discord.js频道的欢迎消息

来自分类Dev

Discord.js发送消息到特定频道

来自分类Dev

在特定公会的特定频道中编辑消息 | Discord.js

来自分类Dev

检索所有具有匹配名称的Discord频道ID

来自分类Dev

Discord.js-获取频道中的最新帖子

来自分类Dev

Discord,JS机器人制作频道

来自分类Dev

使用discord.js创建频道

来自分类Dev

如何与父母建立Discord频道?

来自分类Dev

Discord Bot 应删除“用户固定消息到频道”消息

来自分类Dev

Discord.js - 如何在文本频道中发送垃圾邮件?

来自分类Dev

Discord意图discord.js

来自分类Dev

如何使Discord Bot删除频道中的所有消息?

来自分类Dev

Discord Py将消息发送到特定频道

来自分类Dev

无法清除频道discord.py中的消息

来自分类Dev

Discord.py:如何浏览频道历史并搜索特定消息?

来自分类Dev

向所有频道发送消息— Discord.py

来自分类Dev

如何使Discord Bot删除频道中的所有消息?

来自分类Dev

Discord bot找不到按名称显示的频道

来自分类Dev

如何使用Discord.js删除具有var名称的频道

Related 相关文章

热门标签

归档