Unable to write and read values in a database, using Discord.js

Viewed 181

I'm trying to allow staff to reply to the last DM using ~reply <message> rather than ~dm <userID> <message>, but firstly, I have to save the user's ID in a database to know who was the last DM user, but I'm facing an ERROR here, I'm confused of why this may be happening, but please note, I'm really new to databases, I'd apricate some help!

My code (Not all, just what I'm using for the command.):

I added ">>" before the line the ERROR is.

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)
        )
      }
    }

The ERROR:

(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)

Note: I'm using replit's databases.

1 Answers

I'm answering my own question as I found a way to do it, but I still don't get what the Error is as anything else works fine, anyway, I soon realized that my code really doesn't make since, so I fixed it, here is the code:

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)
    )
  }

And reply command:

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!")
      })
    }
  }
}

I wish I somehow helped someone that uses replit's databases

Related