-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Notes This WIP PR focuses on building a channel archiver function into Valkyrie. So that you can easily dump channel logs from a specific channel at point of archive. Due to Discord APIs message rate limit of 100 messages, we run a loop depending on the total number of messages in the channel, then combine into a file which is sent back to the channel. ### WIP This is just the initial test setup of this, some things we need to add still are: - Better storage for the archival message file (dotenv is temp) - Message headers specifying date of archive, channel name, etc - Permission flags, access to archive - Introduction of a command, valkyrie interface, etc
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Robot } from "hubot" | ||
import { | ||
Client, | ||
AttachmentBuilder, | ||
TextChannel, | ||
Message, | ||
Collection, | ||
} from "discord.js" | ||
import { writeFile, unlink } from "fs/promises" | ||
import dotenv from "dotenv" | ||
|
||
dotenv.config() | ||
|
||
async function fetchAllMessages( | ||
channel: TextChannel, | ||
before?: string, | ||
): Promise<Collection<string, Message<true>>> { | ||
const limit = 100 | ||
const options = before ? { limit, before } : { limit } | ||
const fetchedMessages = await channel.messages.fetch(options) | ||
|
||
if (fetchedMessages.size === 0) { | ||
return new Collection<string, Message<true>>() | ||
} | ||
|
||
const lastId = fetchedMessages.lastKey() | ||
const olderMessages = await fetchAllMessages(channel, lastId) | ||
|
||
return new Collection<string, Message<true>>().concat( | ||
fetchedMessages, | ||
olderMessages, | ||
) | ||
} | ||
|
||
export default async function archiveChannel( | ||
discordClient: Client, | ||
robot: Robot, | ||
) { | ||
const { application } = discordClient | ||
|
||
if (application) { | ||
// Dump all messages from a channel into an array after "!archive" | ||
discordClient.on("messageCreate", async (message) => { | ||
if ( | ||
message.content.toLowerCase() === "!archive" && | ||
message.channel instanceof TextChannel | ||
) { | ||
try { | ||
const allMessages = await fetchAllMessages(message.channel) | ||
|
||
robot.logger.info(`Total messages fetched: ${allMessages.size}`) | ||
|
||
const messagesArray = Array.from(allMessages.values()).reverse() | ||
const messagesContent = messagesArray | ||
.map( | ||
(m) => | ||
`${m.createdAt.toLocaleString()}: ${m.author.username}: ${ | ||
m.content | ||
}`, | ||
) | ||
.join("\n") | ||
|
||
const filePath = "./messages.txt" | ||
await writeFile(filePath, messagesContent, "utf-8") | ||
|
||
const fileAttachment = new AttachmentBuilder(filePath) | ||
await message.channel | ||
.send({ | ||
content: "Here are the archived messages:", | ||
files: [fileAttachment], | ||
}) | ||
.then(() => unlink(filePath)) | ||
.catch(robot.logger.error) | ||
} catch (error) { | ||
robot.logger.error(`An error occurred: ${error}`) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters