javascript - 如何记录用户输入以在以后嵌入 discordjs 中使用
问题描述
我正在制作一个佣金机器人,以便人们打开一张票,然后选择它的类别,但我希望它要求预算等待响应,然后存储输入预算以用于嵌入以发布给自由职业者。
我已经尝试将其存储为常量,然后稍后调用它,但它不想工作,因为我将它存储在不同的函数中。
msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
const budget = messages.first().content;
})
.catch(() => {
msg.channel.send('You did not enter any input!');
});
});
if (messageReaction.emoji.name === reactions.one) {
let web = new Discord.RichEmbed()
.setDescription("Press the check to claim the comission")
.setColor("#15f153")
.addField("Client", `${message.author} with ID: ${message.author.id}`)
.addField("Budget", `${budget}`)
.addField("Time", message.createdAt)
.addField("Requested Freelancer",`<@&603466765594525716>`)
let tickets = message.guild.channels.find('name', "tickets")
if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
我希望它在 .addField 预算部分中发布预算,但它只是说未定义预算
解决方案
您const budget
在与全局范围不同的范围内定义 (有关范围,请参阅此页面)。
这个答案解释了声明、变量和范围如何协同工作。
在这里,您budget
仅在awaitMessages.then
范围内提供,即
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
const budget = messages.first().content;
// the const is only know there
})
但是,该then
块将返回一个值。因为不再有链式承诺(除非有错误,因为它会触发链式catch
)。在此处了解有关承诺的更多信息。
有用的是,一旦 promise 被解决,msg.channel.awaitMessages
就会返回一个值。
然后你可以做两件事:
- 等待 的响应
msg.channel.awaitMessages
,将其分配给变量并稍后使用 - 连锁另一个承诺
等待:
let budget = await msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
return messages.first().content;
})
.catch(() => {
msg.channel.send('You did not enter any input!');
});
});
if (messageReaction.emoji.name === reactions.one) {
let web = new Discord.RichEmbed()
.setDescription("Press the check to claim the comission")
.setColor("#15f153")
.addField("Client", `${message.author} with ID: ${message.author.id}`)
.addField("Budget", `${budget}`)
.addField("Time", message.createdAt)
.addField("Requested Freelancer",`<@&603466765594525716>`)
let tickets = message.guild.channels.find('name', "tickets")
if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
// ...
}
链接:
msg.channel.awaitMessages(filter, { time: 60000, maxMatches: 1, errors: ['time'] })
.then(messages => {
msg.channel.send(`You've entered: ${messages.first().content}`);
return messages.first().content;
})
.then((budget) => {
if (messageReaction.emoji.name === reactions.one) {
let web = new Discord.RichEmbed()
.setDescription("Press the check to claim the comission")
.setColor("#15f153")
.addField("Client", `${message.author} with ID: ${message.author.id}`)
.addField("Budget", `${budget}`)
.addField("Time", message.createdAt)
.addField("Requested Freelancer",`<@&603466765594525716>`)
let tickets = message.guild.channels.find('name', "tickets")
if(!tickets) return message.channel.send(`${message.author} Can't find tickets channel.`)
// ...
}
})
.catch(() => {
msg.channel.send('You did not enter any input!');
});
推荐阅读
- python-3.x - AttributeError:“列表”对象没有属性“读取”
- sql-server - 使用大小写和分隔符获取字段数据
- java - 根据消息类型选择消息实现
- database - 使用python删除对oracle表的查询需要无限的时间
- azure - 使用单一登录(工作帐户)和相同的电子邮件地址管理 Azure 个人和工作订阅
- php - MySQL 子查询无法循环数据
- c# - 如何阻止 VS2017 用空格自动替换 _inline_ 选项卡?
- mysql - SQL - 对数字使用嵌套替换
- xamarin - 当我添加点击手势时,捏和平移在 xamarin 表单中不起作用
- vue.js - 使用 Vue JS 修复 HTML 表上的列