简体   繁体   English

在Discord.JS中遇到Poll命令问题。 你是如何解决这个问题的?

[英]Having problems with Poll Command in Discord.JS. How do you fix this?

I've been coding a Poll Command for my Discord Bot. 我一直在为我的Discord Bot编写Poll命令。 It is in Discord.JS but when I am going to run the command, it does this error: 它在Discord.JS中,但是当我要运行该命令时,它会执行以下错误:

I've been trying to fix this issue for a while and it still does this issue. 我一直试图解决这个问题一段时间,它仍然会解决这个问题。 I've changed some lines of code, particularly line 65 and line 70-80. 我改变了一些代码行,特别是第65行和第70-80行。

Code: 码:

const options = [
  '🇦',
  '🇧',
  '🇨',
  '🇩',
  '🇪',
  '🇫',
  '🇬',
  '🇭',
  '🇮',
  '🇯',
  '🇰',
  '🇱',
  '🇲',
  '🇳',
  '🇴',
  '🇵',
  '🇶',
  '🇷',
  '🇸',
  '🇹',
  '🇺',
  '🇻',
  '🇼',
  '🇽',
  '🇾',
  '🇿',
];

const pollLog = {}; 

function canSendPoll(user_id) {
  if (pollLog[user_id]) {
    const timeSince = Date.now() - pollLog[user_id].lastPoll;
    if (timeSince < 1) {
      return false;
    }
  }
  return true;
}

exports.run = async (client, message, args, level, Discord) => {

    if (args) {
      if (!canSendPoll(message.author.id)) {
        return message
          .channel
          .send(`${message.author} please wait before sending another poll.`);
      } else if (args.length === 1) { // yes no unsure question
        const question = args[0];
        pollLog[message.author.id] = {
          lastPoll: Date.now()
        };
        return message
          .channel
          .send(`${message.author} asks: ${question}`)
          .then(async (pollMessage) => {
            await pollMessage.react('👍');
            await pollMessage.react('👎');
            await pollMessage.react(message.guild.emojis.get('475747395754393622'));
          });
      } else { // multiple choice
        args = args.map(a => a.replace(/"/g, ''));
        const question = args[0];
        const questionOptions = message.content.match(/"(.+?)"/g);
        if (questionOptions.length > 20) {
          return message.channel.send(`${message.author} Polls are limited to 20 options.`);
        } else {
          pollLog[message.author.id] = {
            lastPoll: Date.now()
          };
          return message
            .channel
            .send(`${message.author} asks: ${question}
${questionOptions
    .map((option, i) => `${options[i]} - ${option}`).join('\n')}
`)
            .then(async (pollMessage) => {
              for (let i = 0; i < questionOptions.length; i++) {
                await pollMessage.react(options[i]);
                }
            });
        }
      }
    } else {
      return message.channel.send(`**Poll |** ${message.author} invalid Poll! Question and options should be wrapped in double quotes.`);
    }
  }

The reason some of the question is listed as choices is because you define question as args[0] , which is simply the first word given. 将某些问题列为选项的原因是因为您将question定义为args[0] ,这只是给出的第一个单词。 You can solve this by looping through the arguments and adding those that don't appear to be a choice into the question. 您可以通过循环遍历参数并将那些似乎不是选择的参数添加到问题中来解决此问题。 See the sample code below. 请参阅下面的示例代码。

const args = message.content.trim().split(/ +/g);

// Defining the question...
let question = [];

for (let i = 1; i < args.length; i++) {
  if (args[i].startsWith('"')) break;
  else question.push(args[i]);
}

question = question.join(' ');

// Defining the choices...
const choices = [];

const regex = /(["'])((?:\\\1|\1\1|(?!\1).)*)\1/g;
let match;
while (match = regex.exec(args.join(' '))) choices.push(match[2]);

// Creating and sending embed...
let content = [];
for (let i = 0; i < choices.length; i++) content.push(`${options[i]} ${choices[i]}`);
content = content.join('\n');

var embed = new Discord.RichEmbed()
  .setColor('#8CD7FF')
  .setTitle(`**${question}**`)
  .setDescription(content);

message.channel.send(`:bar_chart: ${message.author} started a poll.`, embed)
  .then(async m => {
    for (let i = 0; i < choices.length; i++) await m.react(options[i]);
  });

The Regex used is from this answer (explanation included). 使用的正则表达式来自这个答案 (包括解释)。 It removes the surrounding quotation marks, allows escaped quotes, and more, but requires a solution like this to access the desired capturing group. 它消除了周围的引号,允许转义引号,更多的,但需要像解决这个访问所需的捕获组。

Note that you'll still have to check whether there's a question and if choices exist, and display any errors as you wish. 请注意,您仍然需要检查是否存在问题以及是否存在选项,并根据需要显示任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM