首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向前缀添加空格

向前缀添加空格
EN

Stack Overflow用户
提问于 2021-05-15 18:25:53
回答 1查看 43关注 0票数 1

例如,我想在我的机器人前缀中添加他的名字+,+一个空格和命令,类似于

代码语言:javascript
复制
bot, weather Dubai

使用

代码语言:javascript
复制
bot,weather Dubai

它是有效的,但与空间不同。这是我的命令运行文件

代码语言:javascript
复制
if(message.author.bot || message.channel.type === "dm") return;
let prefix = 'bot, ';//botconfig.prefix;
let messageArray = message.content.split(" ")
let cmd = messageArray[0].toLowerCase();
console.log(messageArray);
//let args = messageArray.slice(1);
let args = messageArray.slice(1);
if(!message.content.startsWith(prefix)) return;
let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
if(commandfile) commandfile.run(bot, message, args);
console.log(commandfile)

那么,我怎样才能得到命令来处理这么多的空间呢?有没有办法添加它或者/

EN

回答 1

Stack Overflow用户

发布于 2021-05-15 18:39:32

您只需先删除前缀,然后将剩余的字符串拆分为一个数组。第一个元素是命令,其余的是参数。

尝试运行下面的代码片段:

代码语言:javascript
复制
let message = {
  content: 'bot, weather Dubai'
}
let prefix = 'bot, '

// create an args variable that slices off the prefix and splits it into an array
let args = message.content.slice(prefix.length).split(/ +/);
// create a command variable by taking the first element in the array
// and removing it from args
let command = args.shift().toLowerCase();

console.log({
  args,
  command
})

您的代码将如下所示:

代码语言:javascript
复制
let prefix = 'bot, ';
if (
  message.author.bot ||
  message.channel.type === 'dm' ||
  !message.content.startsWith(prefix)
)
  return;

let args = message.content.slice(prefix.length).split(/ +/);
let command = args.shift().toLowerCase();

let commandfile =
  bot.commands.get(command) || bot.commands.get(bot.aliases.get(command));
if (commandfile) commandfile.run(bot, message, args);
console.log(commandfile);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67545682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档