Node.JS Discord机器人输入

时间:2019-12-13 23:04:58

标签: javascript node.js discord

我正在制造一个不和谐的机器人,需要从用户命令(如!add var1 var2 var3 var4 var4 var5 var6)中捕获文本,而vars是需要放入mysql的信息。该代码可以正常运行,但是我是node.js和discord的新手,在Google上找不到如何捕获命令输入以用作数据库值的任何内容。

const prefix = "!";
client.on("message", message => {
if (message.content.startsWith(prefix + "kos")) {
    connection.query("SELECT kos_id, kos_name, kos_coords,kos_seenby FROM rogue_kos ORDER BY 
kos_name", function(err, rows, fields) {
    rows.forEach(function(row) {
    console.log(row.kos_id, row.kos_name, row.kos_coords, row.kos_seenby, row.kos_date);
        const embed = new Discord.RichEmbed()
        .setTitle('Records Management')
        .setColor(3447003)
        .setDescription('Please select an option.')
        .setTimestamp()
        .addField('1. View KoS List', 'Respond with "1" to SEARCH records')
        .addField('2. Add to KoS List', 'Respond with "2" to ADD a record - Currently 
Unavailable.');
        message.channel.send({embed})


        message.channel.awaitMessages(response => (response.content === '1' || response.content === 
"2"), {
            max: 1,
            time: 100000,
            errors: ['time']
        }).then(mg => {
            if (mg.first().content === "1"){ //Have to use .first() because mg is a Collection
        const embed = new Discord.RichEmbed()
        .setTitle('Search Results')
        .setColor(3447003)
        .setDescription('ID | NAME | COORDS | ADDED BY | DATE ADDED\n\n' + row.kos_id + ' | ' + 
row.kos_name + ' | ' + row.kos_coords + ' | ' + row.kos_seenby + ' | ' + row.kos_date)
        .setTimestamp()
        message.channel.send({embed})
            }else if (mg.first().content === "2"){
        const embed = new Discord.RichEmbed()
        .setTitle('Add Record')
        .setColor(3447003)
        .setDescription('Currently Unavailable')
        .setTimestamp()
        message.channel.send({embed})
            }
        }).catch((err) => {
            message.channel.send("Ran out of time!");
        })
    })
})
}
});

1 个答案:

答案 0 :(得分:0)

我不知道语法是否相同,但是对于sqlite3数据库,您将使用这样的查询

const arg = commandArgs;
const user = message.author;
const query = `UPDATE yourTableNameHere SET yourColumn=? WHERE user_id= ?`
  db.run(query, [arg, user],(err, rows) {
    if (err) {
      return console.error(err.message);
    }
***your if/else statements here***    
});

这当然不准确,但是可能会让您知道如何执行此操作。另外,您还想添加限制和检查以确保命令参数符合您的期望。