This JavaScript module provides four Discord-related functions: createThread
, archivedThreads
, activeThreads
, and addThreadMember
, which can be used to interact with threads in a Discord channel. The functions are exported as a single object and can be imported and used in other modules.
npm run import -- "discord threads"
var {DEFAULT_CHANNEL} = importer.import("discord configuration")
const {requestAuthQ} = importer.import("discord request")
async function createThread(name, channelId = DEFAULT_CHANNEL) {
var json = {
'name': name,
'type': 11,
'auto_archive_duration': 60
}
return await requestAuthQ({
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
url: `channels/${channelId}/threads`,
data: JSON.stringify(json)
})
}
async function archivedThreads(channelId = DEFAULT_CHANNEL) {
return await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/threads/archived/public`
})
}
async function activeThreads(channelId = DEFAULT_CHANNEL) {
return await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/threads/active`
})
}
async function addThreadMember(memberId, channelId) {
return await requestAuthQ({
method: 'PUT',
url: `/channels/${channelId}/thread-members/${memberId}`
})
}
module.exports = {
createThread,
archivedThreads,
activeThreads,
addThreadMember,
}
// Import required modules and configurations
const { DEFAULT_CHANNEL } = require('./discord-configuration');
const { requestAuthQ } = require('./discord-request');
// Define a function to create a new thread
/**
* Creates a new thread in a specified Discord channel.
*
* @param {string} name - The name of the thread.
* @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the thread will be created.
* @returns {Promise<object>} A promise that resolves to the created thread object.
*/
async function createThread(name, channelId = DEFAULT_CHANNEL) {
// Define the thread data to be sent in the request body
const threadData = {
name,
type: 11,
auto_archive_duration: 60,
};
// Use the requestAuthQ function to send a POST request to create the thread
return await requestAuthQ({
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
url: `/channels/${channelId}/threads`,
data: JSON.stringify(threadData),
});
}
// Define a function to retrieve archived threads
/**
* Retrieves a list of archived threads in a specified Discord channel.
*
* @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the archived threads will be retrieved from.
* @returns {Promise<object>} A promise that resolves to the list of archived thread objects.
*/
async function archivedThreads(channelId = DEFAULT_CHANNEL) {
// Use the requestAuthQ function to send a GET request to retrieve the archived threads
return await requestAuthQ({
method: 'GET',
url: `/channels/${channelId}/threads/archived/public`,
});
}
// Define a function to retrieve active threads
/**
* Retrieves a list of active threads in a specified Discord channel.
*
* @param {string} [channelId=DEFAULT_CHANNEL] - The ID of the channel where the active threads will be retrieved from.
* @returns {Promise<object>} A promise that resolves to the list of active thread objects.
*/
async function activeThreads(channelId = DEFAULT_CHANNEL) {
// Use the requestAuthQ function to send a GET request to retrieve the active threads
return await requestAuthQ({
method: 'GET',
url: `/channels/${channelId}/threads/active`,
});
}
// Define a function to add a member to a thread
/**
* Adds a member to a thread in a specified Discord channel.
*
* @param {string} memberId - The ID of the member to be added to the thread.
* @param {string} channelId - The ID of the channel where the thread is located.
* @returns {Promise<object>} A promise that resolves to the updated thread object.
*/
async function addThreadMember(memberId, channelId) {
// Use the requestAuthQ function to send a PUT request to add the member to the thread
return await requestAuthQ({
method: 'PUT',
url: `/channels/${channelId}/thread-members/${memberId}`,
});
}
// Export the functions
module.exports = {
createThread,
archivedThreads,
activeThreads,
addThreadMember,
};
var {DEFAULT_CHANNEL} = importer.import('discord configuration')
const {requestAuthQ} = importer.import('discord request')
DEFAULT_CHANNEL
and requestAuthQ
.DEFAULT_CHANNEL
is assumed to be a string representing a default channel ID.requestAuthQ
is a function that makes authenticated HTTP requests.async function createThread(name, channelId = DEFAULT_CHANNEL) {
//...
}
createThread
function creates a new thread in a specified Discord channel.name
(the name of the thread) and channelId
(the ID of the channel where the thread will be created).channelId
is not provided, it defaults to the DEFAULT_CHANNEL
.async function archivedThreads(channelId = DEFAULT_CHANNEL) {
//...
}
archivedThreads
function retrieves a list of archived threads in a specified Discord channel.channelId
(the ID of the channel to retrieve archived threads from).channelId
is not provided, it defaults to the DEFAULT_CHANNEL
.async function activeThreads(channelId = DEFAULT_CHANNEL) {
//...
}
activeThreads
function retrieves a list of active threads in a specified Discord channel.channelId
(the ID of the channel to retrieve active threads from).channelId
is not provided, it defaults to the DEFAULT_CHANNEL
.async function addThreadMember(memberId, channelId) {
//...
}
addThreadMember
function adds a member to a specified thread in a Discord channel.memberId
(the ID of the member to add) and channelId
(the ID of the channel where the thread is located).module.exports = {
createThread,
archivedThreads,
activeThreads,
addThreadMember,
}