簡體   English   中英

Node.JS Discord 機器人輸入

[英]Node.JS Discord bot input

我正在制作一個不和諧的機器人,需要從用戶命令中捕獲文本,例如 !add var1 var2 var3 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!");
        })
    })
})
}
});

我不知道語法是否相同,但是對於 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***    
});

當然,這並不准確,但它可以讓您了解如何去做。 此外,您可能希望添加限制和檢查以確保命令參數是您所期望的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM