diff --git a/package.json b/package.json index b09e63d..a35a080 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "license": "MIT", "dependencies": { "chalk": "4.1.2", + "discord-rpc": "^4.0.1", "eris": "github:abalabahaha/eris#dev" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 353a918..fd57a7f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,10 +2,12 @@ lockfileVersion: 5.4 specifiers: chalk: 4.1.2 + discord-rpc: ^4.0.1 eris: github:abalabahaha/eris#dev dependencies: chalk: 4.1.2 + discord-rpc: 4.0.1 eris: github.com/abalabahaha/eris/eb403730855714eafa36c541dbe2cb84c9979158 packages: @@ -17,6 +19,13 @@ packages: color-convert: 2.0.1 dev: false + /bindings/1.5.0: + resolution: {integrity: sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==} + dependencies: + file-uri-to-path: 1.0.0 + dev: false + optional: true + /chalk/4.1.2: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} @@ -36,11 +45,46 @@ packages: resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} dev: false + /discord-rpc/4.0.1: + resolution: {integrity: sha512-HOvHpbq5STRZJjQIBzwoKnQ0jHplbEWFWlPDwXXKm/bILh4nzjcg7mNqll0UY7RsjFoaXA7e/oYb/4lvpda2zA==} + dependencies: + node-fetch: 2.6.7 + ws: 7.5.9 + optionalDependencies: + register-scheme: github.com/devsnek/node-register-scheme/e7cc9a63a1f512565da44cb57316d9fb10750e17 + transitivePeerDependencies: + - bufferutil + - encoding + - utf-8-validate + dev: false + + /file-uri-to-path/1.0.0: + resolution: {integrity: sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==} + dev: false + optional: true + /has-flag/4.0.0: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} dev: false + /node-addon-api/1.7.2: + resolution: {integrity: sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg==} + dev: false + optional: true + + /node-fetch/2.6.7: + resolution: {integrity: sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==} + engines: {node: 4.x || >=6.0.0} + peerDependencies: + encoding: ^0.1.0 + peerDependenciesMeta: + encoding: + optional: true + dependencies: + whatwg-url: 5.0.0 + dev: false + /opusscript/0.0.8: resolution: {integrity: sha512-VSTi1aWFuCkRCVq+tx/BQ5q9fMnQ9pVZ3JU4UHKqTkf0ED3fKEPdr+gKAAl3IA2hj9rrP6iyq3hlcJq3HELtNQ==} requiresBuild: true @@ -54,12 +98,40 @@ packages: has-flag: 4.0.0 dev: false + /tr46/0.0.3: + resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} + dev: false + /tweetnacl/1.0.3: resolution: {integrity: sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==} requiresBuild: true dev: false optional: true + /webidl-conversions/3.0.1: + resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} + dev: false + + /whatwg-url/5.0.0: + resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} + dependencies: + tr46: 0.0.3 + webidl-conversions: 3.0.1 + dev: false + + /ws/7.5.9: + resolution: {integrity: sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==} + engines: {node: '>=8.3.0'} + peerDependencies: + bufferutil: ^4.0.1 + utf-8-validate: ^5.0.2 + peerDependenciesMeta: + bufferutil: + optional: true + utf-8-validate: + optional: true + dev: false + /ws/8.8.1: resolution: {integrity: sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA==} engines: {node: '>=10.0.0'} @@ -87,3 +159,14 @@ packages: - bufferutil - utf-8-validate dev: false + + github.com/devsnek/node-register-scheme/e7cc9a63a1f512565da44cb57316d9fb10750e17: + resolution: {tarball: https://codeload.github.com/devsnek/node-register-scheme/tar.gz/e7cc9a63a1f512565da44cb57316d9fb10750e17} + name: register-scheme + version: 0.0.2 + requiresBuild: true + dependencies: + bindings: 1.5.0 + node-addon-api: 1.7.2 + dev: false + optional: true diff --git a/src/commands/afk.js b/src/commands/afk.js index 565e793..88931d0 100644 --- a/src/commands/afk.js +++ b/src/commands/afk.js @@ -1,4 +1,5 @@ const {addCommand} = require("../lib/command"); +const {updatePresence} = require("../lib/presence"); addCommand("A", "toggles AFK mode", function () { if (comcord.state.afk == true) { @@ -12,4 +13,6 @@ addCommand("A", "toggles AFK mode", function () { comcord.client.editAFK(true); console.log(""); } + + updatePresence(); }); diff --git a/src/commands/send.js b/src/commands/send.js index b53d96c..2402a5e 100644 --- a/src/commands/send.js +++ b/src/commands/send.js @@ -1,6 +1,7 @@ const chalk = require("chalk"); const {startPrompt} = require("../lib/prompt"); +const {updatePresence} = require("../lib/presence"); function sendMode() { if (!comcord.state.currentChannel) { @@ -30,6 +31,8 @@ function sendMode() { comcord.client.editStatus("online"); comcord.client.editAFK(false); console.log(""); + + updatePresence(); } } catch (err) { console.log(""); diff --git a/src/commands/switchChannel.js b/src/commands/switchChannel.js index ded227f..b59f1ab 100644 --- a/src/commands/switchChannel.js +++ b/src/commands/switchChannel.js @@ -1,5 +1,6 @@ const {addCommand} = require("../lib/command"); const {startPrompt} = require("../lib/prompt"); +const {updatePresence} = require("../lib/presence"); const {listUsers} = require("./listUsers"); @@ -32,6 +33,8 @@ function switchChannel(input) { const channel = guild.channels.get(comcord.state.currentChannel); process.title = `${guild.name} - ${channel.name} - comcord`; + + updatePresence(); } } diff --git a/src/commands/switchGuild.js b/src/commands/switchGuild.js index 77b2243..542b954 100644 --- a/src/commands/switchGuild.js +++ b/src/commands/switchGuild.js @@ -1,5 +1,6 @@ const {addCommand} = require("../lib/command"); const {startPrompt} = require("../lib/prompt"); +const {updatePresence} = require("../lib/presence"); const {listChannels} = require("./listChannels"); const {listUsers} = require("./listUsers"); @@ -47,6 +48,8 @@ function switchGuild(input) { const channel = guild.channels.get(comcord.state.currentChannel); process.title = `${guild.name} - ${channel.name} - comcord`; + + updatePresence(); } } diff --git a/src/index.js b/src/index.js index 284fc9d..ea9c917 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,8 @@ const Eris = require("eris"); const chalk = require("chalk"); +const DiscordRPC = require("discord-rpc"); + +const CLIENT_ID = "1026163285877325874"; const token = process.argv[2]; @@ -24,9 +27,12 @@ const client = new Eris("Bot " + token, { intents: Eris.Constants.Intents.all, }); comcord.client = client; +const rpc = new DiscordRPC.Client({transport: "ipc"}); +comcord.rpc = rpc; const {finalizePrompt} = require("./lib/prompt"); const {processMessage, processQueue} = require("./lib/messages"); +const {updatePresence} = require("./lib/presence"); require("./commands/quit"); require("./commands/clear"); @@ -56,16 +62,27 @@ client.once("ready", function () { client.editStatus("online", [ { - application_id: "1026163285877325874", + application_id: CLIENT_ID, name: "comcord", timestamps: { start: comcord.state.startTime, }, }, ]); + + rpc + .login({ + clientId: CLIENT_ID, + }) + .catch(function () {}); }); client.on("error", function () {}); +rpc.on("ready", function () { + updatePresence(); +}); +rpc.on("error", function () {}); + client.on("messageCreate", function (msg) { if (msg.author.id === client.user.id) return; diff --git a/src/lib/presence.js b/src/lib/presence.js new file mode 100644 index 0000000..3aac056 --- /dev/null +++ b/src/lib/presence.js @@ -0,0 +1,36 @@ +function updatePresence() { + let guild, channel; + if (comcord.state.currentGuild != null) { + guild = comcord.client.guilds.get(comcord.state.currentGuild); + } + if (comcord.state.currentChannel != null && guild != null) { + channel = guild.channels.get(comcord.state.currentChannel); + } + + try { + const activity = { + startTimestamp: comcord.state.startTime, + smallImageKey: `https://cdn.discordapp.com/avatars/${comcord.client.user.id}/${comcord.client.user.avatar}.png?size=1024`, + smallImageText: `${comcord.client.user.username}#${comcord.client.user.discriminator}`, + buttons: [ + {label: "comcord Repo", url: "https://github.com/Cynosphere/comcord"}, + ], + }; + + if (guild != null) { + activity.largeImageKey = `https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.png?size=1024`; + activity.largeImageText = guild.name; + if (channel != null) { + activity.details = `#${channel.name} - ${guild.name}`; + } + } + if (comcord.state.afk == true) { + activity.state = "AFK"; + } + comcord.rpc.setActivity(activity); + } catch (err) { + // + } +} + +module.exports = {updatePresence};