javascript - 我的舰船命令不适用于提及
问题描述
我正在尝试进行船舶指挥,我几乎已经完成了所有工作,但是当我使用它时有两个问题:
- 它从两个词中取前半部分(我想从第一个词中取前半部分,从第二个词中取后半部分)
- 当我提到 2 个人时,我的机器人没有回应
我的代码如下所示:
function getRandomIntInclusive() {
return Math.floor(Math.random() * (100 - 1 + 1)) + 1;
}
if (!args[0]) return message.channel.send("Podaj pierwszy argument!")
if (!args[1]) return message.channel.send("Podaj drugi argument!")
if (args[0] || args[1]) {
var FirstUser = args[0]
var SecondUser = args[1]
if (message.mentions.members.first()) {
const FirstUserSliced = FirstUser.user.username.slice(0, FirstUser.user.username.length / 2)
const SecondUserSliced = SecondUser.map(user => { return user.user.username.slice(user.user.username.length / 2) })
const SecondUserName = SecondUser.map(user => { return user.user.username })
} else if (FirstUser || SecondUser) {
const FirstUserSliced = FirstUser.slice(0, FirstUser.length / 2)
const SecondUserSliced = SecondUser.slice (SecondUser.lenght / 2, SecondUser.length / 2)
const SecondUserName = FirstUserSliced + SecondUserSliced
const embed = new MessageEmbed()
.setTitle('Ship')
.setDescription(`${SecondUserName}`)
.addField(`Ocena shipu:`, `${getRandomIntInclusive()}%`)
.setColor(0x0099ff)
.setTimestamp()
.setFooter(`${message.author.username}`);
message.channel.send(embed)
}
}
```
解决方案
让我回答你的问题:
1.从两个词中取前半部分(我想从第一个词中取前半部分,从第二个词中取后半部分)
在您的代码中,您有这一行
slice(SecondUser.lenght / 2, SecondUser.length / 2)
,假设您在lenght
此处编写(StackOverflow),它的作用是削减前半部分,否则如果您有错字,它会通知您。
- 当我提到 2 个人时,我的机器人没有回应
因为你有那个
if (message.mentions.members.first())
没有进入 MessageEmbed 的 if 语句,所以如果你提到某人,它会转到第一个 if 语句并且因为没有消息而无法发送消息。
我为您的代码编写了一个更简单的版本,因此更容易理解。
function GetHalfText(first, second) {
return first.substring(0, Math.floor(first.length / 2)) + second.substring(Math.floor(second.length / 2), second.length);
}
function Match(arg){
return arg.match(/<@!?(\d{17,19})>/);
}
const {mentions, guild} = message
if(args.length < 2) return message.channel.send("You must enter two arguments")
const FirstArg = Match(args[0]) ? guild.members.cache.get(Match(args[0])[1]).user.username : args[0];
const SecondArg = Match(args[1])|| guild.members.cache.get(Match(args[1])[1]).user.username || args[1];
const embed = new MessageEmbed()
.setTitle('Ship')
.setDescription(`${GetHalfText(FirstArg, SecondArg)}`)
.addField(`Ocena shipu:`, `${getRandomIntInclusive()}%`)
.setColor(0x0099ff)
.setTimestamp()
.setFooter(`${message.author.username}`);
message.channel.send(embed)
推荐阅读
- webpack - Webpack:设置用于加载器的默认选项
- boxplot - sns.boxplot 中的 Wish 参数在更改时不会反映在图形上
- java - 我可以使用 MongoRepository 从另一个集合中检索数据吗?
- php - PHP 中的关联
- php - 在 laravel 中创建外键时出现错误
- node.js - 错误:调用 passport.serializeUser 时无法读取 null 的属性“用户”
- arrays - 如何使用术语查询在此数组中进行搜索?
- c# - 使用 IEnumerable 作为只读列表会更好吗?
- sqlite - (sqlite) 如何跨四个表共享同一个主键
- ffmpeg - 使用 ffmpeg 将视频转换为帧时出错