Added LastActive (#224)

* Added LastActive

LastActive is a plugin that fetches your messages to jump to them.
(https://discord.com/channels/1173279886065029291/1359354001560178872)

* Added to README

* Fix Count

---------

Co-authored-by: thororen1234 <78185467+thororen1234@users.noreply.github.com>
This commit is contained in:
Crxaw 2025-04-09 13:27:05 +01:00 committed by GitHub
parent 04e6a9d3ac
commit f6f0e60b7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 148 additions and 1 deletions

View file

@ -11,7 +11,7 @@ You can join our [discord server](https://discord.gg/5Xh2W87egW) for commits, ch
### Extra included plugins
<details>
<summary>163 additional plugins</summary>
<summary>164 additional plugins</summary>
### All Platforms
@ -172,6 +172,7 @@ You can join our [discord server](https://discord.gg/5Xh2W87egW) for commits, ch
- Woof by Samwich
- WriteUpperCase by Samwich & KrystalSkull
- YoutubeDescription by arHSM
- LastActive by Crxa
### Web Only

View file

@ -0,0 +1,146 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2025 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { EquicordDevs } from "@utils/constants";
import definePlugin from "@utils/types";
import { findByPropsLazy } from "@webpack";
import { Menu, NavigationRouter, Toasts } from "@webpack/common";
const MessageStore = findByPropsLazy("getMessages", "getMessage");
const ChannelStore = findByPropsLazy("getChannel", "getDMFromUserId");
const UserStore = findByPropsLazy("getUser", "getCurrentUser");
const MessageActions = findByPropsLazy("fetchMessages", "searchMessages");
async function findLastMessageFromUser(channelId: string, userId: string) {
try {
if (!MessageStore || !MessageActions) {
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Required Discord modules not found.",
id: Toasts.genId()
});
return null;
}
const messageCollection = MessageStore.getMessages(channelId);
let messages = messageCollection?.toArray() || [];
let userMessage = messages.filter(m => m?.author?.id === userId).pop();
if (userMessage) return userMessage.id;
try {
await MessageActions.fetchMessages({
channelId: channelId,
limit: 50
});
const updatedCollection = MessageStore.getMessages(channelId);
messages = updatedCollection?.toArray() || [];
userMessage = messages.filter(m => m?.author?.id === userId).pop();
if (userMessage) return userMessage.id;
} catch (fetchError) {
console.error("Error fetching messages:", fetchError);
}
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Couldn't find any recent messages from this user.",
id: Toasts.genId()
});
return null;
} catch (error) {
console.error("Error finding last message:", error);
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Failed to find messages. Check console for details.",
id: Toasts.genId()
});
return null;
}
}
async function jumpToLastActive(channel: any, targetUserId?: string) {
try {
if (!channel) {
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Channel information not available.",
id: Toasts.genId()
});
return;
}
const guildId = channel.guild_id !== null ? channel.guild_id : "@me";
const channelId = channel.id;
let userId: string;
if (targetUserId) {
userId = targetUserId;
} else {
if (!UserStore?.getCurrentUser) {
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Could not determine user. Try again later.",
id: Toasts.genId()
});
return;
}
const currentUser = UserStore.getCurrentUser();
if (!currentUser || !currentUser.id) {
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Could not determine current user. Try again later.",
id: Toasts.genId()
});
return;
}
userId = currentUser.id;
}
const messageId = await findLastMessageFromUser(channelId, userId);
if (messageId) {
const url = `/channels/${guildId}/${channelId}/${messageId}`;
NavigationRouter.transitionTo(url);
}
} catch (error) {
console.error("Error in jumpToLastActive:", error);
Toasts.show({
type: Toasts.Type.FAILURE,
message: "Failed to jump to message. Check console for details.",
id: Toasts.genId()
});
}
}
const ChannelContextMenuPatch: NavContextMenuPatchCallback = (children, { channel }) => {
children.push(
<Menu.MenuItem
id="LastActive"
label={<span style={{ color: "green" }}>Jump to Your Last Message</span>}
action={() => {
jumpToLastActive(channel);
}}
/>
);
};
const UserContextMenuPatch: NavContextMenuPatchCallback = (children, { user, channel }) => {
if (!channel || !user?.id) return;
children.push(
<Menu.MenuItem
id="LastActive"
label={<span style={{ color: "green" }}>Jump to User's Last Message</span>}
action={() => {
jumpToLastActive(channel, user.id);
}}
/>
);
};
export default definePlugin({
name: "LastActive",
description: "A plugin to jump to last active message from yourself or another user in a channel/server.",
authors: [EquicordDevs.Crxa],
contextMenus: {
"channel-context": ChannelContextMenuPatch,
"user-context": UserContextMenuPatch,
"thread-context": ChannelContextMenuPatch
}
});