Skip to content

Commit

Permalink
Fixed #70, Added many debugging options [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloWorld017 committed Oct 23, 2018
1 parent 3e326fc commit 8d145b6
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 55 deletions.
3 changes: 3 additions & 0 deletions i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"error-is-directory": "Couldn't open %file% in config directory.",
"error-is-directory-desc": "%file% is not a file, but a directory!",
"error-is-directory-detail": "Please check %directory%.",
"error-no-discord": "Discord is not running!",
"error-no-discord-desc": "Unless discord is turned on and atom is restarted, rich presence will not be shown.",
"error-no-discord-detail": "You can suppress this notification in settings.",
"not-in-project": "No projects are opened!",
"custom-name": "Custom project name",
"custom-name-primary": "OK",
Expand Down
21 changes: 20 additions & 1 deletion i18n/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,24 @@
"atom-description": "아톰으로 코딩 중",
"type-description": "%type% 파일을 편집하고 있습니다.",
"type-unknown": "파일 편집 중",
"developer-idle": "개발자가 잠시 치킨을 뜯으러 간 것 같습니다."
"developer-idle": "개발자가 잠시 치킨을 뜯으러 간 것 같습니다.",
"generate-failed": "설정 디렉터리의 파일 %file% 생성하기에 실패했습니다.",
"generate-failed-desc": "파일 %file%(을)를 생성하는 중 에러가 발생했습니다.",
"read-failed": "설정 디렉터리의 파일 %file% 읽기에 실패했습니다.",
"read-failed-desc": "파일 %file%(을)를 읽는 중 에러가 발생했습니다.",
"write-failed": "설정 디렉터리의 파일 %file% 쓰기에 실패했습니다.",
"write-failed-desc": "파일 %file%(을)를 쓰는 중 에러가 발생했습니다.",
"error-is-file": "설정 디렉터리 내의 %directory% 폴더를 열 수 없습니다.",
"error-is-file-desc": "동일한 이름의 파일이 이미 존재합니다!",
"error-is-file-detail": "%directory%(을)를 체크해주세요.",
"error-is-directory": "설정 디렉터리 내의 %file% 파일을 읽을 수 없습니다.",
"error-is-directory-desc": "동일한 이름의 디렉터리가 이미 존재합니다!",
"error-is-directory-detail": "%directory%(을)를 체크해주세요.",
"error-no-discord": "디스코드가 실행중이 아닙니다!",
"error-no-discord-desc": "디스코드를 켠 후 아톰을 재시작하지 않을 시, Rich Presence가 보여지지 않을 것입니다.",
"error-no-discord-detail": "이 알림을 설정에서 끌 수 있습니다.",
"not-in-project": "어떤 프로젝트도 열려있지 않습니다!",
"custom-name": "커스텀 프로젝트명",
"custom-name-primary": "확인",
"custom-name-secondary": "기본값 사용"
}
55 changes: 18 additions & 37 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dev": "webpack-dev-server"
},
"dependencies": {
"discord-rpc": "^3.0.0-beta.10",
"discord-rpc": "^3.0.1",
"vue": "^2.5.16",
"vue-ripple-directive": "^2.0.1"
},
Expand Down
62 changes: 61 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ const config = {
},
directory: path.join(atom.getConfigDirPath(), 'atom-discord'),
path: path.join(atom.getConfigDirPath(), 'atom-discord', 'customize.json'),
logPath: path.join(atom.getConfigDirPath(), 'atom-discord', 'log.txt'),
customization: {},
usable: true
usable: true,
loggable: atom.config.get('atom-discord.behaviour.debugLog')
};


Expand Down Expand Up @@ -74,13 +76,15 @@ const initialize = async () => {
if(!configStat.isDirectory()) {
showError('error-is-file', {directory: config.directory});
config.usable = false;
config.loggable = false;
}
} catch(err) {
try {
await promisify(fs.mkdir)(config.directory);
} catch(err) {
showError('generate-failed', {file: 'atom-discord'}, err.stack);
config.usable = false;
config.loggable = false;
}
}

Expand Down Expand Up @@ -113,6 +117,15 @@ const initialize = async () => {
config.usable = false;
}
}

if(config.loggable) {
try {
await promisify(fs.writeFile)(config.logPath, '');
} catch(err) {
showError('generate-failed', {file: 'log.txt'}, err.stack);
config.loggable = false;
}
}
};

const showCustomizeProject = () => {
Expand Down Expand Up @@ -260,6 +273,32 @@ const createLoop = () => {

const rendererId = Math.random().toString(36).slice(2);
ipcRenderer.send('atom-discord.online', {id: rendererId});

if(config.loggable) {
let logs = [];
let lastFlush = 0;
const flushLog = () => {
if(lastFlush + 5000 > Date.now()) {
fs.appendFile(config.logPath, logs.join('\n'), err => {
if(!err) logs = [];

lastFlush = Date.now();
});
}
};

ipcRenderer.on('atom-discord.log', log => {
const date = new Date;
logs.push(`[${date.toTimeString()}] ${log}`);
flushLog();
});
}

if(atom.config.get('atom-discord.behaviour.noDiscordNotification')) {
ipcRenderer.once('atom-discord.noDiscord', () => {
showError('error-no-discord');
});
}
};

atom.config.onDidChange('atom-discord.i18n', ({newValue}) => {
Expand Down Expand Up @@ -369,6 +408,27 @@ module.exports = {
description: "Use rest icon for idle status.",
type: "boolean",
default: true
},

ubuntuPatch: {
title: "Patch Ubuntu 18.04 Bug",
description: "Patch Discord Rich Presence not showing on Ubuntu 18.04",
type: "boolean",
default: false
},

debugLog: {
title: "Debug Mode",
description: "Log debugging messages in atom-discord directory",
type: "boolean",
default: false
},

noDiscordNotification: {
title: "No Discord Notification",
description: "Show warning notification when discord IPC wasn't established",
type: "boolean",
default: false
}
},
order: 1
Expand Down
45 changes: 30 additions & 15 deletions src/send-discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ const normalize = (object) => {
return object;
};

const log = text => {
ipcMain.send('atom-discord.log', text);
console.log(text);
};

class DiscordSender {
constructor() {
this.projectName = null;
this.fileName = null;
this.largeImage = null;
this.startTimestamp = Math.floor(Date.now() / 1000);
this.startTimestamp = new Date;

this.onlineRenderers = {};
this.rpc = null;
Expand All @@ -73,25 +78,25 @@ class DiscordSender {
if(this.onlineRenderers[id]) return;

let sendAfter = !this.isRendererOnline;
console.log(`Editor ${id} became online.`);
log(`Editor ${id} became online.`);

this.onlineRenderers[id] = true;

if(sendAfter){
console.log(`New editor confirmed, sending activities...`);
log(`New editor confirmed, sending activities...`);
this.sendActivity();
}
}

setOffline(id) {
if(!this.onlineRenderers[id]) return;

console.log(`Editor ${id} became offline.`);
log(`Editor ${id} became offline.`);

this.onlineRenderers[id] = false;

if(!this.isRendererOnline) {
console.log(`No editor remained, destroying rpc clients...`);
log(`No editor remained, destroying rpc clients...`);
this.destroyRpc();
}
}
Expand All @@ -103,9 +108,24 @@ class DiscordSender {
if(typeof Client === 'undefined') return reject("No client available!");

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

let previousPath = process.env.XDG_RUNTIME_DIR;
if(config.behaviour.ubuntuPatch) {
const { env: { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } } = process;
const prefix = XDG_RUNTIME_DIR || TMPDIR || TMP || TEMP || '/tmp';

prefix += '/snap.discord';

process.env.XDG_RUNTIME_DIR = prefix;
}

rpc.on('ready', () => {
this.rpc = rpc;
this.destroied = false;

if(config.behaviour.ubuntuPatch) {
process.env.XDG_RUNTIME_DIR = previousPath;
}
resolve();
});
rpc.login(DISCORD_ID).catch(reject);
Expand All @@ -116,14 +136,14 @@ class DiscordSender {
if(this.destroied) return;
if(this.rpc === null) return;

console.log("Destroying RPC Client...");
log("Destroying RPC Client...");
this.destroied = true;

const _rpc = this.rpc;
this.rpc = null;

await _rpc.destroy();
console.log("Done destroying RPC Client. It is now safe to turn off the computer.")
log("Done destroying RPC Client. It is now safe to turn off the computer.")
}

sendActivity() {
Expand Down Expand Up @@ -186,13 +206,7 @@ class DiscordSender {
}

clearActivity() {
// TODO change when discord-rpc#25 is uploaded to npm

const pid = process.pid;

this.rpc.request('SET_ACTIVITY', {
pid
});
this.rpc.clearActivity();
}

updateActivity(projectName, fileName) {
Expand Down Expand Up @@ -239,7 +253,8 @@ class DiscordSender {
try {
if(this.destroied) await this.setupRpc(Client)
} catch(e) {
console.error(e);
log(e.stack);
ipcMain.send('atom-discord.noDiscord');
}

if(this.isRendererOnline) this.sendActivity();
Expand Down

0 comments on commit 8d145b6

Please sign in to comment.