Discord.js在执行!register命令后未捕获名称。

huangapple go评论75阅读模式
英文:

Discord.js not capturing name after !register command

问题

if (message.content === '!registrar') {
    // Capture what the user types after the command "!registrar"
    var newName = message.content.split(' ')[1];
    console.log(newName);
}

The code is intended to capture what the user types after "!register" and store it in the newName variable, then display it in the console for testing. If it's not working as expected, there may be an issue with the input or the context in which this code is running.

英文:
    if (message.content === '!registrar') {
        // pegue o nome do usuário digitado após o comando "!registrar"
        var newName = message.content.split(' ')[1];
        console.log(newName);
    }

The idea is to capture what the user types after the !register and store it in the newName variable and display it in the console for testing, but it is not storing it and it also does not print any results in the console, nor does it print the message "undefined", it seems to me that the split is not working, however when I put only message.content without the split also it does not store what was typed by the user.

I tried to store in the variable only what the user typed without using split and it still didn't store it, I also tried to just show that the command is working after trying to store the information in the variable but it also doesn't go to the next line, it doesn't display no errors in the console, it just doesn't display what the user typed after the "!register"

答案1

得分: 1

在你的 if 条件中,你检查消息是否等于 '!registrar'。这不会适用于类似 '!registrar Name' 这样的命令。
你需要检查输入的字符串是否以给定的命令前缀开头

所以你需要像这样修改它:

const registerCmd = ''!registrar'';
// 检查消息是否以注册命令开头
if (message.content.startsWith(registerCmd)) {
  // 从消息中获取名称
  let args = message.content.slice(registerCmd.length).trim().split(/ +/g);
  let newName = args.shift().toLowerCase();
  // 打印名称
  console.log(newName);
}

获取更多信息,请查阅文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith?retiredLocale=en

英文:

In your if condition you check if the message equals '!registrar'. It won't work with a command like '!registrar Name'.
You need to check if the entered String starts with the given command prefix.

So you have to change it like that:

const registerCmd = '!registrar';
// check if message starts with register command
if(message.content.startsWith(registerCmd)) {
  // get name from message
  let args = message.content.slice(registerCmd.length).trim().split(/ +/g);
  let newName = args.shift().toLowerCase();
  // print name
  console.log(newName);
}

For more information check the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith?retiredLocale=en

huangapple
  • 本文由 发表于 2023年1月7日 23:41:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75041599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定