Javascript 无法读取属性';发送';未定义:不协调机器人创建一个通道并向其发送消息,然后对其作出反应
我正在尝试制作一个机器人,它将嵌入的消息发送到一个通道,然后对其做出反应。它发送到的频道是由discord机器人创建的,因此我没有频道的ID,只有名称,它只是Javascript 无法读取属性';发送';未定义:不协调机器人创建一个通道并向其发送消息,然后对其作出反应,javascript,discord,discord.js,Javascript,Discord,Discord.js,我正在尝试制作一个机器人,它将嵌入的消息发送到一个通道,然后对其做出反应。它发送到的频道是由discord机器人创建的,因此我没有频道的ID,只有名称,它只是island info-\。此频道是在您运行命令/channel时创建的,但很快将更改为在您加入服务器时创建,并在您离开时删除。当我运行此代码时: else if (cmd === `${prefix}channel`){ const name = "island-info-" + message.author.
island info-\
。此频道是在您运行命令/channel
时创建的,但很快将更改为在您加入服务器时创建,并在您离开时删除。当我运行此代码时:
else if (cmd === `${prefix}channel`){
const name = "island-info-" + message.author.username.toLowerCase();
message.guild.channels.create(name, {
type: 'text',
permissionOverwrites: [
{
id: message.guild.id,
deny: ["VIEW_CHANNEL", "SEND_MESSAGES"]
},
{
id: message.author.id,
allow: ["VIEW_CHANNEL", "ADD_REACTIONS"]
},
],
parent: "734170209107051074"
})
.catch(console.error);
const Embed = new Discord.MessageEmbed()
.setTitle('ISLAND INFO')
message.guild.channels.cache.find(r => r.name === name).send(Embed)
message.guild.channels.cache.find(r => r.name === name).messages.fetch({ limit: 1 }).then(messages => {
messages.first().react("If it is undefined then it means the channel you need with that name does not exist.
I don't know how in your situation you would handle this, but this is an option:
const Embed = new Discord.MessageEmbed()
.setTitle('ISLAND INFO');
const channel = message.guild.channels.cache.find(r => r.name === name);
if (!channel) message.channel.send("Your channel does not exist!");
else {
channel.send(embed)
}
else if(cmd==`${prefix}通道`){
const name=“island info-”+message.author.username.toLowerCase();
message.guild.channels.create(名称{
键入:“文本”,
许可证覆盖:[
{
id:message.guild.id,
拒绝:[“查看频道”、“发送消息”]
},
{
id:message.author.id,
允许:[“查看频道”,“添加反应”]
},
],
家长:“734170209107051074”
})
.catch(控制台错误);
const Embed=new Discord.MessageEmbed()
.setTitle('岛屿信息')
message.guild.channels.cache.find(r=>r.name==name).send(嵌入)
message.guild.channels.cache.find(r=>r.name==name).messages.fetch({limit:1})。然后(messages=>{
messages.first().react(“如果未定义,则表示您需要的具有该名称的频道不存在。
我不知道在你的情况下你会如何处理,但这是一个选择:
//send a request to Discord to make a channel
message.guild.channels.create(name, {...}).catch(console.error);
...
//immediately, without waiting for Discord to make the channel, send a message to it
message.guild.channels.cache.find(r => r.name === name).send(Embed);
按用户名存储数据时要注意的另一件事是,用户名可以更改。我建议您按用户id命名频道,因为它们永远不会更改。在您尝试向频道发送消息时,频道不存在
您使用的是.then()
和.catch()
,因此您必须对承诺有一定的了解。请记住,承诺所代表的操作除了在承诺回调内部(或者如果使用wait
)之外,在任何地方都没有完成
基本上你写的是:
message.guild.channels.create(name, {...}).then(chan => {
//make embed
chan.send(Embed);
}).catch(console.error);
发送消息的代码取决于已创建的频道。因此,需要处于频道的回调中。然后()
调用频道。创建(…)
承诺。这还有一个好处,承诺将实际与频道对象解析,因此您可以调用发送()
,而无需搜索缓存
同样,您需要将.then()
附加到.send()
调用以对刚刚发送的消息作出反应。因为您需要等待Discord真正生成消息,然后才能对其作出反应。问题是,当我在行中添加“.send”时“它不会创建频道,当我拿出那个部分时,它会使频道一切正常。出于某种原因,我认为它是在创建频道的同时发送消息的,所以它还不存在。你知道解决这个问题的方法吗(即将编辑问题以添加此细节)@CR9GAMING嘿,谢谢你,因为这几乎可以正常工作,它现在会向频道发送一条消息,但没有反应,并抛出类似的错误“无法读取未定义频道的属性'react'”。send(embed)。then(message=>message.react('whatever');OMG非常感谢你!Life saver