discord | discord api | discord channels | Search

The Discord API functions provided allow you to send messages, delete messages, update messages, get pinned messages, pin and unpin messages, and provide a convenient interface for authenticated requests to the Discord API. The functions are available for import and use in other parts of the application through the module.exports object.

Run example

npm run import -- "discord messages"

discord messages


var {DEFAULT_CHANNEL} = importer.import("discord configuration")
const {requestAuthQ} = importer.import("discord request")

async function createMessage(message, channelId = DEFAULT_CHANNEL) {
  var params = typeof message == 'string' ? ({
    'content': message
  }) : message
  return await requestAuthQ({
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'POST',
    url: `channels/${channelId}/messages`,
    data: JSON.stringify(params)
  })
}

async function deleteMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/messages/${messageId}`
  })
}

async function updateMessage(message, messageId, channelId = DEFAULT_CHANNEL) {
  var params = typeof message == 'string' ? ({
    'content': message
  }) : message
  return await requestAuthQ({
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'PATCH',
    url: `channels/${channelId}/messages/${messageId}`,
    data: JSON.stringify(params)
  })
}

async function getPins(channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'GET',
    url: `channels/${channelId}/pins`
  })
}

async function pinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'PUT',
    url: `channels/${channelId}/pins/${messageId}`
  })
}

async function unpinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/pins/${messageId}`
  })
}

module.exports = {
  createMessage,
  deleteMessage,
  updateMessage,
  getPins,
  pinMessage,
  unpinMessage
}


What the code could have been:

// Import required modules and constants
const { DEFAULT_CHANNEL, IMPORTER_CONFIG } = require('./config');
const { requestAuthQ } = require('./request');

/**
 * Creates a new message in a Discord channel.
 * @param {string|object} message - The content of the message. Can be a string or an object with 'content' property.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the message will be created.
 * @returns {Promise} The created message.
 */
async function createMessage(message, channelId = DEFAULT_CHANNEL) {
  const params = typeof message ==='string'? { content: message } : message;
  return await requestAuthQ({
    method: 'POST',
    url: `channels/${channelId}/messages`,
    headers: {
      'Content-Type': 'application/json',
    },
    data: JSON.stringify(params),
  });
}

/**
 * Deletes a message from a Discord channel.
 * @param {string} messageId - The ID of the message to delete.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the message is located.
 * @returns {Promise} The deleted message.
 */
async function deleteMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/messages/${messageId}`,
  });
}

/**
 * Edits a message in a Discord channel.
 * @param {string|object} message - The new content of the message. Can be a string or an object with 'content' property.
 * @param {string} messageId - The ID of the message to edit.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the message is located.
 * @returns {Promise} The updated message.
 */
async function updateMessage(message, messageId, channelId = DEFAULT_CHANNEL) {
  const params = typeof message ==='string'? { content: message } : message;
  return await requestAuthQ({
    method: 'PATCH',
    url: `channels/${channelId}/messages/${messageId}`,
    headers: {
      'Content-Type': 'application/json',
    },
    data: JSON.stringify(params),
  });
}

/**
 * Retrieves a list of pinned messages from a Discord channel.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel to retrieve pinned messages from.
 * @returns {Promise} A list of pinned messages.
 */
async function getPins(channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'GET',
    url: `channels/${channelId}/pins`,
  });
}

/**
 * Pins a message in a Discord channel.
 * @param {string} messageId - The ID of the message to pin.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the message is located.
 * @returns {Promise} The pinned message.
 */
async function pinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'PUT',
    url: `channels/${channelId}/pins/${messageId}`,
  });
}

/**
 * Unpins a message from a Discord channel.
 * @param {string} messageId - The ID of the message to unpin.
 * @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the message is located.
 * @returns {Promise} The unpin operation result.
 */
async function unpinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/pins/${messageId}`,
  });
}

module.exports = {
  createMessage,
  deleteMessage,
  updateMessage,
  getPins,
  pinMessage,
  unpinMessage,
};

Discord API Functions

Importing Configuration

var {DEFAULT_CHANNEL} = importer.import('discord configuration')
const {requestAuthQ} = importer.import('discord request')

Configuration Variables

  • DEFAULT_CHANNEL: Channel ID used as the default value for various functions

Request Function

  • requestAuthQ: A function that handles authenticated requests

Discord API Functions

Create Message

async function createMessage(message, channelId = DEFAULT_CHANNEL) {
  var params = typeof message =='string'? ({
    'content': message
  }) : message
  return await requestAuthQ({
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'POST',
    url: `channels/${channelId}/messages`,
    data: JSON.stringify(params)
  })
}
  • message: The message content to be sent
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the created message data

Delete Message

async function deleteMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/messages/${messageId}`
  })
}
  • messageId: The ID of the message to be deleted
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the deleted message data

Update Message

async function updateMessage(message, messageId, channelId = DEFAULT_CHANNEL) {
  var params = typeof message =='string'? ({
    'content': message
  }) : message
  return await requestAuthQ({
    headers: {
      'Content-Type': 'application/json'
    },
    method: 'PATCH',
    url: `channels/${channelId}/messages/${messageId}`,
    data: JSON.stringify(params)
  })
}
  • message: The updated message content
  • messageId: The ID of the message to be updated
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the updated message data

Get Pins

async function getPins(channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'GET',
    url: `channels/${channelId}/pins`
  })
}
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the list of pinned messages

Pin Message

async function pinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'PUT',
    url: `channels/${channelId}/pins/${messageId}`
  })
}
  • messageId: The ID of the message to be pinned
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the pinned message data

Unpin Message

async function unpinMessage(messageId, channelId = DEFAULT_CHANNEL) {
  return await requestAuthQ({
    method: 'DELETE',
    url: `channels/${channelId}/pins/${messageId}`
  })
}
  • messageId: The ID of the message to be unpinned
  • channelId: Optional, defaulting to DEFAULT_CHANNEL (string)
  • Returns a promise that resolves with the unpinned message data

Exported Functions

module.exports = {
  createMessage,
  deleteMessage,
  updateMessage,
  getPins,
  pinMessage,
  unpinMessage
}

These functions are available for import and use in other parts of the application.