How to get server id and channel id in discord.js using command?

Viewed 30925

I was trying to save guild id and channel id using one command, using lowdb but I when I'm using var server = client.guilds.get(message.guild.id).id; I get error that says, Cannot read property 'get' of undefined. And I don't know why.

var server = client.guilds.get(message.guild.id).id;
var channel = client.guilds.get(message.channel.id).id;

db.get('posts')
  .push({ id: server, title: channel})
  .write();

There's all code I made, and I define everything on start of code

const client = require('<there's path but I don't want to show it cuz there's my name>/my_bot.js');
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

If you know what's wrong please help

2 Answers

If you're using discord.js@v12 then you should remember that you should go through the .cache property for your code to work. You can check that by yourself by looking at the docs for Client.guilds.
Also, you don't need to get the channel and the guild from their IDs, since you already have them stored as message properties.
Here's how I would do it:

let server = message.guild.id, // ID of the guild the message was sent in
  channel = message.channel.id // ID of the channel the message was sent in

To be fair, I wouldn't be able to explain why you get that error.

Ok so you got "get" is not a function because in v12 it has to be client.guilds.cache.get(whatever you wanna put here)

Related