This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiscordMute.cs
104 lines (84 loc) · 3.97 KB
/
DiscordMute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#region
using MelonLoader;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UIExpansionKit.API;
using UnityEngine;
using UnityEngine.UI;
using Keys = System.Windows.Forms.Keys;
#endregion
namespace DiscordMute
{
public static class BuildInfo
{
public const string Name = "DiscordMute";
public const string Description = "Mod for mute/unmute Discord directly in-game";
public const string Author = "Rafa";
public const string Company = "RBX";
public const string Version = "1.1.1";
public const string DownloadLink = null;
}
public class DiscordMute : MelonMod
{
#region DllImport
[DllImport("user32.dll", SetLastError = true)]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private static readonly int KEYEVENTF_KEYUP = 0x0002;
#endregion
private string MuteKey;
public override void OnApplicationStart()
{
MelonPreferences.CreateCategory("DiscordMute");
MelonPreferences.CreateEntry("DiscordMute", nameof(MuteKey), "", "Mute Key", true);
OnPreferencesSaved();
}
public override void OnPreferencesSaved()
{
MuteKey = MelonPreferences.GetEntryValue<string>("DiscordMute", nameof(MuteKey));
}
private GameObject DiscordButton;
public override void VRChat_OnUiManagerInit()
{
BindManager.Initialize();
void ShowBindManager()
{
BindManager.Show("Press your mute key in keyboard", new Action<List<Keys>>(selectedKeys =>
{
string stringKeys = "";
if (selectedKeys.Count > 0) stringKeys = string.Join(",", selectedKeys);
MelonPreferences.SetEntryValue("DiscordMute", nameof(MuteKey), stringKeys);
MelonPreferences.Save();
}), null);
}
ExpansionKitApi.GetExpandedMenu(ExpandedMenu.SettingsMenu).AddSimpleButton("Discord Bind", () => ShowBindManager());
var originalMic = GameObject.Find("/UserInterface/QuickMenu/MicControls");
DiscordButton = GameObject.Instantiate(originalMic, originalMic.transform);
DiscordButton.name = "Discord";
DiscordButton.SetActive(true);
DiscordButton.transform.localPosition -= new Vector3(420, 7);
DiscordButton.GetComponentInChildren<Text>().text = "Discord";
var button = DiscordButton.GetComponentInChildren<Button>();
button.onClick = new Button.ButtonClickedEvent();
button.onClick.AddListener(new Action(() => {
bool isEnabled = !GameObject.Find("/UserInterface/QuickMenu/MicControls/Discord/MicButton/MicEnabled").activeSelf;
GameObject.Find("/UserInterface/QuickMenu/MicControls/Discord/MicButton/MicEnabled").gameObject.SetActive(isEnabled);
GameObject.Find("/UserInterface/QuickMenu/MicControls/Discord/MicButton/MicDisabled").gameObject.SetActive(!isEnabled);
if (!string.IsNullOrEmpty(MuteKey))
{
List<Keys> selectedKeys = new List<Keys>();
if (!string.IsNullOrEmpty(MuteKey))
{
string[] stringKeys = MuteKey.Split(',');
foreach(var stringKey in stringKeys) selectedKeys.Add((Keys)Enum.Parse(typeof(Keys), stringKey));
}
// Hold and Release the selected keys
foreach (var key in selectedKeys) HoldKey(key);
foreach (var key in selectedKeys) ReleaseKey(key);
} else ShowBindManager();
}));
}
private void HoldKey(Keys key) => keybd_event((byte)key, (byte)key, 0, 0);
private void ReleaseKey(Keys key) => keybd_event((byte)key, (byte)key, KEYEVENTF_KEYUP, 0);
}
}