Search code examples
discord.jsquick.db

Trouble sending DM to Message to stored user in DB discord.js


So Im making a ticket command and when you say .new it will open a channel and store message.author so it can dm you a transcript later on when you close it. When I log Console.log(message.author) and the stored person in the db, the message.author has User before the brackets and in the db it does not. Also the db has more lines while message.author has less:

MEMBER:  {
  id: '708457617013342249',
  system: false,
  locale: null,
  flags: 128,
  username: 'chuli',
  bot: false,
  discriminator: '0001',
  avatar: 'a_483669232f603ee04c099c0449e8dc6a',
  lastMessageChannelID: '829491485866065971',
  createdTimestamp: 1588979858402,
  defaultAvatarURL: 'https://cdn.discordapp.com/embed/avatars/1.png',
  tag: 'chuli#0001',
  avatarURL: 'https://cdn.discordapp.com/avatars/708457617013342249/a_483669232f603ee04c099c0449e8dc6a.webp',
  displayAvatarURL: 'https://cdn.discordapp.com/avatars/708457617013342249/a_483669232f603ee04c099c0449e8dc6a.webp'
}
MESSAGE.AUTHOR User {
  id: '708457617013342249',
  system: false,
  locale: null,
  flags: UserFlags { bitfield: 128 },
  username: 'chuli',
  bot: false,
  discriminator: '0001',
  avatar: 'a_483669232f603ee04c099c0449e8dc6a',
  lastMessageID: '842016982039134249',
  lastMessageChannelID: '841958995890667530'
}

So I get an error when trying to send to the stored db user:

 member.send(pembed).catch()
       ^                                                

TypeError: member.send is not a function

Ive been breaking my head over this so I hope someone has the answer to this out there!


Solution

  • The reason they have different amount of lines is because they are two different things. message.author is a User, but what's stored in the db is a GuildMember. What's the difference?

    The reason that message.author has User in front of it is that it's a class. What's in the db was originally a class, and would've been logged with GuildMember in front of it, however you unfortunately cannot keep classes in quick.db. Nor should you want to, as it would take up an insane amount of storage. Methods such as #send are only available on the GuildMember class (as opposed to the GuildMember class turned into an object by quick.db), so you can't access it from the db.

    Instead, store the user ID in the db, as it's a unique identifier and you can get the user from it without storing huge objects in a db. You can use client.users.fetch(id) to get the user.

    // await db.set('channel_id', 'author_id');
    
    const id = await db.get('channel_id');
    const user = await client.users.fetch(id);
    
    user.send(...);