discord.js Javascript字符串操作



所以我正在创建一个带有kick命令的机器人,并希望能够添加所述操作的原因,我从某个地方听说我可能必须进行字符串操作。目前我有一个独立的原因,如下代码所示:

client.on("message", (message) => {
// Ignore messages that aren't from a guild
if (!message.guild) return;
// If the message starts with ".kick"
if (message.content.startsWith(".kick")) {
// Assuming we mention someone in the message, this will return the user
const user = message.mentions.users.first();
// If we have a user mentioned
if (user) {
// Now we get the member from the user
const member = message.guild.member(user);
// If the member is in the server
if (member) {
member
.kick("Optional reason that will display in the audit logs")
.then(() => {
// lets the message author know we were able to kick the person
message.reply(`Successfully kicked ${user.tag}`);
})
.catch((err) => {
// An error happened
// This is generally due to the bot not being able to kick the member,
// either due to missing permissions or role hierarchy
message.reply(
"I was unable to kick the member (this could be due to missing permissions or role hierarchy"
);
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this server
message.reply("That user isn't in this server!");
}
// Otherwise, if no user was mentioned
} else {
message.reply("You didn't mention the user to kick!");
}
}
});

拆分message.content并对前2个数组元素进行切片,这将留下构成原因的元素。将其余元素连接回字符串。

const user = message.mentions.users.first();
const reason = message.content.split(' ').slice(2).join(' ');

这里有一些可以帮助的东西:

const args = message.content.slice(1).split(" "); //1 is the prefix length
const command = args.shift();
//that works as a pretty good command structure
if(command === 'kick') {
const user = message.mentions.users.first();
args.shift();
const reason = args.join(" ");
user.kick(reason);
//really close to Elitezen's answer but you might have a very terrible problem
//if you mention a user inside the reason, depending on the users' id, the bot could kick 
//the user in the reason instead!
}

以下是如何消除这个问题(使用regex(

const userMention = message.content.match(/<@!?[0-9]+>/);
//you may have to do some more "escapes"
//this works since regex stops at the first one, unless you make it global
var userId = userMention.slice(2, userMention.length-1);
if(userId.startsWith("!")) userId = userId.slice(1);
const user = message.guild.members.cache.get(userId);
args.shift();
args.shift();
user.kick(args.join(" "))
.then(user => message.reply(user.username + " was kicked successfully"))
.catch(err => message.reply("An error occured: " + err.message))

我想您希望完整的命令看起来像

.kick @user Being hostile to other members

如果你想假设命令中的所有内容都不是一个提及或";。踢;命令是原因,然后要从该字符串中获取原因,可以执行一些简单的字符串操作,从字符串中提取命令和提及,并保留其他所有内容。

从未使用过Discord API,但从我从文档中拼凑的内容来看,这应该有效。

let reason = message.content.replaceAll(".kick", "")
message.mentions.forEach((mentionedUser) => reason.replaceAll("@" + mentionedUser.username, "")
// assume everything else left in `reason` is the sentence given by the user as a reason
if (member) {
member
.kick(reason)
.then(() => {
// lets the message author know we were able to kick the person
message.reply(`Successfully kicked ${user.tag}`);
})
}

最新更新