Plugin
KeepCurrentChannel
Attempt to navigate to the channel you were in before switching accounts or loading Discord.
1
import * as DataStore from "@api/DataStore";2
import { Devs } from "@utils/constants";3
import definePlugin from "@utils/types";4
import { ChannelRouter, ChannelStore, NavigationRouter, SelectedChannelStore, SelectedGuildStore } from "@webpack/common";5
6
export interface LogoutEvent {7
type: "LOGOUT";8
isSwitchingAccount: boolean;9
}10
11
interface ChannelSelectEvent {12
type: "CHANNEL_SELECT";13
channelId: string | null;14
guildId: string | null;15
}16
17
interface PreviousChannel {18
guildId: string | null;19
channelId: string | null;20
}21
22
let isSwitchingAccount = false;23
let previousCache: PreviousChannel | undefined;24
25
export default definePlugin({26
name: "KeepCurrentChannel",27
description: "Attempt to navigate to the channel you were in before switching accounts or loading Discord.",28
tags: ["Utility", "Organisation"],29
authors: [Devs.Nuckyz],30
31
patches: [32
{33
find: 039;"Switching accounts"039;,34
replacement: {35
match: /goHomeAfterSwitching:\i/,36
replace: "goHomeAfterSwitching:!1"37
}38
}39
],40
41
flux: {42
LOGOUT(e: LogoutEvent) {43
({ isSwitchingAccount } = e);44
},45
46
CONNECTION_OPEN() {47
if (!isSwitchingAccount) return;48
isSwitchingAccount = false;49
50
if (previousCache?.channelId) {51
if (ChannelStore.hasChannel(previousCache.channelId)) {52
ChannelRouter.transitionToChannel(previousCache.channelId);53
} else {54
NavigationRouter.transitionToGuild("@me");55
}56
}57
},58
59
async CHANNEL_SELECT({ guildId, channelId }: ChannelSelectEvent) {60
if (isSwitchingAccount) return;61
62
previousCache = {63
guildId,64
channelId65
};66
await DataStore.set("KeepCurrentChannel_previousData", previousCache);67
}68
},69
70
async start() {71
previousCache = await DataStore.get<PreviousChannel>("KeepCurrentChannel_previousData");72
if (!previousCache) {73
previousCache = {74
guildId: SelectedGuildStore.getGuildId(),75
channelId: SelectedChannelStore.getChannelId() ?? null76
};77
78
await DataStore.set("KeepCurrentChannel_previousData", previousCache);79
} else if (previousCache.channelId) {80
ChannelRouter.transitionToChannel(previousCache.channelId);81
}82
}83
});84