Skip to content

Commit

Permalink
Merge pull request #80 from xhayper/patch-1
Browse files Browse the repository at this point in the history
feat: migrated to `@xhayper/discord-rpc`
  • Loading branch information
leonardssh authored Aug 1, 2022
2 parents d0cac0f + 47a7297 commit a6ad7b7
Show file tree
Hide file tree
Showing 4 changed files with 678 additions and 980 deletions.
47 changes: 22 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,36 @@
},
"homepage": "https://github.com/LeonardSSH/vscord#readme",
"dependencies": {
"bufferutil": "^4.0.3",
"discord-rpc": "^4.0.1",
"electron": "^17.0.1",
"git-url-parse": "^11.5.0",
"utf-8-validate": "^5.0.5"
"@xhayper/discord-rpc": "^0.0.4-beta",
"git-url-parse": "^12.0.0"
},
"optionalDependencies": {
"register-scheme": "devsnek/node-register-scheme"
"bufferutil": "^4.0.6",
"utf-8-validate": "^5.0.9"
},
"devDependencies": {
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@types/discord-rpc": "^4.0.0",
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^17.0.3",
"@types/git-url-parse": "^9.0.1",
"@types/node": "^17.0.18",
"@types/vscode": "^1.59.0",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"@types/node": "^18.6.3",
"@types/vscode": "^1.69.0",
"@typescript-eslint/eslint-plugin": "^5.31.0",
"@typescript-eslint/parser": "^5.31.0",
"clean-webpack-plugin": "^4.0.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.9.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.1",
"lint-staged": "^12.3.4",
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1",
"eslint": "^8.20.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"rimraf": "^3.0.2",
"terser-webpack-plugin": "^5.3.1",
"ts-loader": "^9.2.6",
"typescript": "^4.3.5",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2"
"terser-webpack-plugin": "^5.3.3",
"ts-loader": "^9.3.1",
"typescript": "^4.7.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
},
"commitlint": {
"extends": [
Expand Down
6 changes: 3 additions & 3 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Presence } from 'discord-rpc';
import { SetActivity } from '@xhayper/discord-rpc/dist/structures/ClientUser';
import { getConfig } from './config';
import {
debug,
Expand Down Expand Up @@ -50,7 +50,7 @@ export function onDiagnosticsChange() {
totalProblems = counted;
}

export function activity(previous: Presence = {}, isViewing = false): Presence {
export function activity(previous: SetActivity = {}, isViewing = false): SetActivity {
const config = getConfig();
const { appName } = env;

Expand All @@ -73,7 +73,7 @@ export function activity(previous: Presence = {}, isViewing = false): Presence {
const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails];
const removeLowerDetailsIdling = config[CONFIG_KEYS.RemoveLowerDetailsIdling];

let presence: Presence = {
let presence: SetActivity = {
details: removeDetails
? undefined
: details(
Expand Down
27 changes: 11 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client, Presence } from 'discord-rpc';
import { SetActivity } from '@xhayper/discord-rpc/dist/structures/ClientUser';
import { Client } from '@xhayper/discord-rpc';
import { logError, logInfo } from './logger';
import {
commands,
Expand All @@ -19,7 +20,7 @@ import { getApplicationId } from './helpers/getApplicationId';
import { dataClass } from './data';
import { getFileIcon } from './helpers/resolveFileIcon';

let state: Presence = {};
let state: SetActivity = {};
let rpc: Client | undefined = undefined;
let listeners: Disposable[] = [];
let idleCheckTimeout: NodeJS.Timer | undefined = undefined;
Expand All @@ -30,10 +31,10 @@ statusBarIcon.text = '$(pulse) Connecting to Discord Gateway...';

export const sendActivity = async (isViewing = false) => {
state = {
...(await activity(state, isViewing))
...activity(state, isViewing)
};

await rpc?.setActivity(state);
await rpc?.user?.setActivity(state);
};

export const listen = () => {
Expand Down Expand Up @@ -76,7 +77,7 @@ export const toggleIdling = async (windowState: WindowState) => {
} else {
idleCheckTimeout = setTimeout(async () => {
if (config[CONFIG_KEYS.DisconnectOnIdle]) {
await rpc?.clearActivity();
await rpc?.user?.clearActivity();

if (config[CONFIG_KEYS.ResetElapsedTimeAfterIdle]) {
state.startTimestamp = undefined;
Expand All @@ -91,7 +92,7 @@ export const toggleIdling = async (windowState: WindowState) => {
smallImageText: config[CONFIG_KEYS.IdleText]
};

await rpc?.setActivity(state);
await rpc?.user?.setActivity(state);
}, config[CONFIG_KEYS.IdleTimeout] * 1000);
}
}
Expand All @@ -100,10 +101,12 @@ export const toggleIdling = async (windowState: WindowState) => {
export const login = async () => {
const config = getConfig();

const { clientId } = getApplicationId(config);

statusBarIcon.text = '$(search-refresh) Connecting to Discord Gateway...';
statusBarIcon.tooltip = 'Connecting to Discord Gateway...';

rpc = new Client({ transport: 'ipc' });
rpc = new Client({ clientId });

rpc.on('ready', async () => {
logInfo('Successfully connected to Discord');
Expand Down Expand Up @@ -135,10 +138,8 @@ export const login = async () => {
statusBarIcon.show();
});

const { clientId } = getApplicationId(config);

try {
await rpc.login({ clientId });
await rpc.login();
logInfo(`Successfully logged in to Discord with client ID ${clientId}`);
} catch (error: any) {
logError(`Encountered following error while trying to login:\n${error as string}`);
Expand Down Expand Up @@ -274,9 +275,3 @@ export function deactivate() {

logInfo(`[004] Destroyed Discord RPC client`);
}

declare module 'discord-rpc' {
interface Client {
on(event: 'ready' | 'connected' | 'disconnected', listener: () => void): this;
}
}
Loading

0 comments on commit a6ad7b7

Please sign in to comment.