简体   繁体   English

如何从带有变量的 json 文件中传递 discord 机器人的答案?

[英]How to pass the answer for a discord bot from a json file with variables?

I want to have a.json file where I store all the data about the commands and the answers.我想要一个 .json 文件,在其中存储有关命令和答案的所有数据。 Is it possible, to convert {"message": "'<@' + msg.mention.members.first().id + '>'"} the message with all its variables so that discord can understand it when calling msg.channel.send?是否可以转换{"message": "'<@' + msg.mention.members.first().id + '>'"}消息及其所有变量,以便 discord 在调用 msg 时可以理解它。频道.发送? What I have so far is:到目前为止,我所拥有的是:

msg.channel.send(commands[i].message);

But Discord displays the message instead of interpreting it.但是 Discord 显示消息而不是解释它。 Is there any way of doing so?有什么办法吗?

Found the answer myself:自己找到了答案:

msg.channel.send(eval(commands[i].message));

Hello Nicolas!你好尼古拉斯!

Most of the larger bots you may see around server use a command handler.您可能在服务器周围看到的大多数大型机器人都使用命令处理程序。 A command handler (often called CMD handler) is a function or method that dynamically read a directory most often or something else.命令处理程序(通常称为 CMD 处理程序)是 function 或最常动态读取目录或其他内容的方法。 This is an easy way to make your code more readable and easier for you the developer to create commands!这是一种使您的代码更具可读性和开发人员更容易创建命令的简单方法!

So say you have a directory/folder that looks like the following:假设您有一个如下所示的目录/文件夹:

MyDiscordBot
¦---package.json
¦---index.js
¦---Commands
¦   ----Help.js
¦   ----Kick.js

You can easily read the directory with fs readdir() and require the file through a loop of the directory.您可以使用 fs readdir()轻松读取目录,并通过目录循环获取文件。 Then execute an exported function or method in that file.然后在该文件中执行导出的 function 或方法。

Not sure if that is what you meant, but if it was - for more info you can use discord.js guide for a command handler!不确定这是否是您的意思,但如果是的话-有关更多信息,您可以使用 discord.js 指南作为命令处理程序!

Hope this helped:)希望这有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM