Inventor-U - Audit Log Usage - 24JUL24

In this session of Inventor University, we will go over how to use the Discord Audit Log and it’s related fields then how to listen for only the type(s) of entries we want and perform a different action from there. In this lesson, we will be learning how to use the Send Network Request and Parse JSON blocks in relation to the Discord Audit Log

Minimum requirements
Inventor bot with:

  1. At least 2 free component spaces (4 recommended)
  2. At least 50 unused storage crates

This is a multi-part tutorial, so you may need to click View the discussion on this topic below to see the full instructions.

What we learned in the lesson of the 24th

  • How to read the Discord Developer Portal: We briefly went over how to use the search bar functionality in the Discord Developer Portal to search for the field or endpoint we want for the function we want
  • How to use the Inventor Message Cache: We covered how to go into your Inventor settings and enable certain features as well as how to edit them for our specific needs and circumstances

Setup

  1. Make a database with the rows of; server_id, message_logs. Or use an existing database if one exists
  2. Enable your bot’s message cache at https://inventor.gg/bots/settings/msgcache
  3. Slash command flow to enable/disable the logging
  4. Create a flow triggered by a message being edited
  5. Create a flow for triggered by a message being deleted

Optional

Make a custom block to handle your audit log requests

Slash command flow

1) Make the command name setup with the subcommand being logging
2) Add two options to the command
2.1) Add a text option and label it disable-logging with a choice of Message Logging
2.2) Add a channel option and name it message
3) Add a Lookup by Value or Create block to the flow, set the database to the logging channels database and use the server_id column as the column to search for
4) Create a local variable and call it: changes. Make the Variable Value “No changes were made
5) Create an if statement
5.1) Set Value 1 as Slash Command - disable-logging ([[ 0.disable-logging ]]), the Comparison as equals (case insensitive) and leave Value 2 blank
5.2) Add an Update Row block and set the database to the same as before. Set the keep value if blank to true and put the Slash command channel ID for the option in the correct row. (repeat for however many options you have)
(all database references in this flow will be referring to the logging database created before)
5.3) Add another if statement to check if the row exists in the database
5.3.1) Value 1: Lookup by Value or Create - Row Created, Comparison: equals (case insensitive), Value 2: false
5.3.2) Add an Update Local Variable block, select the only variable in the dropdown then make the Variable Value, Logging Updated
5.4) Duplicate step 5.3 and change the Value 2 in the if block to “true” and the Variable Value in the Update Local Variable block to “Logging Created
6) Add an if block and check if the disable logging option is not blank
6.1) Add another if and check to see if the feature to be disabled is the Message Logging. If it is, update the row for messages and set the new value to “Disabled” (important later) and update the local variable to reflect
7) Text Reply to Interaction and send the local variable









Message edited flow

1) Add a Lookup by Values block and use the database mentioned in the setup
2) Add an Advanced Conditional set to any with Lookup by Values equals (case insensitive) (leave this blank) and Lookup by Values equals (case insensitive) “Disabled
2.1) Exit flow
3) Get User and use the Message Edited - Author ID ([[ 0.Author ID ]]) as the User ID
4) Get Channel with the Message Edited - Channel ID ([[ 0.Channel ID ]]) as the ID of the channel to get
5) Embed Field Builder with the fields of
5.1) Field 1
5.1.1) Name: Before
5.1.2) Value: Message Edited - Previous Message Content ([[ 0.Previous Message Content ]]f
5.2) Field 2
5.1.1) Name: After
5.1.2) Value: Message Edited - Message Content ([[ 0.Message Content ]])
6) Add a Send or Edit Embed Message and set the channel to the message logging channel from the database and add the Field builder from step 5 into the slot labelled “Field Builder Data





Message deleted flow

1) Add a Lookup by Values block and use the database mentioned in the setup
2) Add an Advanced Conditional set to any with Lookup by Values equals (case insensitive) (leave this blank) and Lookup by Values equals (case insensitive) “Disabled
2.1) Exit flow
3) Get User and use the Message Edited - Author ID ([[ 0.Author ID ]]) as the User ID
4) Get Channel with the Message Edited - Channel ID ([[ 0.Channel ID ]]) as the ID of the channel to get
5) Create Local Variable and set the value to the message to display when a user deletes their own message
6) Create a folder. (This is where the Audit Logging API will take place)
6.1) Get Token block
6.2) Get Current Date and Time block
6.3) Send Network Request, type is “GET” (More info at bottom of this instruction panel)
6.4) Parse JSON block with the JSON String being the Response Body from step 5.3 and the Parse Value being: “audit_log_entries[0].id
6.5) Parse JSON block with the JSON String being the Response Body from step 5.3 and the Parse Value being: “audit_log_entries[0].type
6.6) Parse JSON block with the JSON String being the Response Body from step 5.3 and the Parse Value being: “audit_log_entries[0].target_id
6.7) Parse JSON block with the JSON String being the Response Body from step 5.3 and the Parse Value being: “audit_log_entries[0].user_id
6.8) Get Snowflake Timestamp
6.9) Math Operations. Value 1: Get Current Date and Time, Operation: - (subtraction), Value 2: Get Snowflake Timestamp. Use unix for all
6.10) Advanced Conditional set to All of the following
6.10.1) Message Deleted - Author ID equals step 6.6
6.10.2) Step 6.5 equals, 72
6.10.3) Step 6.7 does not equal Message Deleted - Author ID
6.10.4) Step 6.9 less than or equal to 2
If all conditions are met: Update local variable to the message when a mod deletes a message
7) Add a Embed Field Builder block and set Field 1 as
7.1) Name: Message Content
7.2) Value: Message Deleted - Previous Message Content ([[ 0.Previous Message Content ]])
8) Add a Send or Edit Embed Message and set the channel to the message logging channel from the database and add the Field builder from step 7 into the slot labelled “Field Builder Data” with the Body of the message being the Local Variable created in step 5









Message Deleted Flow - Step 6.3

Type: GET
URL https://discord.com/api/v10/guilds/[[ 0.Server ID ]]/audit-logs?limit=1
Request Headers User-Agent=Inventor (https://inventor.gg, V1) Authorization=Bot {Get Token - Token}
Replace the {Get Token - Token} with the value from the Get Token block labelled in step 6.1