简体   繁体   English

我正在尝试创建一个discord.js命令,该命令从json文件中抓取一行,然后在用户DM中发送它

[英]I'm trying to create a discord.js command that grabs a line from a json file and then sends it in a users DM

So i have a discord bot that generates keys, which you can redeem by a command. 因此,我有一个不和谐的bot,它可以生成密钥,您可以通过命令进行赎回。 I want a command that grabs a line from a keys.txt and dms the sender of the command. 我想要一个可从keys.txt中抓取一行并命令发送者dms的命令。

I have tried doing it myself, without any luck. 我尝试自己做,没有任何运气。 I'm not that familiar with javascript so it's hard for me. 我对javascript不太熟悉,所以对我来说很难。

I don't have any code yet. 我还没有任何代码。

I expect it to DM the sender of the command the key that it grabbed from the keys.json 我希望它能将命令从key.json中获取的密钥发送给命令发送者

Reading and handling the file... 读取和处理文件...

const fs = require('fs');  // Included in Node.js

const keysPath = './keys.txt'; // Change to the relative path of the txt file

try {
  // The next line reads the file and returns an array containing each line
  const keys = fs.readFileSync(keysPath).replace(/\r/g, '').split(/\n/);
} catch(err) {
  console.error(err);
}

Sending and handling a key... 正在发送和处理密钥...

message.author.send(`Here's your key: ||${keys[0]}||`)
  .then(() => {
    keys.splice(0, 1); // Removes the first key that was just given out

    try {
      fs.writeFileSync(keys.join('\n')); // Puts the updated 'keys' array back into the file
    } catch(err) {
      console.error(err);
    }
  })
  .catch(console.error);

Get a line from a txt file 从txt文件获取一行

For this you need "line-reader". 为此,您需要“行读取器”。 Install it with npm i line-reader . npm i line-reader安装它。

const lineReader = require("line-reader")

lineReader.eachLine("path/to/file.txt", (line) => {

 // This will be executed for each line in the file

})

Send a direct message to a user who executed a command 向执行命令的用户发送直接消息

// client is your Discord.Client()

client.on("message", (message) => {

 if (message.content === "YOUR_COMMAND") {

  message.author.send("YOUR_MESSAGE")

 }

})

Full code 完整代码

const lineReader = require("line-reader")

var keys = []

lineReader.eachLine("path/to/file.txt", (line) => {

 keys.push(line)

})

client.on("message", (message) => {

 if (message.content === "YOUR_COMMAND") {

  message.author.send("Key: " + keys[0]) // This will send the user the first key/line in the list.

 }

})

暂无
暂无

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

相关问题 discord.js DM 命令未直接消息提及用户 - discord.js DM command not direct messaging mentioned users discord.js || 具有特定角色的 dm 用户 - discord.js || dm users with certain role Discord.js ID 支持 DM 命令 - Discord.js ID support for DM command Discord.js 数据库从 .txt 发送同一行 - Discord.js database sends same line from .txt 我正在使用 VSC 中的 discord.js 编写 discord 机器人,所有命令都在响应,除了一个,这是我正在尝试创建的票证命令 - I'm coding a discord bot using discord.js in VSC and all commands are responding except one, which is a ticket command I'm trying to create 我正在使用 Discord.js 制作机器人。 这个机器人将用户保存到一个数组中,以便稍后对他们进行 dm。 我收到错误 - i'm using Discord.js to make a bot. This bot saves users into an array to later dm them. i'm getting errors 我正在尝试在 discord.js v13 中创建一个斜杠命令处理程序 - I'm trying to make a slash command handler in discord.js v13 我试图运行命令 Discord.js 但出现错误“TypeError:无法读取未定义的属性'get'” - i m trying to run a command Discord.js but error appear “TypeError: Cannot read property 'get' of undefined” 我正在尝试创建一个名为 user args 的频道。 [DISCORD.JS V12] - I'm trying to create a channel named like user args. [DISCORD.JS V12] 在 discord.js 中处理将 DM 发送给用户的错误 - Handle error sending DM to users in discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM