Jump to content

[GUIDE] How to get your server scripts to talk to Discord without using a bot.


Recommended Posts

Edited 3rd August
[On 2nd August I was made aware that if you copied my script from the video, which I never intended, you might copy an event handler I wrote, and in that could be a players name which, could include special characters which could break the command script and potentially cause issues. I have updated the script with a text replacement to remove some of the special characters that DOS uses as escape characters.

If you used this before you MUST UPDATE YOUR SCRIPT!!! I provided a sample attached to this post.] 


Hi folks,

What is this?: After reading this, you will be able to send messages to your Discord from a script running on your dedicated servers mission.


Right but that's super complex and I need to have a server with a Discord bot and i cannot be bothered.
Wrong, you can do it with one executable file and a short script (sample attached). Discord invented a technology which allowed messages to be sent to a channel using a temporary token and a URL that was too difficult to guess. This is a Webhook.

WARNING! Since the video I have updated the code. DO NOT COPY IT FROM HERE.

 



1. Get DiscordSendWebhook.exe from https://github.com/phoenix125/DiscordSendWebhook
2. Remove the Operating system sanitising in your missionscripting.lua (important - you must research this if it doenst make sense, it allows Lua to execute any command on your machine, thus, you need to be very aware of what you run in Lua and who from.)
3. Create a Webhook in your Discord server and copy the URL to line 22 where the Discord URL is
4. Customise a Lua Script from mine attached

5. Generate some code that makes some text and execute your function like BotSay("Have a jolly nice day you superstar!")

FAQ:
Do I need Mist or Moose or something?
No, although in the video I wrote my example Event Handler in Moose. However there are plenty of examples of mist, moose and native SSE Event Handlers. The attached miz and file has a selection of MOOSE event handlers.

What else can i do with this?
I researched this being unaware of webhooks and thinking this was complex to do. And i bet others have given up before realising its so easy. Some ideas I had were
~ Starting Scrambles from the desktop - you can provide the Bullseye right to discord with a Scramble message.
~ I did wonder if you can chain Discord bots, like using another bot to read my bot then act, like play a noise when I scrambled someone...
~ Recon info and BDA right to desktop
~ CSAR events (now the event for parachutists works!)
~ Server admin - server start/mission start
~ LSO trap scores
~ extending Discord as a middleware, perhaps taking mission information output and putting it elsewhere, maybe even a Google API or spreadsheet

Let me know what you come up with!

How can i trust the executable?
I'd suggest reading the uncompiled code and compiling it with Autoit yourself. It's likely to alarm some AV software because the heuristics in AV software hates Autoit. I dont guarantee this software in any way but I used it the last 6 months and I am still well!

Why do I see a black command box when this fires?

This is a feature of LUA and it sounds like you are testing this on a local SIngle Player test in the Mission Editor. Run it on a server and none of the code executes locally and won't bother you and you wont see it or feel any pause as the sim is tabbed out of focus by the command box.


Help!
No!

Thanks!
You are welcome. Go share your ideas and make the internet a useful place.

Thanks to Lurcher for the player name sanitization code!
 

botsay.miz botsay.lua


Edited by Pikey
Big Rework, sample miz and script
  • Like 5

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

  • 2 months later...

Hopefully this snipit helps others. It gave me a days worth of trouble as the local text=..... line requires very specific formatting and it wasn't until I dropped the -a flag that it started working. This should be enough to get someone started tho.

 

redacted

 


Edited by chevelle970
Link to comment
Share on other sites

  • 2 weeks later...

First of all, using a webhook like this is a great idea and makes discord integration very easy, so thank you for sharing.

However to anyone reading this, please, do not use any of these example Lua snippets directly, they are extremely unsafe. Filter your inputs.

  • Thanks 1
Link to comment
Share on other sites

3 hours ago, Lurcher said:

First of all, using a webhook like this is a great idea and makes discord integration very easy, so thank you for sharing.

However to anyone reading this, please, do not use any of these example Lua snippets directly, they are extremely unsafe. Filter your inputs.

Could you exlain how code not exposed to users is unsafe and who to? I dont see it myseĺf but if you need to dm me an example please do id rather make an edit to the op or explain it. 

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

I spoke to Lurcher and he pointed out a valid improvement (thanks) which I will work on. There are circumstances that the script would fail and cause a mess since this is dealing with commands these should fail earlier and do so cleanly, so I need to rewrite the code to clean up the inputs. I don't personally think this is sinister, but best to be watertight.

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

Hello I have updated the OP.
Firstly I've added a sample miz and script. This was because someone helpfully copied out my event handlers from the video and this was never intended. Subsequently people went through that code and identified some weaknesses which it then fell upon me to fix. This was however a valid excercise and important. Therefore I've set a sample that avoids the first ones shortfalls.
 

--added
function repl(dirty)
    local text = dirty:gsub("&", ""):gsub('"', ""):gsub("|", " "):gsub("'", ""):gsub("%%", ""):gsub("/", ""):gsub("\\", ""):gsub(">", "")
    local clean = text:gsub("<", "")
    return clean
end

function BotSay(msg)
local message=repl(msg)--added string replacement for some DOS escape characters
local text='C:\\Webhook\\DiscordSendWebhook.exe -m "'..message..'" -w "https://discord.com/api/webhooks/832742644517830667/blahblahblah" '         
os.execute(text)
end

 

  • Thanks 1

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

  • 11 months later...
55 minutes ago, hancerPL said:

I used the standard "botsay.lua" to change the webhook address. File "DSW.exe" as in the script, link "C: \ Webhook \ DiscordSendWebhook.exe"

botsay.lua 4.75 kB · 0 downloads

Are you running MOOSE in the mission?

I would suggest you structure a simple message sent from the mission to make sure everything works if you are already running MOOSE and getting an error.

If you arent running MOOSE, then your problem lies there. 
 

The unedited botsay requires MOOSE for the event handlers

 

 

 

 

EDsignaturefleet.jpg

Link to comment
Share on other sites

4 hours ago, hancerPL said:

Of course I'm using Moose

dcs.log 1.13 MB · 2 downloads

 

There are two errors repeating. Both in scheduler functions. One is probably in MOOSE from the line number, the other script is unknown (line 35 is producing the error). 
 

If you send the mission, I will look at. Barring that, it is just guesswork.

 

 

 

 

EDsignaturefleet.jpg

Link to comment
Share on other sites

  • 2 months later...
On 4/18/2021 at 8:06 PM, Pikey said:

Edited 3rd August
[On 2nd August I was made aware that if you copied my script from the video, which I never intended, you might copy an event handler I wrote, and in that could be a players name which, could include special characters which could break the command script and potentially cause issues. I have updated the script with a text replacement to remove some of the special characters that DOS uses as escape characters.

If you used this before you MUST UPDATE YOUR SCRIPT!!! I provided a sample attached to this post.] 


Hi folks,

What is this?: After reading this, you will be able to send messages to your Discord from a script running on your dedicated servers mission.


Right but that's super complex and I need to have a server with a Discord bot and i cannot be bothered.
Wrong, you can do it with one executable file and a short script (sample attached). Discord invented a technology which allowed messages to be sent to a channel using a temporary token and a URL that was too difficult to guess. This is a Webhook.

WARNING! Since the video I have updated the code. DO NOT COPY IT FROM HERE.

 



1. Get DiscordSendWebhook.exe from https://github.com/phoenix125/DiscordSendWebhook
2. Remove the Operating system sanitising in your missionscripting.lua (important - you must research this if it doenst make sense, it allows Lua to execute any command on your machine, thus, you need to be very aware of what you run in Lua and who from.)
3. Create a Webhook in your Discord server and copy the URL to line 22 where the Discord URL is
4. Customise a Lua Script from mine attached

5. Generate some code that makes some text and execute your function like BotSay("Have a jolly nice day you superstar!")

FAQ:
Do I need Mist or Moose or something?
No, although in the video I wrote my example Event Handler in Moose. However there are plenty of examples of mist, moose and native SSE Event Handlers. The attached miz and file has a selection of MOOSE event handlers.

What else can i do with this?
I researched this being unaware of webhooks and thinking this was complex to do. And i bet others have given up before realising its so easy. Some ideas I had were
~ Starting Scrambles from the desktop - you can provide the Bullseye right to discord with a Scramble message.
~ I did wonder if you can chain Discord bots, like using another bot to read my bot then act, like play a noise when I scrambled someone...
~ Recon info and BDA right to desktop
~ CSAR events (now the event for parachutists works!)
~ Server admin - server start/mission start
~ LSO trap scores
~ extending Discord as a middleware, perhaps taking mission information output and putting it elsewhere, maybe even a Google API or spreadsheet

Let me know what you come up with!

How can i trust the executable?
I'd suggest reading the uncompiled code and compiling it with Autoit yourself. It's likely to alarm some AV software because the heuristics in AV software hates Autoit. I dont guarantee this software in any way but I used it the last 6 months and I am still well!

Why do I see a black command box when this fires?

This is a feature of LUA and it sounds like you are testing this on a local SIngle Player test in the Mission Editor. Run it on a server and none of the code executes locally and won't bother you and you wont see it or feel any pause as the sim is tabbed out of focus by the command box.


Help!
No!

Thanks!
You are welcome. Go share your ideas and make the internet a useful place.

Thanks to Lurcher for the player name sanitization code!
 

botsay.miz 1.28 MB · 66 downloads botsay.lua 4.69 kB · 111 downloads

 

Hi @Pikey

Excellent work!, it works perfectly.
I added this event, it works fine too:

Spoiler

LU = EVENTHANDLER:New()
LU:HandleEvent(EVENTS.PlayerLeaveUnit)

function LU:OnEventPlayerLeaveUnit(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." has leaving slot or Server"
BotSay(txt)
end

But I have tried to add Refueling and StopRefueling events, but I have not been able to get them to be sent, they are these:

Spoiler

-- PLAYER REFUELING EVENT

RE = EVENTHANDLER:New()
RE:HandleEvent(EVENTS.Refueling)

function RE:OnEventRefueling(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." is refueling"
BotSay(txt)
end

-- PLAYER STOP REFUELING EVENT


RES = EVENTHANDLER:New()
RES:HandleEvent(EVENTS.RefuelingStop)

function RES:OnEventRefuelingStop(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." is stopping refueling"
BotSay(txt)
end

Can you help me?
Anyway, thank you very much

[sIGPIC][/sIGPIC]

Intel(R) Core(TM) i9-10900KF CPU @ 3.70GHz   3.70 GHz ROG STRIX Z490-E GAMING
RAM 128 M.2*2 2T total SSD*3 2.5T total
GeForce RTX 3090   Orion2 HOTAS F-16EX  Saitek Pro Rudder

Link to comment
Share on other sites

  • 2 weeks later...
On 9/27/2022 at 5:10 PM, Colmillo said:

Hi @Pikey

Excellent work!, it works perfectly.
I added this event, it works fine too:

  Reveal hidden contents

LU = EVENTHANDLER:New()
LU:HandleEvent(EVENTS.PlayerLeaveUnit)

function LU:OnEventPlayerLeaveUnit(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." has leaving slot or Server"
BotSay(txt)
end

But I have tried to add Refueling and StopRefueling events, but I have not been able to get them to be sent, they are these:

  Reveal hidden contents

-- PLAYER REFUELING EVENT

RE = EVENTHANDLER:New()
RE:HandleEvent(EVENTS.Refueling)

function RE:OnEventRefueling(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." is refueling"
BotSay(txt)
end

-- PLAYER STOP REFUELING EVENT


RES = EVENTHANDLER:New()
RES:HandleEvent(EVENTS.RefuelingStop)

function RES:OnEventRefuelingStop(EventData) 
local playername = EventData.IniPlayerName
local txt=playername.." is stopping refueling"
BotSay(txt)
end

Can you help me?
Anyway, thank you very much

Once you can send things the rest is DCS scripting and not to do with the DIscord webhook exe or how to make a webhook. So you should look for help with your scripts in the scripting section to find out what you did. Troubleshooting scripts very much requires people to read their dcs.log and look for errors.

  • Like 1

___________________________________________________________________________

SIMPLE SCENERY SAVING * SIMPLE GROUP SAVING * SIMPLE STATIC SAVING *

Link to comment
Share on other sites

  • 3 months later...

Saddly it stop working...
Only can figure out that may be was after this update:
January 10, 2023—KB5022286 (OS Build 17763.3887)

Cheers

[sIGPIC][/sIGPIC]

Intel(R) Core(TM) i9-10900KF CPU @ 3.70GHz   3.70 GHz ROG STRIX Z490-E GAMING
RAM 128 M.2*2 2T total SSD*3 2.5T total
GeForce RTX 3090   Orion2 HOTAS F-16EX  Saitek Pro Rudder

Link to comment
Share on other sites

5 hours ago, Colmillo said:

Saddly it stop working...
Only can figure out that may be was after this update:
January 10, 2023—KB5022286 (OS Build 17763.3887)

Cheers

Same not working for me anymore either.  Mission scripting is sanitized


Edited by Snoopy
Link to comment
Share on other sites

  • 1 month later...

I literally just found this and have not installed it yet, but:

https://github.com/phoenix125/DiscordSendWebhook

Looking at this, it should be easy to debug it via command line. There are examples there which show running it directly which takes DCS completely out of the equation. 

See if that still works, and we'll back into DCS from there. 

ETA - it's not working for me on the command line either. 

😒

We need to find  or create a different exe.  This one is not maintained anymore.

It can be done super simply with curl though.  This as a bat file works:

 

Quote

@echo off
set /p webhook=Webhook : 
:send
cls
set /p message=Message : 
curl -X POST -H "Content-type: application/json" --data "{\"content\": \"%message%\"}" %webhook%
goto :send
pause

 


Edited by fargo007

 

Banner EDForum2020.jpg

Have fun. Don't suck. Kill bad guys. 👍

https://discord.gg/blacksharkden/

Link to comment
Share on other sites

Quote

$msg = $args[0]
$webhook = "https://your_discord_webhook_URL..........................."


$body = @{
 "content"= $msg
} | ConvertTo-Json

$header = @{
 "Content-Type"="application/json"

Invoke-RestMethod -Uri $webhook -Method 'Post' -Body $body -Headers $header 

This is a powershell (ps1) script that I came up with that is working for me at least on the command line. 

 


Edited by fargo007

 

Banner EDForum2020.jpg

Have fun. Don't suck. Kill bad guys. 👍

https://discord.gg/blacksharkden/

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...