This module provides a set of functions for interacting with Discord channels, including retrieving user and guild channels, fetching messages from a channel with pagination, deleting a channel, and exporting these functions for use in other modules. The functions utilize a request authentication function requestAuthQ
and configuration variables imported from separate modules to perform their respective operations.
npm run import -- "discord channels"
var {
DEFAULT_GUILD, DEFAULT_CHANNEL, MESSAGE_TIME,
MESSAGES_START,
} = importer.import("discord configuration")
const {requestAuthQ} = importer.import("discord request")
async function userChannels(userId = '@me') {
return await requestAuthQ({
method: 'GET',
url: `channels/${userId}`
})
}
async function guildChannels(guildId = DEFAULT_GUILD) {
return await requestAuthQ({
method: 'GET',
url: `guilds/${guildId}/channels`
})
}
async function channelMessagesB(channelId = DEFAULT_CHANNEL, messageTime = MESSAGE_TIME) {
var params = {
limit: 100,
after: messageTime.toString()
};
var messages = await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/messages`,
params
})
if(messages.length == 100) {
messages = messages.concat(await channelMessagesB(channelId, BigInt(messages[0].id) + BigInt(1)))
}
return messages
}
async function channelMessages(channelId = DEFAULT_CHANNEL, messageTime = MESSAGE_TIME) {
var params = {
limit: 100,
after: (BigInt(Date.now() - MESSAGES_START - messageTime) << BigInt(22)).toString()
};
var messages = await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/messages`,
params
})
if(messages.length == 100) {
messages = messages.concat(await channelMessagesB(channelId, BigInt(messages[0].id) + BigInt(1)))
}
return messages
}
async function deleteChannel(channelId) {
return await requestAuthQ({
method: 'DELETE',
url: `channels/${channelId}`
})
}
module.exports = {
userChannels,
guildChannels,
channelMessages,
deleteChannel
}
import {
DEFAULT_GUILD,
DEFAULT_CHANNEL,
MESSAGE_TIME,
MESSAGES_START,
} from 'discord configuration';
import { requestAuthQ } from 'discord request';
/**
* Retrieves a list of channels for a given user.
*
* @param {string} [userId=@me] The user ID to fetch channels for, defaults to '@me' if not provided.
* @returns {Promise
var {
DEFAULT_GUILD, DEFAULT_CHANNEL, MESSAGE_TIME,
MESSAGES_START,
} = importer.import('discord configuration')
const {requestAuthQ} = importer.import('discord request')
Imports configuration variables and a request function from separate modules.
async function userChannels(userId = '@me') {
return await requestAuthQ({
method: 'GET',
url: `channels/${userId}`
})
}
Retrieves user channels for the specified userId
or '@me' by default.
async function guildChannels(guildId = DEFAULT_GUILD) {
return await requestAuthQ({
method: 'GET',
url: `guilds/${guildId}/channels`
})
}
Retrieves guild channels for the specified guildId
or the default guild by default.
async function channelMessagesB(channelId = DEFAULT_CHANNEL, messageTime = MESSAGE_TIME) {
var params = {
limit: 100,
after: messageTime.toString()
};
var messages = await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/messages`,
params
})
if(messages.length == 100) {
messages = messages.concat(await channelMessagesB(channelId, BigInt(messages[0].id) + BigInt(1)))
}
return messages
}
Retrieves 100 messages from the specified channelId
at the specified messageTime
, recursively fetching more messages until the end is reached.
async function channelMessages(channelId = DEFAULT_CHANNEL, messageTime = MESSAGE_TIME) {
var params = {
limit: 100,
after: (BigInt(Date.now() - MESSAGES_START - messageTime) << BigInt(22)).toString()
};
var messages = await requestAuthQ({
method: 'GET',
url: `channels/${channelId}/messages`,
params
})
if(messages.length == 100) {
messages = messages.concat(await channelMessagesB(channelId, BigInt(messages[0].id) + BigInt(1)))
}
return messages
}
Retrieves 100 messages from the specified channelId
with a timestamp in the past by the specified messageTime
, using a similar approach to channelMessagesB
but with a different timestamp calculation.
async function deleteChannel(channelId) {
return await requestAuthQ({
method: 'DELETE',
url: `channels/${channelId}`
})
}
Deletes the specified channelId
using a DELETE request.
module.exports = {
userChannels,
guildChannels,
channelMessages,
deleteChannel
}
Exports the functions for use in other modules.