async function sendMessage(
  channelId,
  content,
) {
  if (typeof content === "string") {
    content = {
      content,
    };
  }
  const channel = await cacheHandlers.get("channels", channelId);
  if (channel) {
    if (
      ![
        DiscordChannelTypes.DM,
        DiscordChannelTypes.GuildNews,
        DiscordChannelTypes.GuildText,
        DiscordChannelTypes.GuildPublicThread,
        DiscordChannelTypes.GuildPivateThread,
        DiscordChannelTypes.GuildNewsThread,
      ].includes(channel.type)
    ) {
      throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
    }
    const requiredPerms = new Set([
      "SEND_MESSAGES",
      "VIEW_CHANNEL",
    ]);
    if (content.tts) requiredPerms.add("SEND_TTS_MESSAGES");
    if (content.embed) requiredPerms.add("EMBED_LINKS");
    if (
      content.messageReference?.messageId ||
      content.allowedMentions?.repliedUser
    ) {
      requiredPerms.add("READ_MESSAGE_HISTORY");
    }
    await requireBotChannelPermissions(channelId, [
      ...requiredPerms,
    ]);
  }
  // Use ... for content length due to unicode characters and js .length handling
  if (
    content.content && !validateLength(content.content, {
      max: 2000,
    })
  ) {
    throw new Error(Errors.MESSAGE_MAX_LENGTH);
  }
  if (content.allowedMentions) {
    if (content.allowedMentions.users?.length) {
      if (
        content.allowedMentions.parse?.includes(
          DiscordAllowedMentionsTypes.UserMentions,
        )
      ) {
        content.allowedMentions.parse = content.allowedMentions.parse.filter((
          p,
        ) => p !== "users");
      }
      if (content.allowedMentions.users.length > 100) {
        content.allowedMentions.users = content.allowedMentions.users.slice(
          0,
          100,
        );
      }
    }
    if (content.allowedMentions.roles?.length) {
      if (
        content.allowedMentions.parse?.includes(
          DiscordAllowedMentionsTypes.RoleMentions,
        )
      ) {
        content.allowedMentions.parse = content.allowedMentions.parse.filter((
          p,
        ) => p !== "roles");
      }
      if (content.allowedMentions.roles.length > 100) {
        content.allowedMentions.roles = content.allowedMentions.roles.slice(
          0,
          100,
        );
      }
    }
  }
  if (content.components?.length) {
    validateComponents(content.components);
  }
  const result = await rest.runMethod(
    "post",
    endpoints.CHANNEL_MESSAGES(channelId),
    snakelize({
      ...content,
      ...content.messageReference?.messageId
        ? {
          messageReference: {
            ...content.messageReference,
            failIfNotExists: content.messageReference.failIfNotExists === true,
          },
        }
        : {},
    }),
  );
  return structures.createDiscordenoMessage(result);
}
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
Send a message to the channel.
Send a message to the channel .
async function sendMessage(
  channelId,
  content,
) {
  if (typeof content === "string") {
    content = {
      content,
    };
  }
  const channel = await cacheHandlers.get("channels", channelId);
  if (channel) {
    if (
      ![
        DiscordChannelTypes.DM,
        DiscordChannelTypes.GuildNews,
        DiscordChannelTypes.GuildText,
        DiscordChannelTypes.GuildPublicThread,
        DiscordChannelTypes.GuildPivateThread,
        DiscordChannelTypes.GuildNewsThread,
      ].includes(channel.type)
    ) {
      throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
    }
    const requiredPerms = new Set([
      "SEND_MESSAGES",
      "VIEW_CHANNEL",
    ]);
    if (content.tts) requiredPerms.add("SEND_TTS_MESSAGES");
    if (content.embed) requiredPerms.add("EMBED_LINKS");
    if (
      content.messageReference?.messageId ||
      content.allowedMentions?.repliedUser
    ) {
      requiredPerms.add("READ_MESSAGE_HISTORY");
    }
    await requireBotChannelPermissions(channelId, [
      ...requiredPerms,
    ]);
  }
  if (
    content.content && !validateLength(content.content, {
      max: 2000,
    })
  ) {
    throw new Error(Errors.MESSAGE_MAX_LENGTH);
  }
  if (content.allowedMentions) {
    if (content.allowedMentions.users?.length) {
      if (
        content.allowedMentions.parse?.includes(
          DiscordAllowedMentionsTypes.UserMentions,
        )
      ) {
        content.allowedMentions.parse = content.allowedMentions.parse.filter((
          p,
        ) => p !== "users");
      }
      if (content.allowedMentions.users.length > 100) {
        content.allowedMentions.users = content.allowedMentions.users.slice(
          0,
          100,
        );
      }
    }
    if (content.allowedMentions.roles?.length) {
      if (
        content.allowedMentions.parse?.includes(
          DiscordAllowedMentionsTypes.RoleMentions,
        )
      ) {
        content.allowedMentions.parse = content.allowedMentions.parse.filter((
          p,
        ) => p !== "roles");
      }
      if (content.allowedMentions.roles.length > 100) {
        content.allowedMentions.roles = content.allowedMentions.roles.slice(
          0,
          100,
        );
      }
    }
  }
  if (content.components?.length) {
    validateComponents(content.components);
  }
  const result = await rest.runMethod(
    "post",
    endpoints.CHANNEL_MESSAGES(channelId),
    snakelize({
      ...content,
      ...content.messageReference?.messageId
        ? {
          messageReference: {
            ...content.messageReference,
            failIfNotExists: content.messageReference.failIfNotExists === true,
          },
        }
        : {},
    }),
  );
  return structures.createDiscordenoMessage(result);
}
async function sendMessage ( channelId , content , ) { if ( typeof content === "string" ) { content = { content , } ; } const channel = await cacheHandlers . get ( "channels" , channelId ) ; if ( channel ) { if ( ! [ DiscordChannelTypes . DM , DiscordChannelTypes . GuildNews , DiscordChannelTypes . GuildText , DiscordChannelTypes . GuildPublicThread , DiscordChannelTypes . GuildPivateThread , DiscordChannelTypes . GuildNewsThread , ] . includes ( channel . type ) ) { throw new Error ( Errors . CHANNEL_NOT_TEXT_BASED ) ; } const requiredPerms = new Set ( [ "SEND_MESSAGES" , "VIEW_CHANNEL" , ] ) ; if ( content . tts ) requiredPerms . add ( "SEND_TTS_MESSAGES" ) ; if ( content . embed ) requiredPerms . add ( "EMBED_LINKS" ) ; if ( content . messageReference ?. messageId || content . allowedMentions ?. repliedUser ) { requiredPerms . add ( "READ_MESSAGE_HISTORY" ) ; } await requireBotChannelPermissions ( channelId , [ ... requiredPerms , ] ) ; } if ( content . content && ! validateLength ( content . content , { max : 2000 , } ) ) { throw new Error ( Errors . MESSAGE_MAX_LENGTH ) ; } if ( content . allowedMentions ) { if ( content . allowedMentions . users ?. length ) { if ( content . allowedMentions . parse ?. includes ( DiscordAllowedMentionsTypes . UserMentions , ) ) { content . allowedMentions . parse = content . allowedMentions . parse . filter ( ( p , ) => p !== "users" ) ; } if ( content . allowedMentions . users . length > 100 ) { content . allowedMentions . users = content . allowedMentions . users . slice ( 0 , 100 , ) ; } } if ( content . allowedMentions . roles ?. length ) { if ( content . allowedMentions . parse ?. includes ( DiscordAllowedMentionsTypes . RoleMentions , ) ) { content . allowedMentions . parse = content . allowedMentions . parse . filter ( ( p , ) => p !== "roles" ) ; } if ( content . allowedMentions . roles . length > 100 ) { content . allowedMentions . roles = content . allowedMentions . roles . slice ( 0 , 100 , ) ; } } } if ( content . components ?. length ) { validateComponents ( content . components ) ; } const result = await rest . runMethod ( "post" , endpoints . CHANNEL_MESSAGES ( channelId ) , snakelize ( { ... content , ... content . messageReference ?. messageId ? { messageReference : { ... content . messageReference , failIfNotExists : content . messageReference . failIfNotExists === true , } , } : { } , } ) , ) ; return structures . createDiscordenoMessage ( result ) ; }
Send a message to the channel.
Send a message to the channel .
// Use ... for content length due to unicode characters and js .length handling