1

I'm trying to make a command that sends a message to every channel id stored using quick.db

db.set(`channelID_${message.guıld.ıd}`, message.channel.id)

so this is how i would store channel id is there a possible way to send a message to every channelID stored using this i have been trying and failed

EDIT: I tried to do it using

const channelIDs = db.all() 
  .filter(e => e.ID.startsWith("channeltest"));

channelIDs.forEach(async (id) => {
  try {
    
    const channel = await client.channels.fetch(id);
    channel.send('test');
  }
});

getting error channel_id: Value "[object Object]" is not snowflake.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Meme Loke
  • 97
  • 1
  • 9

2 Answers2

0

First of all I think you have a little typo in your code. You set a key but no data. Here is my correction:

db.set(`channelID_${message.guıld.ıd}`, message.channel.id)

I would recommend using a Array.forEach() loop through your database entries of channel IDs:

array1.forEach(async databaseElement => {
const fetchedChannel = await client.channels.fetch('ENTER CHANNEL ID');
fetchedChannel.send('Test message. It worked!')
});
Gilles Heinesch
  • 2,889
  • 1
  • 22
  • 43
0

You should probably set an array of channel IDs as Worthy Alpaca mentioned.

db.set(`channelID_${message.guild.id}`, ['859324671543153', '859320171505419'])

You can add new IDs later using the push() method:

db.push(`channelID_${message.guild.id}`, message.channel.id)

Once you've got your db set up, you can get the array of IDs using the .get() method and iterate over it using the .forEach() method. For every channel ID, you can use .fetch() to get the channel. It returns a promise, so you'll need to resolve it first (that's why there is that await keyword). Once you got a channel, you can use the channel.send() method to send a message to that channel:

// get the array of IDs from the database
const channelIDs = db.get(`channelID_${message.guild.id}`);

// for every ID stored, send a message
channelIDs.forEach(async (id) => {
  try {
    // fetch the channel by its ID
    const channel = await client.channels.fetch(id);
    channel.send('Minus officiis dolore eveniet');
  } catch (error) {
    console.log(error);
  }
});
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • i get error TypeError: Cannot read property 'forEach' of null i'm editing my question with a way i tried to do and failed – Meme Loke Apr 06 '21 at 16:42
  • I'm not sure why you use `db.all()` and also not sure what and how you store in your database. It doesn't look like you store channel IDs in it. – Zsolt Meszaros Apr 06 '21 at 17:26