1

我正在尝试创建一个命令,向使用 quick.db 存储的每个通道 id 发送消息

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

所以这就是我存储频道ID的方式有没有一种可能的方法可以向使用此存储的每个频道ID发送消息我一直在尝试但失败了

编辑:我尝试使用

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

获取错误 channel_id:值“[object Object]”不是雪花。

4

2 回答 2

0

首先,我认为您的代码中有一些错字。您设置了密钥但没有数据。这是我的更正:

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

我建议Array.forEach()在您的频道 ID 数据库条目中使用循环:

array1.forEach(async databaseElement => {
const fetchedChannel = await client.channels.fetch('ENTER CHANNEL ID');
fetchedChannel.send('Test message. It worked!')
});
于 2021-04-06T15:32:45.913 回答
0

正如 Worthy Alpaca 所提到的,您可能应该设置一组频道 ID。

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

您可以稍后使用以下push()方法添加新 ID:

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

设置好数据库后,您可以使用该方法获取 ID 数组并使用该.get()方法对其进行迭代.forEach()。对于每个频道 ID,您可以使用.fetch()来获取频道。它返回一个承诺,所以你需要先解决它(这就是为什么有那个await关键字)。获得频道后,您可以使用该channel.send()方法向该频道发送消息:

// 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);
  }
});
于 2021-04-06T16:05:46.687 回答