Skip to content

Commit

Permalink
fux
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Dec 31, 2024
1 parent 9efd70e commit 9206982
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ members = [
"services/template-worker/crates/*",
]
exclude = [
"infra/templating-types/darklua" # Avoid workspace conflicts
"infra/templating-types/darklua", # Avoid workspace conflicts
"infra/templating-template/darklua"
]
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion docs/src/user/templating/2-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ Represents a sting on AntiRaid
"creator": "system",
"target": "user:1945824",
"state": "active",
"created_at": "2024-12-30T17:10:22.259357866Z",
"created_at": "2024-12-31T10:45:59.302502629Z",
"duration": {
"secs": 60,
"nanos": 0
Expand Down
57 changes: 14 additions & 43 deletions docs/src/user/templating/3-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Next, we create the embed using the events ``title`` field as the embed title an
local embed = {
title = args.title,
description = "", -- Start with empty description
fields = {}, -- Start with empty fields
}
```

Expand All @@ -53,68 +54,38 @@ for key, value in pairs(my_table) do
end
```

#### 3. Typing (optional)
#### 3. Creating the audit log message fields etc.

The next step is to add fields to the embed by actually handling the Discord events we want properly. This is pretty annoying to do without some typing and helper methods though...

Fortunately, AntiRaid has a solution: [templating-types](https://github.com/Anti-Raid/templating-types)! This does require extra effort in the form of bundling.
Fortunately, AntiRaid has a solution: [templating-types](https://github.com/Anti-Raid/templating-types)! This does require extra effort in the form of bundling with [templating-template](https://github.com/Anti-Raid/templating-template) as a template. See [our guide](./4-bundling.md) for more information on how to do this.

Once you're done making the message as you'd like. It's time to send the message!

#### 4. Sending the message

Finally, we can send the message using the Discord module. To do this, we need to create a message object and set the embeds property to an array containing our embed. This is also where interop comes in handy, as we need to set the metatable of the embeds array to the interop array metatable so AntiRaid knows that embeds is an array. Next, we use the Discord plugin to make a new Discord action executor which then lets us send the message to the specified channel (`args.sink` is another Audit Log specific variable that contains the channel ID/whatever `sink` is set to in the database).
Finally, we can send the message using the Discord module. To do this, we need to create a message object and set the embeds property to an array containing our embed. This is also where interop comes in handy, as we need to set the metatable of the embeds array to the interop array metatable so AntiRaid knows that embeds is an array. Next, we use the Discord plugin to make a new Discord action executor which then lets us send the message to the specified channel.

```lua
local message = { embeds = {} }
setmetatable(message.embeds, interop.array_metatable)

table.insert(message.embeds, embed)

-- Send message using action executor
local discord_executor = discord.new(token);
discord_executor:create_message({
channel_id = args.sink,
channel_id = "CHANNEL_ID_HERE",
message = message
})
```

Finally, we can put the entire loop together as so:

```lua
-- @pragma {"lang":"lua","allowed_caps":["discord:create_message"]}
local args, token = ...
local discord = require "@antiraid/discord"
local interop = require "@antiraid/interop"
local formatter = require "@antiraid/formatters"

-- Make the embed
local embed = {
title = args.event_titlename,
description = "", -- Start with empty description
}

-- Add the event data to the description
for key, value in pairs(args.event_data) do
local should_set = false
### 5. (Optional) Key-value

if value ~= nil and value.type ~= "None" then
should_set = true
end
With ``@antiraid/kv``, you can save the channel id to a key-value store in the website and then fetch it like so:

if should_set then
local formatted_value = formatter.format_gwevent_field(value)
embed.description = embed.description .. "**" .. key:gsub("_", " "):upper() .. "**: " .. formatted_value .. "\n"
end
end

local message = { embeds = {} }
setmetatable(message.embeds, interop.array_metatable)

table.insert(message.embeds, embed)
```lua
local kv = require "@antiraid/kv"

-- Send message using action executor
local discord_executor = discord.new(token);
discord_executor:create_message({
channel_id = args.sink,
message = message
})
```
local kvExecutor = kv.new(token)
local channelId = kvExecutor:get("auditlog_channel")
```
2 changes: 1 addition & 1 deletion infra/templating-template
2 changes: 1 addition & 1 deletion infra/templating-types

0 comments on commit 9206982

Please sign in to comment.