Discord.JS 中的轮询命令出现问题。你如何解决这个问题?
Having problems with Poll Command in Discord.JS. How do you fix this?
我一直在为我的 Discord Bot 编写投票命令。它在 Discord.JS 中,但是当我要执行 运行 命令时,它会出现以下错误:
我已经尝试解决这个问题一段时间了,但它仍然存在这个问题。我更改了一些代码行,特别是第 65 行和第 70-80 行。
代码:
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.`);
}
}
部分问题被列为选项的原因是您将 question
定义为 args[0]
,这只是给出的第一个单词。您可以通过遍历参数并将那些似乎不是一个选择的参数添加到问题中来解决这个问题。请参阅下面的示例代码。
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 = /(["'])((?:\||(?!).)*)/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]);
});
使用的正则表达式来自 this answer (explanation included). It removes the surrounding quotation marks, allows escaped quotes, and more, but requires a solution like this 以访问所需的捕获组。
请注意,您仍然需要检查是否存在问题以及是否存在选项,并根据需要显示任何错误。
我一直在为我的 Discord Bot 编写投票命令。它在 Discord.JS 中,但是当我要执行 运行 命令时,它会出现以下错误:
我已经尝试解决这个问题一段时间了,但它仍然存在这个问题。我更改了一些代码行,特别是第 65 行和第 70-80 行。
代码:
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.`);
}
}
部分问题被列为选项的原因是您将 question
定义为 args[0]
,这只是给出的第一个单词。您可以通过遍历参数并将那些似乎不是一个选择的参数添加到问题中来解决这个问题。请参阅下面的示例代码。
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 = /(["'])((?:\||(?!).)*)/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]);
});
使用的正则表达式来自 this answer (explanation included). It removes the surrounding quotation marks, allows escaped quotes, and more, but requires a solution like this 以访问所需的捕获组。
请注意,您仍然需要检查是否存在问题以及是否存在选项,并根据需要显示任何错误。