Skip to content

Commit

Permalink
Added SQF building
Browse files Browse the repository at this point in the history
Moved config building into its own function
  • Loading branch information
Connor committed Aug 26, 2018
1 parent be42a51 commit a1a7c9a
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 70 deletions.
2 changes: 2 additions & 0 deletions addon/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CfgFunctions {
class Scripts {
file = "\DiscordEmbedBuilder";
class init {preInit=1;};
class buildCfg {};
class buildSqf {};
class send {};
};
};
Expand Down
69 changes: 69 additions & 0 deletions addon/fn_buildCfg.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*──────────────────────────────────────────────────────┐
│ Author: Connor │
│ Steam: https://steamcommunity.com/id/_connor │
│ Github: https://github.com/ConnorAU │
│ │
│ Please do not modify or remove this comment block │
└──────────────────────────────────────────────────────*/

// The extension deserializer doesn't like empty strings, so this string is reserved as empty
#define EMPTY_STRING "DEB_EMPTY_STR"

if !(uiNamespace getVariable ["DiscordEmbedBuilder_LoadSuccess",false]) exitwith {false};
params [["_template","",[""]],["_textParameters",[],[[]]]];

private _cfgDir = configFile >> "CfgDiscordEmbedBuilder" >> _template;
if (isNull _cfgDir) exitwith {false};

private _emptyString = { [EMPTY_STRING,_this] select (_this != ""); };
private _formatString = {
params [["_str","",[""]],["_replaceEmpty",true,[true]]];
private _str = format([_str]+_textParameters);
if _replaceEmpty then {_str call _emptyString} else {_str}
};

private _webhookurl = getText(_cfgDir >> "webhookurl");
private _message = [getText(_cfgDir >> "message"),false] call _formatString;
private _username = [getText(_cfgDir >> "username"),false] call _formatString;
private _avatar = getText(_cfgDir >> "avatar");
private _tts = getNumber(_cfgDir >> "tts") isEqualTo 1;
private _embeds = "true" configClasses (_cfgDir >> "embeds");

private ["_title","_description","_url","_color","_timestamp","_thumbnail","_image","_timestamp","_authorName","_authorURL","_authorImage","_author","_footerText","_footerImage","_footer","_fields"];
{
_title = getText(_x >> "title") call _formatString;
_description = getText(_x >> "description") call _formatString;
_url = getText(_x >> "url") call _emptyString;
_color = getText(_x >> "color") call _formatString;
_timestamp = getNumber(_x >> "timestamp");
_thumbnail = getText(_x >> "thumbnail") call _emptyString;
_image = getText(_x >> "image") call _emptyString;

// tobool
_timestamp = _timestamp isEqualTo 1;

// Author things
_authorName = getText(_x >> "Author" >> "name") call _formatString;
_authorURL = getText(_x >> "Author" >> "url") call _emptyString;
_authorImage = getText(_x >> "Author" >> "image") call _emptyString;
_author = [[],[_authorName,_authorURL,_authorImage]] select (_authorName != EMPTY_STRING);

// Footer things
_footerText = getText(_x >> "Footer" >> "text") call _formatString;
_footerImage = getText(_x >> "Footer" >> "image") call _emptyString;
_footer = [[],[_footerText,_footerImage]] select (_footerText != EMPTY_STRING);

// Field things
_fields = getArray(_x >> "fields") apply {
_x params [
["_fieldName","",[""]],
["_fieldContent","",[""]],
["_fieldInline",0,[0]]
];
[_fieldName call _formatString,_fieldContent call _formatString,_fieldInline isEqualTo 1];
};

_embeds set [_forEachIndex,[_title,_description,_url,_color,_timestamp,_thumbnail,_image,_author,_footer,_fields]];
} foreach _embeds;

[_webhookurl,_message,_username,_avatar,_tts,_embeds] call DiscordEmbedBuilder_fnc_send;
155 changes: 155 additions & 0 deletions addon/fn_buildSqf.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*──────────────────────────────────────────────────────┐
│ Author: Connor │
│ Steam: https://steamcommunity.com/id/_connor │
│ Github: https://github.com/ConnorAU │
│ │
│ Please do not modify or remove this comment block │
└──────────────────────────────────────────────────────*/

/*
Example 1:
[
"Example",
"This is an example message",
"Example Name",
"https://discordapp.com/assets/2c21aeda16de354ba5334551a883b481.png",
false
] call DiscordEmbedBuilder_fnc_buildSqf;
Example 2:
[
"Example",
"This is another example message",
"Different username, same webhook",
"",
false,
[ // Maximum 10 embeds per message
[
"My Embed Title",
"My Embed Description",
"https://arma3.com/",
"00FF00",
true,
"https://arma3.com/assets/img/wallpapers/1/9/arma3_white_plain_800x600.jpg",
"https://arma3.com/assets/img/wallpapers/low/3/Arma%203%20Laws%20of%20War_wallpaper_1024x768.jpg",
[
profileName,
"https://steamcommunity.com/id/_connor",
"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/1e/1e3c83b65d6f34cc9708eae853e8bc9848865dd1_full.jpg"
],
[
"This is the footer text",
"https://steamcdn-a.opskins.media/steamcommunity/public/images/apps/107410/3212af52faf994c558bd622cb0f360c1ef295a6b.jpg"
],
[ // Maximum 25 fields per embed
["Field 1","Content 1",true],
["Field 2","Some words",true],
["Field 3",":+1:",false],
["Field 4","__Markdown works here__",false]
]
],
[
"Second embed",
"This embed uses [productVersion](https://community.bistudio.com/wiki/productVersion) as the sqf parameter",
"",
"",
true,
"",
"",
[],
[],
[
["Product Name",format["%1",productVersion select 0],true],
["Product Name Short",format["%1",productVersion select 1],true],
["Product Version",format["%1",productVersion select 2],true],
["Product Build",format["%1",productVersion select 3],true],
["Product Branch",format["%1",productVersion select 4],true],
["Has Mods",format["%1",productVersion select 5],true],
["Platform",format["%1",productVersion select 6],true],
["Architecture",format["%1",productVersion select 7],true],
["All",format["You can use sqf parameters in the message, username, embed title, embed description, author name, footer text, field name and field content.\n**Discord** markdown `is` also _supported_.\n```sqf\n%1```",productVersion],false]
]
]
]
] call DiscordEmbedBuilder_fnc_buildSqf;
*/

// The extension deserializer doesn't like empty strings, so this string is reserved as empty
#define EMPTY_STRING "DEB_EMPTY_STR"

if !(uiNamespace getVariable ["DiscordEmbedBuilder_LoadSuccess",false]) exitwith {false};

private _emptyString = { [EMPTY_STRING,_this] select (_this != ""); };

params [
["_webhookName","",[""]],
["_message","",[""]],
["_username","",[""]],
["_avatar","",[""]],
["_tts",false,[true]],
["_embeds",[],[[]]]
];

// Webhook URL wouldn't be safe in client files so we keep it in the mod instead
private _webhookurl = switch _webhookName do {
// https://discordapp.com/api/webhooks/000000000000000000/XXXXXXX_XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
case "Example":{"000000000000000000/XXXXXXX_XXXXXXXXXXXXXXX_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"};
default {_webhookName}; // If we can't find a matching case, assume the webhook name is the webhook url (not recommended)
};

{
_x params [
["_title","",[""]],
["_description","",[""]],
["_url","",[""]],
["_color","",[""]],
["_timestamp",false,[true]],
["_thumbnail","",[""]],
["_image","",[""]],
["_author",[],[[]]],
["_footer",[],[[]]],
["_fields",[],[[]]]
];

// Bare bones
_title = _title call _emptyString;
_description = _description call _emptyString;
_url = _url call _emptyString;
_color = _color call _emptyString;
_thumbnail = _thumbnail call _emptyString;
_image = _image call _emptyString;

// Author things
_author params [
["_authorName","",[""]],
["_authorURL","",[""]],
["_authorImage","",[""]]
];
_authorName = _authorName call _emptyString;
_authorURL = _authorURL call _emptyString;
_authorImage = _authorImage call _emptyString;
_author = [[],[_authorName,_authorURL,_authorImage]] select (_authorName != EMPTY_STRING);

// Footer things
_footer params [
["_footerName","",[""]],
["_footerImage","",[""]]
];
_footerName = _footerName call _emptyString;
_footerImage = _footerImage call _emptyString;
_footer = [[],[_footerName,_footerImage]] select (_footerName != EMPTY_STRING);

// Field things
_fields = _fields apply {
_x params [
["_fieldName","",[""]],
["_fieldContent","",[""]],
["_fieldInline",false,[true]]
];
[_fieldName call _emptyString,_fieldContent call _emptyString,_fieldInline];
};

_embeds set [_forEachIndex,[_title,_description,_url,_color,_timestamp,_thumbnail,_image,_author,_footer,_fields]];
} foreach _embeds;

[_webhookurl,_message,_username,_avatar,_tts,_embeds] call DiscordEmbedBuilder_fnc_send;
80 changes: 10 additions & 70 deletions addon/fn_send.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,16 @@
│ Please do not modify or remove this comment block │
└──────────────────────────────────────────────────────*/

// The extension deserializer doesn't like empty strings, so this string is reserved as empty
#define EMPTY_STRING "DEB_EMPTY_STR"

if !(uiNamespace getVariable ["DiscordEmbedBuilder_LoadSuccess",false]) exitwith {};
params [["_template","",[""]],["_textParameters",[],[[]]]];

private _cfgDir = configFile >> "CfgDiscordEmbedBuilder" >> _template;
if (isNull _cfgDir) exitwith {false};

private _formatString = {
private _s = format([_this]+_textParameters);
[EMPTY_STRING,_s] select (_s != "");
};

private _webhookurl = getText(_cfgDir >> "webhookurl");
private _message = getText(_cfgDir >> "message") call _formatString;
private _username = getText(_cfgDir >> "username") call _formatString;
private _avatar = getText(_cfgDir >> "avatar");
private _tts = getNumber(_cfgDir >> "tts") isEqualTo 1;
private _embeds = "true" configClasses (_cfgDir >> "embeds");

private ["_title","_description","_url","_color","_timestamp","_thumbnail","_image","_timestamp","_authorName","_authorURL","_authorImage","_author","_footerText","_footerImage","_footer","_fields"];
{

_title = getText(_x >> "title") call _formatString;
_description = getText(_x >> "description") call _formatString;
_url = getText(_x >> "url");
_color = getText(_x >> "color");
_timestamp = getNumber(_x >> "timestamp");
_thumbnail = getText(_x >> "thumbnail");
_image = getText(_x >> "image");

if (_url == "") then {_url = EMPTY_STRING};
if (_color == "") then {_color = EMPTY_STRING};
if (_thumbnail == "") then {_thumbnail = EMPTY_STRING};
if (_image == "") then {_image = EMPTY_STRING};

// tobool
_timestamp = _timestamp isEqualTo 1;

// Author things
_authorName = getText(_x >> "Author" >> "name") call _formatString;
_authorURL = getText(_x >> "Author" >> "url");
_authorImage = getText(_x >> "Author" >> "image");
if (_authorURL == "") then {_authorURL = EMPTY_STRING};
if (_authorImage == "") then {_authorImage = EMPTY_STRING};
_author = [[],[_authorName,_authorURL,_authorImage]] select (_authorName != "");

// Footer things
_footerText = getText(_x >> "Footer" >> "text") call _formatString;
_footerImage = getText(_x >> "Footer" >> "image");
if (_footerImage == "") then {_footerImage = EMPTY_STRING};
_footer = [[],[_footerText,_footerImage]] select (_footerText != "");

// Field things
_fields = getArray(_x >> "fields") apply {
_x params [
["_fieldName","",[""]],
["_fieldContent","",[""]],
["_fieldInline",0,[0]]
];

if (_fieldName == "") then {_fieldName = EMPTY_STRING};
if (_fieldContent == "") then {_fieldContent = EMPTY_STRING};

[_fieldName call _formatString,_fieldContent call _formatString,_fieldInline isEqualTo 1];
};

_embeds set [_forEachIndex,[_title,_description,_url,_color,_timestamp,_thumbnail,_image,_author,_footer,_fields]];
} foreach _embeds;
if !(uiNamespace getVariable ["DiscordEmbedBuilder_LoadSuccess",false]) exitwith {false};

params [
["_webhookurl","",[""]],
["_message","",[""]],
["_username","",[""]],
["_avatar","",[""]],
["_tts",false,[true]],
["_embeds",[],[[]]]
];

private _key = uiNamespace getVariable ["DiscordEmbedBuilder_SessionKey",{""}];
"DiscordEmbedBuilder" callExtension [call _key,[_webhookurl,_message,_username,_avatar,_tts,_embeds]];
Expand Down

0 comments on commit a1a7c9a

Please sign in to comment.