Skip to content

Commit

Permalink
chore!: switch to rvjs v7
Browse files Browse the repository at this point in the history
some testing has ben done but I haven't tested *everything* yet so I'll likely push more fixes befor releasing any updates
  • Loading branch information
Rexogamer committed Jul 21, 2024
1 parent daf16e0 commit b57e925
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 183 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"devDependencies": {
"@types/node": "^20.14.11",
"revolt.js": "^6.0.20",
"revolt.js": "^7.0.3",
"typescript": "^5.5.3"
}
}
39 changes: 22 additions & 17 deletions packages/revchiver/src/archiveChannel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { /* Member, */ Message } from "revolt.js";
import type { Client, /* Member, */ Message } from "revolt.js";

export async function archiveChannel(msg: Message, ignoredMsgs?: Message[]) {
export async function archiveChannel(
client: Client,
msg: Message,
ignoredMsgs?: Message[]
) {
const sleep = (ms: number | undefined) =>
new Promise((r) => setTimeout(r, ms));

const autumnURL = msg.client.configuration?.features.autumn.url;
const config = client.configuration ?? (await client.api.get("/"));
const autumnURL = config.features.autumn.url;

const archiveData = {
server_id: "",
Expand All @@ -17,15 +22,15 @@ export async function archiveChannel(msg: Message, ignoredMsgs?: Message[]) {
};

// check if the script is being run by rexbot
const isRexBot = msg.client.user?._id! === "01FEEXZT74QWW1HSQH8B8BH1S1";
const isRexBot = client.user?.id! === "01FEEXZT74QWW1HSQH8B8BH1S1";

// gather info
const isServer = msg.channel?.server;
archiveData.server_id = isServer ? msg.channel.server._id : "notAServer";
archiveData.server_id = isServer ? msg.channel.serverId : "notAServer";
archiveData.server_name = isServer ? msg.channel.server.name : "notAServer";
archiveData.channel_id = msg.channel_id!;
archiveData.channel_id = msg.channelId!;
archiveData.channel_name = msg.channel?.name!;
archiveData.archiver = isRexBot ? msg.author?._id! : msg.client.user?._id!; // if the script is being run by rexbot, use the id of the author of the message; else, use the client's id
archiveData.archiver = isRexBot ? msg.authorId! : client.user?.id!; // if the script is being run by rexbot, use the id of the author of the message; else, use the client's id
archiveData.archived_at = new Date().getTime();

// fetch/push messages
Expand All @@ -39,7 +44,7 @@ export async function archiveChannel(msg: Message, ignoredMsgs?: Message[]) {

let attachmentsObj: string[] = [];
m.attachments?.forEach((a) => {
attachmentsObj.push(`${autumnURL}/attachments/${a._id}/${a.filename}`);
attachmentsObj.push(`${autumnURL}/attachments/${a.id}/${a.filename}`);
});

type Reaction = {
Expand All @@ -54,38 +59,38 @@ export async function archiveChannel(msg: Message, ignoredMsgs?: Message[]) {
});

archiveData.messages.push({
message_id: m._id,
sender_id: m.author_id,
message_id: m.id,
sender_id: m.authorId,
sender_name:
m.masquerade?.name ?? m.member?.nickname ?? m.author?.username, // order: masq > nick > username
sender_avatar: `${autumnURL}/avatars/${
m.masquerade?.avatar ?? m.member?.avatar
? `${m.member?.avatar?._id}/${m.member?.avatar?.filename}`
: `${m.author?.avatar?._id}/${m.author?.avatar?.filename}`
? `${m.member?.avatar?.id}/${m.member?.avatar?.filename}`
: `${m.author?.avatar?.id}/${m.author?.avatar?.filename}`
}`, // order: masq > server > global
content: m.content ?? m.system,
content: m.content ?? m.systemMessage,
attachments: attachmentsObj,
reactions: reactionsObj,
});
}
let continueFetching = true;
let fetchbefore = msg._id;
let fetchbefore = msg.id;
while (continueFetching) {
const msgs = await msg.channel?.fetchMessages({
limit: 100,
before: fetchbefore,
});
if (!msgs) return "nothingToArchive?";

if (fetchbefore === msg._id) {
const extraMsg = await msg.channel?.fetchMessage(msg._id);
if (fetchbefore === msg.id) {
const extraMsg = await msg.channel?.fetchMessage(msg.id);
pushMsg(extraMsg!);
}

if (msgs.length < 100) {
continueFetching = false;
} else {
fetchbefore = msgs[99]._id;
fetchbefore = msgs[99].id;
}

msgs.forEach((m) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/termivolt/src/commands/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const archive = async function (
} else {
const msg = msgs[0];
console.log(styles.info("[INFO] Starting archival process..."));
const archiveData = await archiveChannel(msg);
const archiveData = await archiveChannel(client, msg);
console.log(styles.info("[INFO] Preparing file..."));
const rawjson = JSON.stringify(archiveData, null, 4);

Expand All @@ -62,7 +62,9 @@ const archive = async function (
const json = rawjson.replace(regex, "");

// define filenames
const filename = `archive_${msg.channel_id}_${msg.createdAt}.json`;
const filename = `archive_${
msg.channelId
}_${new Date().valueOf()}.json`;
const dir = `archives/${filename}`;

const __dirname = path.resolve();
Expand Down
1 change: 0 additions & 1 deletion packages/termivolt/src/commands/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const ban = async function (
`There was an issue getting the server - is the ID correct?\nThe error was: ${error}`
)
);
client.logout();
return process.exit(1);
}

Expand Down
19 changes: 7 additions & 12 deletions packages/termivolt/src/commands/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,20 @@ const interactive = async function (
console.log(styles.info("[INFO] Fetching servers/conversations..."));

// get servers and channels
const s = client.servers;
const c = client.channels;
const s = client.servers.toList();
const c = client.channels.toList();

// create arrrays
let conversations = [] as NameIDPair[];
let conversationNames = [] as string[];

// filter through channels for DMs/groups
for (const cnl of c) {
if (
cnl[1].channel_type === "DirectMessage" ||
cnl[1].channel_type === "Group"
) {
if (cnl.type === "DirectMessage" || cnl.type === "Group") {
// use group name > DM recipient username > recipient ID
const cname =
cnl[1].name ??
cnl[1].recipient?.username ??
`Unnamed channel (${cnl[1]._id})`;
conversations.push({ name: cname, id: cnl[1]._id });
cnl.name ?? cnl.recipient?.username ?? `Unnamed channel (${cnl.id})`;
conversations.push({ name: cname, id: cnl.id });
conversationNames.push(cname);
}
}
Expand All @@ -71,8 +66,8 @@ const interactive = async function (

for (const srv of s) {
// use server name > server ID
servers.push({ name: srv[1].name, id: srv[1]._id });
serverNames.push(srv[1].name);
servers.push({ name: srv.name, id: srv.id });
serverNames.push(srv.name);
}

async function runtime() {
Expand Down
1 change: 0 additions & 1 deletion packages/termivolt/src/commands/kick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const kick = async function (
`There was an issue getting the server - is the ID correct?\nThe error was: ${error}`
)
);
client.logout();
return process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/termivolt/src/commands/rmfromgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const rmFromGroup = async function (

client.on("ready", async () => {
const channel2 = await fetchChannel(client, group);
if (channel2.channel_type !== "Group") {
if (channel2.type !== "Group") {
console.log(
styles.error(
`That doesn't seem to be a group - have you used the right ID?`
Expand Down
29 changes: 20 additions & 9 deletions packages/termivolt/src/lib/constants/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const checkIfBot = function (input: string) {
};

const login = async function (token: string, apiURL: string, userType: string) {
const client = new Client({ apiURL });
const client = new Client({ baseURL: apiURL });
const isBot = checkIfBot(userType);
if (isBot === "invalid") {
console.log(
Expand All @@ -25,7 +25,17 @@ const login = async function (token: string, apiURL: string, userType: string) {
return process.exit(1);
}
try {
isBot ? client.loginBot(token) : client.useExistingSession({ token });
if (isBot) {
await client.loginBot(token);
} else {
await client.useExistingSession({
_id: "",
token,
user_id: "",
});
client.connect();
}

return client;
} catch (error: any) {
console.log(strings.global.errors.loginFailed(error));
Expand All @@ -36,15 +46,14 @@ const login = async function (token: string, apiURL: string, userType: string) {
const fetchChannel = async function (client: Client, channel: string) {
try {
const chnl = client.channels?.get(channel);
if (chnl === undefined) throw Error;
if (chnl === undefined) throw Error(`Channel is undefined (${chnl})`);
return chnl;
} catch (error) {
console.log(
styles.error(
`There was an issue getting the channel - is the ID correct?\nThe error was: ${error}`
)
);
client.logout();
return process.exit(1);
}
};
Expand All @@ -57,11 +66,11 @@ const renderMessages = async function (
) {
// common code
function contentToSend(msg: Message) {
if (msg.content === null) {
if (msg.system === null) {
if (msg.content === "") {
if (msg.systemMessage === undefined) {
return "No content.";
} else {
return msg.system?.type;
return msg.systemMessage.type;
}
} else {
return msg.content;
Expand All @@ -71,7 +80,7 @@ const renderMessages = async function (
function messageRenderer(msg: Message) {
const content = contentToSend(msg);
return `${styles.info(
`${msg.author?.username} (${msg.author_id}) - ${msg._id} (${msg.createdAt})`
`${msg.author?.username} (${msg.authorId}) - ${msg.id} (${msg.createdAt})`
)}\n${content}`;
}

Expand All @@ -85,7 +94,9 @@ const renderMessages = async function (
if (useLegacyBehaviour)
console.log(styles.success("Sucessfully fetched messages!"));
let rawoutput = [] as string[];
const sortedMsgs = msgs.sort((msg, msg2) => msg.createdAt - msg2.createdAt);
const sortedMsgs = msgs.sort(
(msg, msg2) => msg.createdAt.valueOf() - msg2.createdAt.valueOf()
);
sortedMsgs.forEach((msg) => {
const obj = messageRenderer(msg);
rawoutput.push(obj);
Expand Down
Loading

0 comments on commit b57e925

Please sign in to comment.