我正在尝试进行船舶指挥,我几乎已经完成了所有工作,但是当我使用它时有两个问题:
我的代码如下:
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)
}
}
```
答案 0 :(得分:1)
让我来回答您的问题:
1.两个单词取前半部分(我想取第一个单词的前半部分,第二个单词的后半部分)
<块引用>在你的代码中有这一行 slice(SecondUser.lenght / 2, SecondUser.length / 2)
,假设你在这里写了 lenght
(StackOverflow),它的作用是削减前半部分,否则如果你有那个错字,它会通知你。
因为你有 if 语句 if (message.mentions.members.first())
没有进入 MessageEmbed 所以如果你提到某人它会转到第一个 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)