Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
danim1130 committed Apr 4, 2024
1 parent 622d9a2 commit b2b9f23
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 220 deletions.
232 changes: 122 additions & 110 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
let isEnabled = false;
let controller = undefined;
let messagePorts = new Set();
let fs = require("fs")
let path = require("path")

let fs = require("fs");
let path = require("path");

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

Expand Down Expand Up @@ -71,103 +70,118 @@ exports.loadPackage = async function (gridController, persistedData) {
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"})
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,
},
})
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"})
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,
},
})
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"})
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,
},
})
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"})
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,
},
})
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;
};
Expand Down Expand Up @@ -220,9 +234,9 @@ exports.sendMessage = async function (args) {
if (type === "input") {
let vol = Number(args[1]);
let transformedVol = 0;
if (vol < 0){
if (vol < 0) {
transformedVol = 0;
} else if (vol >= 100){
} else if (vol >= 100) {
transformedVol = 100;
} else {
transformedVol = Math.exp((vol - 19.966) / 17.369);
Expand All @@ -234,11 +248,11 @@ exports.sendMessage = async function (args) {
if (type === "output") {
let vol = Number(args[1]);
let transformedVol = 0;
if (vol < 0){
if (vol < 0) {
transformedVol = 0;
} else if (vol >= 200){
} else if (vol >= 200) {
transformedVol = 200;
} else if (vol <= 100){
} else if (vol <= 100) {
//Two different curves, switches at apprx. 100
transformedVol = Math.exp((vol - 19.966) / 17.369);
} else {
Expand All @@ -248,11 +262,13 @@ exports.sendMessage = async function (args) {
.setVoiceSettings({ output: { volume: Math.min(transformedVol, 200) } })
.catch(console.info);
}
if (type.includes("mute") || type.includes("deafen")){
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")){
if (type.includes("toggle")) {
let voiceSetting = await client.user
.getVoiceSettings()
.catch(console.info);
if (type.includes("mute")) {
newValue = !voiceSetting.mute;
} else {
newValue = !voiceSetting.deaf;
Expand All @@ -261,17 +277,13 @@ exports.sendMessage = async function (args) {
newValue = Boolean(args[1]);
}

if (type.includes("mute")){
client.user
.setVoiceSettings({ mute: newValue })
.catch(console.info);
if (type.includes("mute")) {
client.user.setVoiceSettings({ mute: newValue }).catch(console.info);
} else {
client.user
.setVoiceSettings({ deaf: newValue })
.catch(console.info);
client.user.setVoiceSettings({ deaf: newValue }).catch(console.info);
}
}
if (type === "select-channel"){
if (type === "select-channel") {
let channelId = args[1];
let force = Boolean(args[2]);
client.user
Expand Down
72 changes: 41 additions & 31 deletions select_voice_channel_action.html
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
<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"/>
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 input = document.currentScript.previousElementSibling.firstElementChild;
let checkbox = input.nextElementSibling;
function handleConfigUpdate(config){
const regex = /^gps\("package-discord", "select-channel", "*(.*?)"*, (.*?)\)$/;
function handleConfigUpdate(config) {
const regex =
/^gps\("package-discord", "select-channel", "*(.*?)"*, (.*?)\)$/;

const match = config.script.match(regex);
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;
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);
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)
checkbox.addEventListener("change", updateActionCode, false);
input.addEventListener("change", updateActionCode, false);

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

0 comments on commit b2b9f23

Please sign in to comment.