WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.23k stars 390 forks source link

TypeError: db.get is not a function #1152

Closed tanjiro-habshi closed 4 months ago

tanjiro-habshi commented 4 months ago

Hi i coded a discord bot's command but it doesnt seem to work because of this error TypeError: db.get is not a function my code for index.js to send and randomize pfps: client.on("ready", async () => { const randompfp = async () => { try { const avatar = client.users.cache.filter(user => user.avatarURL()).random().avatarURL({ size: 1024, dynamic: true });

  const channels = client.guilds.cache.map(guild => db.get(`pfpchannel_${guild.id}`)).filter(Boolean);

  for (const chanId of channels) {
    try {
      const channel = await client.channels.fetch(chanId);
      const embed = new MessageEmbed()
        .setTitle(`${client.user.username} pfps`)
        .setColor("2f3136")
        .setImage(avatar);
      channel.send({ embeds: [embed] });
    } catch (error) {
      console.error(`Error sending pfp to channel ${chanId}:`, error);
    }
  }
} catch (error) {
  console.error("Error generating random pfp:", error);
}

};

setInterval(randompfp, 20 * 1000); // Adjust the interval as needed });

my code for autopfp set and clear through users: const Discord = require("discord.js"); const db = require("quick.db"); const config = require("../../config") const { warn } = require('../../emojis.json') const { approve } = require('../../emojis.json') const { mark } = require('../../emojis.json') const { green } = require('../../color.json') const { red } = require('../../color.json') const { yellow } = require('../../color.json') const { PREFIX } = require('../../config');

module.exports = { config: { name: "autopfp", aliases: ["autopfp", "ap", "af"] }, run: async (client, message, args) => { const developers = ['984093615510933544', '190287212615696384'];

// Check if the message author is a developer
if (!developers.includes(message.author.id)) {
  return message.reply('Premium command');
}

// Developer-only section
if (developers.includes(message.author.id)) {
  // Split the message content into arguments
  const args1 = message.content.trim().split(/ +/g);

  const logsEmbed = new Discord.MessageEmbed()
    .setTitle("Command: autopfp")
    .setColor(red)
    .setAuthor(message.author.username, message.author.avatarURL({ dynamic: true }))
    .setDescription(`
      \`\`\`
      -  []  =  optional argument 
      +  <>  =  required argument
      \`\`\`
      \`\`\`
      > autopfp set
      > autopfp clear
      \`\`\`
    `)
    .setThumbnail(client.user.displayAvatarURL({ size: 256, dynamic: true }));

  // Get the command argument
  let channel2 = args[0];

  if (!channel2) {
    // Send the logsEmbed if no argument is provided
    return message.channel.send({ embeds: [logsEmbed] });
  }

  if (args[0] === "clear") {
    // Clear autopfp channel
    db.delete(`pfpchannel_${message.guild.id}`);
    return message.channel.send({
      embeds: [
        new Discord.MessageEmbed()
          .setDescription(`<:OnlineStatus:1029221207905665064>The autopfp has been reset`)
          .setColor(red)
      ],
    });
  }

  if (args[0] === "set") {
    // Set autopfp channel
    let channel = message.mentions.channels.first();

    const fetched = db.get(`pfpchannel_${message.guild.id}`);
    if (fetched) {
      return message.channel.send({
        embeds: [
          new Discord.MessageEmbed()
            .setDescription(`<:Idle:1029222036012273714>There is already an autopfp channel set`)
            .setColor(red)
        ],
      });
    }

    if (!channel) {
      return message.channel.send({
        embeds: [
          new Discord.MessageEmbed()
            .setDescription(`<:Idle:1029222036012273714>Invalid syntax. try ${botprefix}autopfp set #channel`)
            .setColor(red)
        ],
      });
    }

    // Save autopfp channel to the database
    db.set(`pfpchannel_${message.guild.id}`, channel.id);
    return message.channel.send({
      embeds: [
        new Discord.MessageEmbed()
          .setDescription(`<:OnlineStatus:1029221207905665064> autopfp channel has been set to ${channel}`)
          .setColor(red)
      ],
    });
  }
}

} };

Prinzhorn commented 4 months ago

You can't just throw (poorly formatted) code in here and expect someone else to fix bugs for you. I'm certain that this is not a bug in better-sqlite3 but a user error. For all I know your code doesn't even use better-sqlite3 at all.

I want to point out that the Database class does indeed not have a get method. Assuming db even is an instance of Database, how would I know?

tanjiro-habshi commented 4 months ago

This code was working before even without better-sqlite3 but I don't know it has suddenly stopped working however i checked and browsed through docs.

neoxpert commented 4 months ago

What do you mean by "This code was working before even without better-sqlite3"?

As far as I understand quick.db, the package you are importing db from, is more or less a wrapper for several different database drivers, that just recommends better-sqlite3 as a default.

And if you are really just doing this

const db = require("quick.db");
...
db.get(...)

This call to get will throw an expected error, as "db" is not a usable instance of a database. As shown within the minimal example in the quick.db repo you have to create a new quick.db instance

const { QuickDB } = require("quick.db");
const db = new QuickDB(); 
...
tanjiro-habshi commented 4 months ago

Turns out my code wasn't optimized for quick.db, I fixed it now.

On Fri, Mar 1, 2024, 1:03 AM neoxpert @.***> wrote:

What do you mean by "This code was working before even without better-sqlite3"?

As far as I understand quick.db https://github.com/plexidev/quick.db, the package you are importing db from, is more or less a wrapper for several different database drivers, that just recommends better-sqlite3 as a default.

And if you are really just doing this

const db = require("quick.db"); ...db.get(...)

This call to get will throw an expected error, as "db" is not a usable instance of a database. As shown within the minimal example in the quick.db repo you have to create a new quick.db instance

const { QuickDB } = require("quick.db");const db = new QuickDB(); ...

— Reply to this email directly, view it on GitHub https://github.com/WiseLibs/better-sqlite3/issues/1152#issuecomment-1971867356, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXGHKJ2IWLALMXFHYXYJWVLYV6EQPAVCNFSM6AAAAABD5OBY7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZRHA3DOMZVGY . You are receiving this because you authored the thread.Message ID: @.***>