Skip to content

Commit

Permalink
Add basic discord functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
danim1130 committed Apr 4, 2024
1 parent 7a761b4 commit 622d9a2
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 27 deletions.
1 change: 1 addition & 0 deletions discord-mark-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
177 changes: 169 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
let isEnabled = false;
let controller = undefined;
let messagePorts = new Set();
let fs = require("fs")
let path = require("path")


const { Client } = require("@xhayper/discord-rpc");

Expand Down Expand Up @@ -67,10 +70,125 @@ exports.loadPackage = async function (gridController, persistedData) {
if (token) {
initializeDiscord(clientId, clientSecret, token);
}

let discordIconSvg = fs.readFileSync(path.resolve(__dirname, 'discord-mark-white.svg'), {encoding: "utf-8"})

let volumeActionHtml = fs.readFileSync(path.resolve(__dirname, 'volume_control_action.html'), {encoding: "utf-8"})
controller.sendMessageToRuntime({
id: "add-action",
info: {
actionId: 0,
short: "xdiscvc",
name: "Discord_VolumeControl",
displayName: "Volume Control",
rendering: "standard",
category: "discord",
blockTitle: "Volume Control",
defaultLua: 'gps("package-discord", "input", val)',
color: "#5865F2",
icon: discordIconSvg,
blockIcon: discordIconSvg,
selectable: true,
movable: true,
hideIcon: false,
type: "single",
toggleable: true,
actionHtml: volumeActionHtml,
},
})

let voiceSetHtml = fs.readFileSync(path.resolve(__dirname, 'voice_set_action.html'), {encoding: "utf-8"})
controller.sendMessageToRuntime({
id: "add-action",
info: {
actionId: 1,
short: "xdiscvs",
name: "Discord_VoiceSet",
displayName: "Voice Set",
rendering: "standard",
category: "discord",
blockTitle: "Voice Set",
defaultLua: 'gps("package-discord", "mute-set", val)',
color: "#5865F2",
icon: discordIconSvg,
blockIcon: discordIconSvg,
selectable: true,
movable: true,
hideIcon: false,
type: "single",
toggleable: true,
actionHtml: voiceSetHtml,
},
})

let voiceToggleHtml = fs.readFileSync(path.resolve(__dirname, 'voice_toggle_action.html'), {encoding: "utf-8"})
controller.sendMessageToRuntime({
id: "add-action",
info: {
actionId: 2,
short: "xdiscvt",
name: "Discord_VoiceToggle",
displayName: "Voice Toggle",
rendering: "standard",
category: "discord",
blockTitle: "Voice Toggle",
defaultLua: 'gps("package-discord", "mute-toggle")',
color: "#5865F2",
icon: discordIconSvg,
blockIcon: discordIconSvg,
selectable: true,
movable: true,
hideIcon: false,
type: "single",
toggleable: true,
actionHtml: voiceToggleHtml,
},
})

let selectChannelHtml = fs.readFileSync(path.resolve(__dirname, 'select_voice_channel_action.html'), {encoding: "utf-8"})
controller.sendMessageToRuntime({
id: "add-action",
info: {
actionId: 3,
short: "xdiscsc",
name: "Discord_ChannelSelect",
displayName: "Channel Select",
rendering: "standard",
category: "discord",
blockTitle: "Channel Select",
defaultLua: 'gps("package-discord", "select-channel", null, false)',
color: "#5865F2",
icon: discordIconSvg,
blockIcon: discordIconSvg,
selectable: true,
movable: true,
hideIcon: false,
type: "single",
toggleable: true,
actionHtml: selectChannelHtml,
},
})

isEnabled = true;
};

exports.unloadPackage = async function () {
controller.sendMessageToRuntime({
id: "remove-action",
actionId: 0,
});
controller.sendMessageToRuntime({
id: "remove-action",
actionId: 1,
});
controller.sendMessageToRuntime({
id: "remove-action",
actionId: 2,
});
controller.sendMessageToRuntime({
id: "remove-action",
actionId: 3,
});
controller = undefined;
messagePorts.forEach((port) => port.close());
messagePorts.clear();
Expand Down Expand Up @@ -101,20 +219,63 @@ exports.sendMessage = async function (args) {
let type = args[0];
if (type === "input") {
let vol = Number(args[1]);
// vol must be between 0 and 100
if (vol > 100) vol = 100;
if (vol < 0) vol = 0;
let transformedVol = 0;
if (vol < 0){
transformedVol = 0;
} else if (vol >= 100){
transformedVol = 100;
} else {
transformedVol = Math.exp((vol - 19.966) / 17.369);
}
client.user
.setVoiceSettings({ input: { volume: vol } })
.setVoiceSettings({ input: { volume: Math.min(transformedVol, 100) } })
.catch(console.info);
}
if (type === "output") {
let vol = Number(args[1]);
// vol must be between 0 and 100
if (vol > 100) vol = 100;
if (vol < 0) vol = 0;
let transformedVol = 0;
if (vol < 0){
transformedVol = 0;
} else if (vol >= 200){
transformedVol = 200;
} else if (vol <= 100){
//Two different curves, switches at apprx. 100
transformedVol = Math.exp((vol - 19.966) / 17.369);
} else {
transformedVol = Math.exp((vol + 565.99) / 144.63);
}
client.user
.setVoiceSettings({ output: { volume: vol } })
.setVoiceSettings({ output: { volume: Math.min(transformedVol, 200) } })
.catch(console.info);
}
if (type.includes("mute") || type.includes("deafen")){
let newValue = false;
if (type.includes("toggle")){
let voiceSetting = await client.user.getVoiceSettings().catch(console.info);
if (type.includes("mute")){
newValue = !voiceSetting.mute;
} else {
newValue = !voiceSetting.deaf;
}
} else {
newValue = Boolean(args[1]);
}

if (type.includes("mute")){
client.user
.setVoiceSettings({ mute: newValue })
.catch(console.info);
} else {
client.user
.setVoiceSettings({ deaf: newValue })
.catch(console.info);
}
}
if (type === "select-channel"){
let channelId = args[1];
let force = Boolean(args[2]);
client.user
.selectVoiceChannel(channelId, undefined, force)
.catch(console.info);
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "package-discord",
"version": "0.0.1",
"version": "0.0.2",
"description": "Discord RPC",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 0 additions & 18 deletions preference.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@
</div>

<div class="py-2 flex flex-col">
<label for="mic_volume">Microphone volume</label>
<input
class="my-1 text-white px-2 py-1 bg-primary focus:outline-none focus:ring-1"
min="0"
max="100"
id="mic_volume"
type="number"
/>

<label for="out_volume">Output volume</label>
<input
class="my-1 text-white px-2 py-1 bg-primary focus:outline-none focus:ring-1"
min="0"
max="100"
id="out_volume"
type="number"
/>

<textarea
placeholder="logs..."
class="my-2 text-white outline-none p-1 bg-primary"
Expand Down
43 changes: 43 additions & 0 deletions select_voice_channel_action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<div class="flex items-center text-white">
Channel Id: <input class="flex flex-grow h-auto bg-primary focus:outline-none py-1 px-2 border border-transparent"/>
Force: <input type="checkbox"/>
</div>
<script>
{
let input = document.currentScript.previousElementSibling.firstElementChild
let checkbox = input.nextElementSibling;
function handleConfigUpdate(config){
const regex = /^gps\("package-discord", "select-channel", "*(.*?)"*, (.*?)\)$/;

const match = config.script.match(regex);

if (match) {
const channelId = match[1];
const force = match[2];
if (channelId){
input.value = channelId;
} else {
input.value = "null"
}
checkbox.value = force;
}
}

function updateActionCode(){
var channelId = input.value
if (channelId !== "null"){
channelId = `"${channelId}"`
}
var force = checkbox.value;
var code = `gps("package-discord", "select-channel", ${channelId}, ${force})`;
const event = new CustomEvent("updateCode", {bubbles: true, detail: {script: String(code)}})
input.dispatchEvent(event);
}

checkbox.addEventListener("change", updateActionCode, false)
input.addEventListener("change", updateActionCode, false)

const event = new CustomEvent("updateConfigHandler", {bubbles: true, detail: {handler: handleConfigUpdate}})
input.dispatchEvent(event)
}
</script>
39 changes: 39 additions & 0 deletions voice_set_action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<div class="flex items-center text-white">
<select class="flex flex-grow h-auto bg-primary focus:outline-none py-1 px-2 border border-transparent">
<option value='mute-set'>Mute</option>
<option value='deafen-set'>Deafen</option>
</select>
<input class="flex flex-grow h-auto bg-primary focus:outline-none py-1 px-2 border border-transparent"/>
</div>
<script>
{
let select = document.currentScript.previousElementSibling.firstElementChild
let input = select.nextElementSibling;
function handleConfigUpdate(config){
const regex = /^gps\("package-discord", "(mute-set|deafen-set)", (.*?)\)$/;

const match = config.script.match(regex);

if (match) {
const targetType = match[1];
const inputValue = match[2];
select.value = targetType;
input.value = inputValue;
}
}

function updateActionCode(){
var targetType = select.value
var inputValue = input.value;
var code = `gps("package-discord", "${targetType}", ${inputValue})`;
const event = new CustomEvent("updateCode", {bubbles: true, detail: {script: String(code)}})
select.dispatchEvent(event);
}

select.addEventListener("change", updateActionCode, false)
input.addEventListener("change", updateActionCode, false)

const event = new CustomEvent("updateConfigHandler", {bubbles: true, detail: {handler: handleConfigUpdate}})
select.dispatchEvent(event)
}
</script>
33 changes: 33 additions & 0 deletions voice_toggle_action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div class="flex items-center text-white">
<select class="h-auto bg-primary focus:outline-none py-1 px-2 border border-transparent">
<option value='mute-toggle'>Mute</option>
<option value='deafen-toggle'>Deafen</option>
</select>
</div>
<script>
{
let select = document.currentScript.previousElementSibling.firstElementChild
function handleConfigUpdate(config){
const regex = /^gps\("package-discord", "(mute-toggle|deafen-toggle)"\)$/;

const match = config.script.match(regex);

if (match) {
const targetType = match[1];
select.value = targetType;
}
}

function updateActionCode(){
var targetType = select.value
var code = `gps("package-discord", "${targetType}")`;
const event = new CustomEvent("updateCode", {bubbles: true, detail: {script: String(code)}})
select.dispatchEvent(event);
}

select.addEventListener("change", updateActionCode, false)

const event = new CustomEvent("updateConfigHandler", {bubbles: true, detail: {handler: handleConfigUpdate}})
select.dispatchEvent(event)
}
</script>
Loading

0 comments on commit 622d9a2

Please sign in to comment.