NodeJS 如何向数据库中存储的每个通道发送消息id

bvn4nwqk  于 2022-11-03  发布在  Node.js
关注(0)|答案(1)|浏览(187)

我想发送一个消息到每个频道/频道ID从每个公会是在我的数据库这里是我尝试和谢谢!

const { SlashCommandBuilder, PermissionFlagsBits, Client,} = require("discord.js");
const db = require("../../Structures/models/guild");
const mongoose = require("mongoose");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("post")
    .setDescription("Post Something")
    .setDefaultMemberPermissions(PermissionFlagsBits.SendMessages),

  async execute(interaction, client) {
    const channelids = mongoose.get(`channelID_${interaction.guild.id}`);

    channelids.forEach(async (id) => {
      try {
        const channel = await client.channels.fetch(id);
        channel.send('Something');
      } catch (error) {
        console.log(error);
      }
    });

    await interaction.reply({content: 'Brwww'})
  },
};

错误
C:\用户\maako\OneDrive\文档\DiscordJS\Bumps\节点模块\mongoose\库\索引.js:223
抛出新错误(\ ${key}“'是无效选项。”');
错误:channelID_1032246238126542950是无效的选项。

bttbmeg0

bttbmeg01#

试试看:

const { SlashCommandBuilder, PermissionFlagsBits, Client,} = require("discord.js");
    const db = require("../../Structures/models/guild");
    const mongoose = require("mongoose");

    module.exports = {
      data: new SlashCommandBuilder()
        .setName("post")
        .setDescription("Post Something")
        .setDefaultMemberPermissions(PermissionFlagsBits.SendMessages),

      async execute(interaction, client) {
        const channelids = mongoose.get(`channelID_${interaction.guild.id}`);

        for (const channelid of channelids) {
          try {
            const channel = await client.channels.cache.get(channelid);
            channel.send('Something');
          } catch (error) {
            console.log(error);
          }
        }

        await interaction.reply({content: 'Brwww'})
      },
    };

您将执行channelid的for const channelid,以便它将所有id分隔为1,然后您将执行channelid的cache.get,一旦获得它,它将在该通道中发送一条消息。

相关问题