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.
npm run import -- "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
}
// 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
Discord API Functions
var {DEFAULT_CHANNEL} = importer.import('discord configuration')
const {requestAuthQ} = importer.import('discord request')
DEFAULT_CHANNEL
: Channel ID used as the default value for various functionsrequestAuthQ
: A function that handles authenticated requestsasync 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 sentchannelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)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 deletedchannelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)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 contentmessageId
: The ID of the message to be updatedchannelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)async function getPins(channelId = DEFAULT_CHANNEL) {
return await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/pins`
})
}
channelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)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 pinnedchannelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)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 unpinnedchannelId
: Optional, defaulting to DEFAULT_CHANNEL
(string)module.exports = {
createMessage,
deleteMessage,
updateMessage,
getPins,
pinMessage,
unpinMessage
}
These functions are available for import and use in other parts of the application.