如何从命令参数数组中删除提到的用户?
How do I remove mentioned users from the command arguments array?
我希望能够从命令的参数数组中删除任何用户提及。
我已经尝试按照官方 Discord JS 指南提供的有关此主题的帮助 here,但是该示例仅适用于提及位于特定参数数组位置的情况。
目的是允许用户在命令参数中的任何地方提及其他人(不限于那些提及必须是第一个、第二个、最后一个等参数)并针对所提及的用户制定命令).
有几种方法可以做到这一点,但关键概念是:您想使用 Message.mentions.USERS_PATTERN
检查参数数组中的字符串是否被提及;如果是这样,您可能想要删除它们(或将它们存储在单独的数组中)。
这是一个例子:
// ASSUMPTIONS:
// args is an array of arguments (strings)
// message is the message that triggered the command
// Discord = require('discord.js'); -> it's the module
let argsWithoutMentions = args.filter(arg => !Discord.MessageMentions.USERS_PATTERN.test(arg));
如果您需要使用提及项,您可以通过 Message.mentions.users
获取它们:请注意,它们不会按发送顺序排列。如果您按顺序需要它们,您可以这样做:
let argsWithoutMentions = [],
mentions = [];
for (let arg of args) {
if (Discord.MessageMentions.USERS_PATTERN.test(arg)) mentions.push(arg);
else argsWithoutMentions.push(arg);
}
// In order to use those mentions, you'll need to parse them:
// This function is from the guide you previously mentioned (https://github.com/discordjs/guide/blob/master/guide/miscellaneous/parsing-mention-arguments.md)
function getUserFromMention(mention) {
const matches = mention.match(/^<@!?(\d+)>$/);
const id = matches[1];
return client.users.get(id);
}
let mentionedUsers = [];
for (let mention of mentions) {
let user = getUserFromMention(mention);
if (user) mentionedUsers.push(user);
}
我希望能够从命令的参数数组中删除任何用户提及。
我已经尝试按照官方 Discord JS 指南提供的有关此主题的帮助 here,但是该示例仅适用于提及位于特定参数数组位置的情况。
目的是允许用户在命令参数中的任何地方提及其他人(不限于那些提及必须是第一个、第二个、最后一个等参数)并针对所提及的用户制定命令).
有几种方法可以做到这一点,但关键概念是:您想使用 Message.mentions.USERS_PATTERN
检查参数数组中的字符串是否被提及;如果是这样,您可能想要删除它们(或将它们存储在单独的数组中)。
这是一个例子:
// ASSUMPTIONS:
// args is an array of arguments (strings)
// message is the message that triggered the command
// Discord = require('discord.js'); -> it's the module
let argsWithoutMentions = args.filter(arg => !Discord.MessageMentions.USERS_PATTERN.test(arg));
如果您需要使用提及项,您可以通过 Message.mentions.users
获取它们:请注意,它们不会按发送顺序排列。如果您按顺序需要它们,您可以这样做:
let argsWithoutMentions = [],
mentions = [];
for (let arg of args) {
if (Discord.MessageMentions.USERS_PATTERN.test(arg)) mentions.push(arg);
else argsWithoutMentions.push(arg);
}
// In order to use those mentions, you'll need to parse them:
// This function is from the guide you previously mentioned (https://github.com/discordjs/guide/blob/master/guide/miscellaneous/parsing-mention-arguments.md)
function getUserFromMention(mention) {
const matches = mention.match(/^<@!?(\d+)>$/);
const id = matches[1];
return client.users.get(id);
}
let mentionedUsers = [];
for (let mention of mentions) {
let user = getUserFromMention(mention);
if (user) mentionedUsers.push(user);
}