当前位置: 首页 > 知识库问答 >
问题:

不和谐机器人制造商-MySQL调平系统或Discord.js-MySQL调平系统

彭俊智
2023-03-14

让我来布置一下背景:

Discord Bot Maker:我有一个升级系统,它可以根据帖子添加xp,并将其存储到JSON文件中,并且可以正常工作。一旦成员达到某个xp级别,bot将在discord中为他们分配一个新角色。如果我必须重新启动bot,所有拥有xp的成员都会在1级重新启动,使用0 xp。

不一致JSI有一个xp机器人,它根据帖子添加xp,并将信息存储在在线MySQL数据库中。当角色达到某个xp级别时,此bot不会自动分配角色,但如果我需要重新启动bot,则会保存信息。

我需要帮助(给我指出正确的方向)让DBM机器人在MySQL中存储数据(我已经搜索了几个小时,找不到教程或任何东西)。这里是DBM Raw数据(减去ID和其他不相关的信息)

指挥级

{
  "name": "level",
  "permissions": "NONE",
  "restriction": "1",
  "actions": [
    {
      "member": "1",
      "varName": "",
      "dataName": "exp",
      "defaultVal": "0",
      "storage": "1",
      "varName2": "memberexp",
      "name": "Store Member Data"
    },
    {
      "member": "1",
      "varName": "",
      "dataName": "level",
      "defaultVal": "1",
      "storage": "1",
      "varName2": "myLevel",
      "name": "Store Member Data"
    },
    {
      "channel": "0",
      "varName": "",
      "message": "```You are level ${tempVars(\"myLevel\")} with ${tempVars(\"memberexp\")}!```",
      "storage": "0",
      "varName2": "",
      "name": "Send Message"
    }
  ],
  "_id": "Tucww",
  "comType": "0"
}

指挥级系统

{
  "name": "LevelingSystem",
  "temp": "msg",
  "event-type": "2",
  "actions": [
    {
      "info": "0",
      "find": "channelid goes here",
      "storage": "2",
      "varName": "channel name goes here",
      "name": "Find Channel"
    },
    {
      "storage": "2",
      "varName": "myLevel",
      "comparison": "1",
      "value": "7",
      "iftrue": "2",
      "iftrueVal": "7",
      "iffalse": "0",
      "iffalseVal": "",
      "name": "Check Variable"
    },
    {
      "storage": "2",
      "varName": "myLevel",
      "comparison": "1",
      "value": "13",
      "iftrue": "2",
      "iftrueVal": "10",
      "iffalse": "0",
      "iffalseVal": "",
      "name": "Check Variable"
    },
    {
      "storage": "2",
      "varName": "myLevel",
      "comparison": "1",
      "value": "25",
      "iftrue": "2",
      "iftrueVal": "13",
      "iffalse": "0",
      "iffalseVal": "",
      "name": "Check Variable"
    },
    {
      "storage": "2",
      "varName": "myLevel",
      "comparison": "1",
      "value": "50",
      "iftrue": "2",
      "iftrueVal": "16",
      "iffalse": "0",
      "iffalseVal": "",
      "name": "Check Variable"
    },
    {
      "storage": "2",
      "varName": "myLevel",
      "comparison": "1",
      "value": "100",
      "iftrue": "2",
      "iftrueVal": "19",
      "iffalse": "1",
      "iffalseVal": "",
      "name": "Check Variable"
    },
    {
      "info": "0",
      "find": "roleid number goes here",
      "storage": "2",
      "varName": "role name goes here",
      "name": "Find Role"
    },
    {
      "member": "3",
      "varName2": "member",
      "role": "4",
      "varName": "role name goes here",
      "reason": "",
      "name": "Add Member Role"
    },
    {
      "name": "End Action Sequence"
    },
    {
      "info": "0",
      "find": "roleid number goes here",
      "storage": "2",
      "varName": "role name goes here",
      "name": "Find Role"
    },
    {
      "member": "3",
      "varName2": "member",
      "role": "4",
      "varName": "role name goes here",
      "reason": "",
      "name": "Add Member Role"
    },
    {
      "name": "End Action Sequence"
    },
    {
      "info": "0",
      "find": "roleid number goes here",
      "storage": "2",
      "varName": "role name goes here",
      "name": "Find Role"
    },
    {
      "member": "3",
      "varName2": "member",
      "role": "4",
      "varName": "role name goes here",
      "reason": "",
      "name": "Add Member Role"
    },
    {
      "name": "End Action Sequence"
    },
    {
      "info": "0",
      "find": "roleid number goes here",
      "storage": "2",
      "varName": "role name goes here",
      "name": "Find Role"
    },
    {
      "member": "3",
      "varName2": "member",
      "role": "4",
      "varName": "role name goes here",
      "reason": "",
      "name": "Add Member Role"
    },
    {
      "name": "End Action Sequence"
    },
    {
      "info": "0",
      "find": "roleid number goes here",
      "storage": "2",
      "varName": "role name goes here",
      "name": "Find Role"
    },
    {
      "member": "3",
      "varName2": "member",
      "role": "4",
      "varName": "role name goes here",
      "reason": "",
      "name": "Add Member Role"
    },
    {
      "name": "End Action Sequence"
    }
  ],
  "_id": "gUxAU",
  "restriction": "1",
  "comType": "3",
  "permissions": "NONE"
}

事件级别系统

{
  "name": "LevelingSystem",
  "temp": "msg",
  "event-type": "2",
  "actions": [
    {
      "message": "1",
      "varName": "msg",
      "info": "3",
      "storage": "2",
      "varName2": "member",
      "name": "Store Message Info"
    },
    {
      "member": "3",
      "varName": "member",
      "dataName": "exp",
      "changeType": "1",
      "value": "1",
      "name": "Control Member Data"
    },
    {
      "member": "3",
      "varName": "member",
      "dataName": "level",
      "defaultVal": "1",
      "storage": "2",
      "varName2": "myLevel",
      "name": "Store Member Data"
    },
    {
      "member": "3",
      "varName": "member",
      "dataName": "exp",
      "comparison": "4",
      "value": "{serverVars(\"myLevel\")*48}",
      "iftrue": "0",
      "iftrueVal": "",
      "iffalse": "1",
      "iffalseVal": "",
      "name": "Check Member Data"
    },
    {
      "member": "3",
      "varName": "member",
      "dataName": "level",
      "changeType": "1",
      "value": "1",
      "name": "Control Member Data"
    }
  ],
  "_id": "Mtoid"
}

我需要弄清楚如何消除这种不和谐。当存储在MySQL中的xp达到某个级别时,js bot向成员添加角色。这就是不和谐。js bot代码(再次删除登录信息):

./机器人程序。js

const Discord = require('discord.js');
const bot = new Discord.Client();
const mysql = require("mysql");
const prefix = '.';
const fs = require('fs');

bot.commands = new Discord.Collection();


fs.readdir("./cmds/", (err, files) => {
  if(err) console.error(err);

  let jsfiles = files.filter(f => f.split(".").pop() === "js");
  if(jsfiles.length <= 0) {
    console.log("No commands to load!");
    return;
  }

  console.log(`Loading ${jsfiles.length} commands!`);

  jsfiles.forEach((f, i) => {
    let props = require(`./cmds/${f}`);
    console.log(`${i + 1}: ${f} loaded!`);
    bot.commands.set(props.help.name, props);
  });
});

bot.login(process.env.BOT_TOKEN);

bot.once('ready', () => {
    console.log('Bot is online!')
});

var con =mysql.createConnection({
    host: "host name goes here",
    user: "username goes here",
    password: "password goes here",
    database: "database name goes here"
});

con.connect(err => {
  if(err) throw err;
  console.log("Connected to database.");
  
});

function generateXp() {
  let min = 1
  let max = 1
//the above numbers are both one's because I only want each message to count as 1xp
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

bot.on('message', async message => {
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;

  con.query(`SELECT * FROM xp WHERE id = '${message.author.id}'`, (err, rows) => {
    if (err) throw err;
    
    let sql;

    if(rows.length < 1) {
      sql = `INSERT INTO xp (id, xp) VALUES ('${message.author.id}', ${generateXp()})`
    } else {
        let xp = rows[0].xp;
        sql = `UPDATE xp SET xp = ${xp +generateXp()} WHERE id = '${message.author.id}'`;
    }

    con.query(sql);

  });

  if(/(?:https?:\/)?discord(?:app.com\/invite|.gg)/gi.test(message.content)) {
    message.delete();
    return;
  }

  let messageArray = message.content.split(/\s+/g);
  let command = messageArray[0];
  let args = messageArray.slice(1);

  if(!command.startsWith(prefix)) return;

  let cmd = bot.commands.get(command.slice(prefix.length));
  if(cmd) cmd.run(bot, message, args, con);

});

./cmds/xp。js

module.exports.run = async (bot, message, args, con) => {
    let target = message.mentions.users.first() || message.author;

    con.query(`SELECT * FROM xp WHERE id = '${target.id}'`, (err, rows) => {
        if(err) throw err;

        if(!rows[0]) return message.channel.send("This member has no XP on record.")
        let xp = rows[0].xp;
        message.channel.send(xp);
    });
};

module.exports.help = {
    name: "xp"
}

共有1个答案

江同化
2023-03-14

我对Discord Bot Maker没有太多经验,但我可以为Discord提供一个潜在的解决方案。js。它的文档可能需要一点时间来适应,但是做得很好,对于找到您需要的东西非常有用。

在主消息事件处理程序中,在运行更新其体验点的SQL查询(con.query(SQL))后,您应该检查其体验点是否超过该角色的阈值。

// inside the callback for your SELECT query
if (err) throw err;

let sql;
let originalXp;
let updatedXp;

if (rows.length < 1) {
  originalXp = 0;
  updatedXp = generateXp();
  sql = `INSERT INTO xp (id, xp) VALUES ('${message.author.id}', ${updatedXp})`;
} else {
  originalXp = rows[0].xp;
  updatedXp = originalXp + generateXp();
  sql = `UPDATE xp SET xp = ${updatedXp} WHERE id = '${message.author.id}'`;
}

con.query(sql);

// if you want different roles at certain thresholds, replace this with the snippet later in the answer
if (originalXp < SOME_XP_THRESHOLD && updatedXp >= SOME_XP_THRESHOLD) {
  // this example gets a role based on name
  // you could also get the role you want using some other criteria
  const role = message.member.guild.roles.cache.find(role => role.name === ROLE_TO_ADD);
  message.member.roles.add(role);
}

如果要包含多个角色以达到各种阈值,可以将它们设置在一个数组中:

const roles = [
  {
    name: 'foo',
    threshold: 5,
  },
  {
    name: 'bar',
    threshold: 10,
  },
];

然后在它们之间循环:

// insert this snippet instead at the point indicated above
for (const xpRole of roles) {
  if (originalXp < xpRole.threshold && updatedXp >= xpRole.threshold) {
    const role = message.member.guild.roles.cache.find(role => role.name === xpRole.name);
    // you could potentially remove the previous role here as well
    message.member.roles.add(role);
  }
}
 类似资料:
  • 我已经创建了一个bot,它现在在我的discord服务器中,使用下面的代码。 我的问题是,一旦我在与bot不和谐的聊天中,我如何调用命令让bot运行代码,为用户列表收集csv?我不确定如何调用机器人,一旦它在聊天/服务器中获得列表。

  • 我是一个编程新手。几天前,我开始开发我的第一个discord机器人,你知道,让我和我的朋友一起玩。现在,让我们假设我想让这个机器人检测消息中的单词,并在每次有人提到该单词时回复,无论消息的哪个部分。我能做到,但现在有个问题。假设我要找的单词是“你好”。如果有人说“哦,你好”,“你好”,一条写着“你好”的消息,机器人会回复“你好”。但是机器人也会在自己的消息中检测到hello,并一遍又一遍地回复自己

  • 主要内容:Magento,Shopify,Woocommerce,Bigcommerce在本章中,我们将讨论电子商务。 哪些是最受欢迎和最安全的电子商务平台? 需要什么来保护他们? 电子商务是通过互联网销售和购买商品和服务,并通过这种媒介进行支付。 这个交易发生在客户到企业,B2B,客户端到客户之间。 所有这一切,都有钱交易,在使用或者建立电子商务网站时要慎重。 下面列出了一些安全的顶级电子商务平台,并详细说明了这些平台。 Magento 网站链接是 - http://magento

  • 数据概览 1.机器人问答界面 机器人问答是系统中重要的组成部分,能够帮助企业回答用户高重复、高频、无人或忙碌阶段的问答问题,较好的使用机器人问答能够回复80%以上的客户问题,通过知识维护和学习还将不断提高问答率,节省企业人工,提升准确率。 机器人问答界面包括几种类型: 单独网页标签页界面:单独网页咨询界面是一个独立网页,包含较为全面的页面信息 独立咨询控件窗口界面:单独的咨询控件窗口界面是一个悬浮

  • 本章描述 Linux 内核中的系统调用概念。 系统调用概念简介 - 介绍 Linux 内核中的系统调用概念 Linux 内核如何处理系统调用 - 介绍 Linux 内核如何处理来自于用户空间应用的系统调用。 vsyscall and vDSO - 介绍 vsyscall 和 vDSO 概念。 Linux 内核如何运行程序 - 介绍一个程序的启动过程。 open 系统调用的实现 - 介绍 open

  • curl 和 curl -I 可以被轻松地应用于 web 调试中,它们的好兄弟 wget 也是如此,或者也可以试试更潮的 httpie。 获取 CPU 和硬盘的使用状态,通常使用使用 top(htop 更佳),iostat 和 iotop。而 iostat -mxz 15 可以让你获悉 CPU 和每个硬盘分区的基本信息和性能表现。 使用 netstat 和 ss 查看网络连接的细节。 dstat