I made a Discord bot that runs from a hidden Excel sheet. Because why not.
Our take
I already posted it on r/vba But I'd like to share this knowledge and the interesting things you can create using Wasabi with other communities as well!
So I've been working on a native WebSocket module for VBA called Wasabi, and at some point I asked myself: "can I connect this to Discord's gateway and make a bot?" The answer is yes, and it's glorious and completely unnecessary.
The concept
Discord's bot API is just a WebSocket connection to wss://gateway.discord.gg. Once connected, Discord sends you a JSON payload with an op code telling you to identify yourself. You send back your bot token and intents, and from that point on you receive events (messages, reactions, etc.) as JSON frames. You respond to them by hitting Discord's REST API with plain HTTP calls.
That's it. No SDK needed. Just WebSocket + JSON parsing.
Step 1: Get a bot token
Go to https://discord.com/developers/applications, create an application, add a bot, copy the token. Enable the "Message Content Intent" under the bot settings or it won't receive message content.
Step 2: Import Wasabi
Download Wasabi.bas from https://github.com/uesleibros/wasabi/releases and import it into your VBA project via File > Import File. No references to enable, no DLLs to register.
Step 3: Connect to the gateway and keep it alive
Discord requires you to send a heartbeat payload every N milliseconds (it tells you the interval on connect). Here's a minimal loop that handles that:
#If VBA7 Then Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long #Else Private Declare Function GetTickCount Lib "kernel32" () As Long #End If Sub RunDiscordBot() Dim token As String token = "YOUR_BOT_TOKEN_HERE" Dim handle As Long If Not WebSocketConnect("wss://gateway.discord.gg/?v=10&encoding=json", handle) Then MsgBox "Failed to connect to Discord gateway." Exit Sub End If Dim msg As String Dim heartbeatInterval As Long Dim lastHeartbeat As Long Dim identified As Boolean identified = False lastHeartbeat = 0 Do While WebSocketIsConnected(handle) msg = WebSocketReceive(handle) If Len(msg) > 0 Then Dim op As Long op = ExtractOp(msg) ' parse the "op" field from JSON ' Hello payload: Discord sends heartbeat interval If op = 10 Then heartbeatInterval = ExtractHeartbeatInterval(msg) lastHeartbeat = GetTickCount() ' Identify If Not identified Then Dim identifyPayload As String identifyPayload = "{""op"":2,""d"":{""token"":""" & token & """,""intents"":33280,""properties"":{""os"":""windows"",""browser"":""excel"",""device"":""excel""}}}" WebSocketSend identifyPayload, handle identified = True End If ' Dispatch: actual events like MESSAGE_CREATE ElseIf op = 0 Then Call HandleEvent(msg, token) End If End If ' Send heartbeat when due If GetTickCount() - lastHeartbeat >= heartbeatInterval Then WebSocketSend "{""op"":1,""d"":null}", handle lastHeartbeat = GetTickCount() End If DoEvents Loop WebSocketDisconnect handle End Sub Step 4: Handle a message event
When op = 0 and the event type is MESSAGE_CREATE, you get the channel ID and message content from the JSON. Then you can reply via Discord's REST API using XMLHTTP:
Sub HandleEvent(ByVal payload As String, ByVal token As String) If InStr(payload, "MESSAGE_CREATE") = 0 Then Exit Sub Dim channelId As String Dim content As String channelId = ExtractField(payload, "channel_id") content = ExtractField(payload, "content") If content = "!ping" Then Call SendDiscordMessage(channelId, "Pong! (sent from Excel)", token) End If End Sub Sub SendDiscordMessage(ByVal channelId As String, ByVal message As String, ByVal token As String) Dim http As Object Set http = CreateObject("MSXML2.XMLHTTP") Dim url As String url = "https://discord.com/api/v10/channels/" & channelId & "/messages" Dim body As String body = "{""content"":""" & message & """}" http.Open "POST", url, True http.setRequestHeader "Authorization", "Bot " & token http.setRequestHeader "Content-Type", "application/json" http.Send body Do While http.readyState <> 4 DoEvents Loop End Sub Where to go from here
This is just the skeleton. From here you can:
- Read/write cells based on Discord commands ("!get A1" returns the cell value, "!set A1 42" writes to it)
- Trigger macros remotely by sending a command from Discord
- Post alerts to a channel when a cell value crosses a threshold (combine with a Worksheet_Change event)
- Run the bot from a hidden sheet so it sits quietly in the background while you use the spreadsheet normally
The browser and device fields in the identify payload are set to "excel" in the example above. Discord doesn't validate these, but it's a fun touch.
Full module docs and source: https://github.com/uesleibros/wasabi
[link] [comments]
Read on the original site
Open the publisher's page for the full experience