Applescript to mute Discord
Firstly, well done for exploring and doing your own research with which you've come armed to ask how it can be applied to form a solution. It makes it so much easier to provide useful help.
With that in mind, the screenshot from UI Browser is immensely helpful, as it literally lays out the hierarchy of UI elements that we need to traverse in order to reach the target menu item and issue a click via AppleScript:
tell application "System Events"
tell application process "Discord"
tell menu bar 2
tell menu bar item 1
click
tell (a reference to menu 1)
repeat until it exists
delay 0.2
end repeat
tell menu item "Mute"
click
end tell
end tell
end tell
end tell
end tell
end tell
However, there is a known and well-queried/documented/complained-about phenomenon of an irritating 5-second inexplicable delay between the first click
that shows the menu, and the second click
that selects the menu item. This isn't specific to Discord, but seems to apply to menu bar icon menus.
Various solutions have been put forward to workaround this, but I won't go into them in detail because none of them are a) reliable, b) without side-effects, or c) clean to implement. You'll undoubtedly do a quick search yourself, and come upon the most common solution, which is to kill the System Events process between the first two click
events. But you'll find upon further reading that later on, users report that this method stops working; and in the interrim, causes System Events to throw other, unrelated errors where it didn't before.
Therefore, this solution for you is bittersweet, I'm afraid. I've shown you how to AppleScript your way to the menu item you want; but the end result may not be any more appealing than your first option.
EDIT: Possible Non-AppleScript Solution
Having downloaded a copy of Discord myself to see what's what, I first note that my version of the application has no such menu bar icon to speak of. It must be a setting I haven't gleaned upon.
What I did glean upon, however, is the option to set a user-defined keybinding that triggers one of a selection of functions, one of which being a toggle for the mic mute:
Here, I set up one to bind ⌘⌥⇥ to Toggle Mute. As you'd expect, it works within the application. Surprisingly, it works outwith the application too, when it doesn't have focus, and even when it's minimised. Even more surprisingly, it worked when Discord was resident on a separate desktop (space), and at no point did the Discord app need to attain focus or the active desktop be switched away from.
I believe this is exactly the solution you're after - a hotkey that operates globally on your system to mute your Discord microphone.
After a huge amount of frustration and none of the proposed solutions working for me, I solved the issue by making a Better Discord plugin that listens for a change to a .txt file simply containing a 0 or a 1. When the text file contains 1, it will mute, when it contains 0, it will unmute. Then I just made 2 applescripts that get and set the text file, respectively. Those are implemented using MTMR. It works pretty well! In the future, I intend to see about having the button disappear when no call is active.