javascript - 消息和别名
问题描述
前几天我在我的机器人上开发,我想知道是否可以在消息上创建别名,例如我:“ping”,机器人:“Pong!”,所以我想做的是让我可以说:p机器人仍然会用 Pong 响应!我正在使用这段代码:
client.on('message', msg => {
if (msg.content === 'ping') {
msg.lineReplyNoMention('Pong!');
}
});
如果我可以在此代码上创建别名,请告诉我!
解决方案
您可以创建一个别名列表并用于Array#includes()
检查是否msg.content
在该数组中:
let pingCommands = ['ping', 'p']
let msg = {}
msg.content = 'p'
if (pingCommands.includes(msg.content))
console.log(msg.content, 'Pong!')
msg.content = 'pong'
if (pingCommands.includes(msg.content))
console.log(msg.content, 'Pong!')
msg.content = 'ping'
if (pingCommands.includes(msg.content))
console.log(msg.content, 'Pong!')
msg.content = 'penge'
if (pingCommands.includes(msg.content))
console.log(msg.content, 'Pong!')
您的代码应如下所示:
client.on('message', msg => {
let pingCommands = ['ping', 'p']
if (pingCommands.includes(msg.content)) {
msg.lineReplyNoMention('Pong!');
}
});
推荐阅读
- android - 星期几一天一天
- html - 在具有自定义限制的多行上显示 div
- r - R中的数据透视图和分组条形图
- intellij-idea - Intellij - 事件日志字体
- c# - xml 反序列化产生的对象只有 1 个元素,尽管 xml 有多个元素
- rest - 在 DSS 中使用 Base64 编码证书调用服务器签名服务
- c# - XpsDocumenWriter 从 Windows 服务挂起,但从控制台运行时工作正常
- google-analytics - Google 跟踪代码管理器 - 在触发点击代码时从另一个元素返回 href
- java - Tomcat 7 和 Spring boot 大战启动
- function - 仅针对 LUA 中的特定实例的覆盖功能