所以我在创建机器人时遇到了一些问题。我要做的就是跟踪键入命令“ + me”加入“等待列表”,“-me”以便从列表中删除和“?list”的玩家列表。以显示列表。我计划以后再添加其他命令。
我遇到的问题是,该命令可以很好地将某人添加到列表中,但是在执行第一个命令后,机器人会停止响应命令。这样一来,我便可以将自己添加到队列中,但之后便无法离开,没有其他人可以加入,也无法列出它,等等。 另外,如果您能提到一种移动for循环的方法,那么我正在使用它来将列表显示为单独的函数,我将大为赞赏。我是Java语言的新手,由于某种原因,我的尝试使其崩溃了。
const Discord = require('discord.js');
const {prefix, token} = require('./config.json');
const client = new Discord.Client();
var rankedList = []; // Initialise an empty array
client.login(token);
client.once('ready', () =>{
console.log('Ready!');
})
client.once('message', message =>{
// Add to queue
if(message.content.startsWith(`${prefix}me`)){
console.log(message.author + "added to queue.");
message.channel.send(message.author + " added to queue.");
var temp = message.author;
rankedList.push(temp); // the array will dynamically grow
// Show queue after adding
//for (var i = 0; i < rankedList.length; i++) {
// message.channel.send(i+1 + "- " + rankedList[i]);
//}
message.channel.send(`${rankedList.map((player,index) => `${index+1} - ${player}`).join('n')}`);
}
// Remove from queue
if(message.content.startsWith(`-me`)){
console.log(message.author + "removed from queue.");
message.channel.send(message.author + " removed from queue.");
for( var i = 0; i < rankedList.length; i++){
if ( rankedList[i] === message.author) {
rankedList.splice(i, 1);
i--;
}
}
// Show queue after removing
for (var i = 0; i < rankedList.length; i++) {
message.channel.send(i+1 + "- " + rankedList[i]);
}
}
if(message.content.startsWith(`?list`)){
console.log(message.author + "displayed the queue.");
// Show list
for (var i = 0; i < rankedList.length; i++) {
message.channel.send(i+1 + "- " + rankedList[i]);
}
}
})
答案 0 :(得分:3)
问题可能是您使用了“一次”而不是“开启”。后者每次触发事件,而前者每次收听一次。
<ReactTable
data={data}
filterable
defaultFilterMethod={(filter, row) =>
String(row[filter.id]) === filter.value}
columns={[
{
Header: "Name",
columns: [
{
Header: "First Name",
accessor: "firstName",
filterMethod: (filter, row) =>
row[filter.id].startsWith(filter.value) &&
row[filter.id].endsWith(filter.value)
},
{
Header: "Last Name",
id: "lastName",
accessor: d => d.lastName,
filterMethod: (filter, rows) =>
matchSorter(rows, filter.value, { keys: ["lastName"] }),
filterAll: true
}
]
},
{
Header: "Info",
columns: [
{
Header: "Age",
accessor: "age"
},
{
Header: "Over 21",
accessor: "age",
id: "over",
Cell: ({ value }) => (value >= 21 ? "Yes" : "No"),
filterMethod: (filter, row) => {
if (filter.value === "all") {
return true;
}
if (filter.value === "true") {
return row[filter.id] >= 21;
}
return row[filter.id] < 21;
},
Filter: ({ filter, onChange }) =>
<select
onChange={event => onChange(event.target.value)}
style={{ width: "100%" }}
value={filter ? filter.value : "all"}
>
<option value="all">Show All</option>
<option value="true">Can Drink</option>
<option value="false">Can't Drink</option>
</select>
}
]
}
]}
defaultPageSize={10}
className="-striped -highlight"
/>
基本上,您是一次发送整个邮件。我可以看到send()函数是异步[https://discord.js.org/#/docs/main/stable/class/DMChannel?scrollTo=send],因此不应像上面那样在for循环中链接。上面的代码也更加简洁
我已经使用模板文字[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals]删除了多个串联。