无法使用 Discord.js 在数据库中写入和读取值
Posted
技术标签:
【中文标题】无法使用 Discord.js 在数据库中写入和读取值【英文标题】:Unable to write and read values in a database, using Discord.js 【发布时间】:2022-01-24 04:37:15 【问题描述】:我试图让员工使用~reply <message>
而不是~dm <userID> <message>
回复最后一个 DM,但首先,我必须将用户的 ID 保存在数据库中才能知道谁是最后一个 DM 用户,但我在这里遇到一个错误,我很困惑为什么会发生这种情况,但请注意,我对数据库真的很陌生,我会寻求一些帮助!
我的代码(不是全部,只是我用于命令的代码。):
我在错误所在的行之前添加了“>>”。
const Client, Collection, Intents, MessageEmbed = require('discord.js');
const client = new Client(partials: ['MESSAGE', 'CHANNEL', 'REACTION'], ws: intents: Intents.ALL );
const Database = require('@replit/database');
const db = new Database();
client.on('message', async message =>
//Dm checker
if (message.channel.type === 'dm')
>> let lastDM = await db.get(`dm_$message.author.id`)
if (lastDM === null) lastDm = `dm_NONE`
if (message.author.id == client.user.id) return;
if (message.author.id == '818749579369512978') return message.channel.send("This chat has been Blacklisted by the developer (<@"+ BOT_OWNER +">)");
const embed1 = new MessageEmbed()
.setTitle("New message!")
.setAuthor(`Name: \`$message.author.username\` ID: \`$message.author.id\` `)
.setColor("GRAY")
.setFooter("Dm message")
.addField("Message:", `\`\`\`$message.content\`\`\``, false);
const embed2 = new MessageEmbed()
.setTitle("New reply!")
.setAuthor(`Name: \`$message.author.username\` ID: \`$message.author.id\` `)
.setColor("GRAY")
.setFooter("Dm reply")
.addField("Reply:", `\`\`\`$message.content\`\`\``, false);
if (lastDM === `dm_$message.author.id`)
client.channels.cache.get("920895881656532992").send(`You got a reply!`, embed2)
console.log(lastDM)
else
await db.set(`dm_$message.author.id`).then(
client.channels.cache.get("920895881656532992").send(`I was DMed!`, embed1),
console.log(lastDM)
)
错误:
(node:703) UnhandledPromiseRejectionWarning: SyntaxError: Failed to parse value of dm_612110791683866644, try passing a raw option to get the raw value
at /home/runner/DwaCraft-Main-bot-Fixed/node_modules/@replit/database/index.js:36:17
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Client.get (/home/runner/DwaCraft-Main-bot-Fixed/node_modules/@replit/database/index.js:20:12)
at async Client.<anonymous> (/home/runner/DwaCraft-Main-bot-Fixed/main.js:116:18)
(node:703) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
注意:我使用的是replit's 数据库。
【问题讨论】:
【参考方案1】:我正在回答我自己的问题,因为我找到了一种方法,但我仍然不明白错误是什么,因为其他任何东西都可以正常工作,无论如何,我很快意识到我的代码真的没有,所以我修复了它,这是代码:
const MessageEmbed = require('discord.js');
const Database = require('@replit/database');
const db = new Database();
client.on('message', async message =>
//Dm checker
if (message.channel.type === 'dm')
let lastDM = await db.get(`lastDM`)
if (!lastDM) lastDM = `NONE`
if (message.author.id == client.user.id) return;
if (message.author.id == '818749579369512978') return message.channel.send("This chat has been Blacklisted by the developer (<@"+ BOT_OWNER +">)");
const embed = new MessageEmbed()
.setTitle("New message!")
.setAuthor(`Name: \`$message.author.username\` ID: \`$message.author.id\` `)
.setColor("GRAY")
.setFooter("Dm message")
.addField("Message:", `\`\`\`$message.content\`\`\``, false);
await db.set(`lastDM`, message.author.id).then(
client.channels.cache.get("920895881656532992").send(`Someone messaged me.`, embed),
console.log(lastDM)
)
并回复命令:
const MessageEmbed = require('discord.js');
const Database = require('@replit/database');
const db = new Database();
module.exports =
name: 'reply',
category: 'Owner',
description: 'Replys to last DM.',
aliases: [],
usage: 'Reply <message>',
userperms: ['BOT_OWNER'],
botperms: [],
run: async (client, message, args) =>
if (message.author.bot) return;
let lastDM = await db.get(`lastDM`);
let msg = args.slice(0).join(' ')
if (!lastDM) return message.channel.send("There was no any resent DMs.")
if (!msg) return message.channel.send("I can't send air, please state a message.")
if (!isNaN(lastDM))
client.users.cache.get(lastDM).send(msg)
.catch (err =>
return message.channel.send("Failed to send DM, as the user has blocked me or they have DMs closed!")
)
我希望我能以某种方式帮助使用 replit 数据库的人
【讨论】:
以上是关于无法使用 Discord.js 在数据库中写入和读取值的主要内容,如果未能解决你的问题,请参考以下文章
我如何让我的机器人在 .JSON 文件 discord.js 中写入多行 [重复]
TypeError:无法使用 discord.js 读取属性“”错误
使用 Node.js、Mongoose 和 Discord.js 的未定义错误 [无法读取未定义的属性]