Paste #90440: dTagParser

Date: 2021/11/30 20:28:21 UTC-08:00
Type: Denizen Script

View Raw Paste Download This Paste Edit Of Paste 83975 View Edit Report
Copy Link


tag_parser_bot:
    type: world
    debug: false
    samples:
        link: (UNSET)
        sample_bool: true
        sample_0: 0
        sample_1: 1
        sample_10: 10
        sample_0p5: 0.5
        sample_text: Hello, world!
        help: try typing 'help' to the bot!
    events:
        after server start:
        - wait 5s
        - flag server tag_parser_bot_running:!
        - inject tag_parser_start_bot_task
        on script generates error:
        - flag server tag_parser_result_temp.failed:|:Error=<context.message.strip_color>
        on server generates exception:
        - flag server tag_parser_result_temp.failed:|:Exception=<context.type>-<context.message>
        after discord message received for:tag_parser:
        - flag server tag_parser_bot_running
        - if !<context.new_message.mentioned_users.parse[id].contains[<context.bot.self_user.id>]>:
            - stop
        # Ensure it's an actual '@' not just a reply
        - if !<context.new_message.text.contains_text[<&lt>@<context.bot.self_user.id><&gt>]> && !<context.new_message.text.contains_text[<&lt>@!<context.bot.self_user.id><&gt>]>:
            - stop
        - definemap args:
            author: <context.new_message.author>
            channel: <context.channel>
            to_parse: <context.new_message.text_no_mentions>
            reply_to: <context.new_message>
            handle_output: tag_parser_message_reply_task
        - run tag_parse_core_task defmap:<[args]>
        on discord slash command name:parsetag:
        - ~discordinteraction defer interaction:<context.interaction>
        - definemap args:
            author: <context.interaction.user>
            channel: <context.channel>
            to_parse: <context.options.get[tag].if_null[<empty>]>
            reply_to: <context.interaction>
            handle_output: tag_parser_interaction_reply_task
            handle_fail: tag_parser_interaction_fail_task
        - run tag_parse_core_task defmap:<[args]>

tag_parser_stop_bot_task:
    type: task
    debug: false
    script:
    - if <server.has_flag[tag_parser_bot_running]>:
        - ~discord id:tag_parser disconnect
        - flag server tag_parser_bot_running:!

tag_parser_start_bot_task:
    type: task
    debug: false
    script:
    - inject tag_parser_stop_bot_task
    - yaml load:data/tag_parser.yml id:tag_parser_temp
    - if !<yaml.list.contains[tag_parser_temp]>:
        - debug error "Load failed."
        - stop
    - flag server tag_parser_logpath:<yaml[tag_parser_temp].read[bot.discord.log_path]>
    - flag server tag_parser_validchannels:!
    - flag server tag_parser_validchannels:|:<yaml[tag_parser_temp].read[bot.discord.valid_channels]>
    - flag server tag_parser_barredchannels:!
    - flag server tag_parser_barredchannels:|:<yaml[tag_parser_temp].read[bot.discord.barred_channels]>
    - flag server tag_parser_barredchannelmessages:!
    - flag server tag_parser_barredchannelmessages:|:<yaml[tag_parser_temp].read[bot.discord.barred_channel_messages]>
    - flag server tag_parser_link:<yaml[tag_parser_temp].read[script.link]>
    - yaml unload id:tag_parser_temp
    - define samples <script.data_key[samples].with[link].as[<server.flag[tag_parser_link]>].with[version].as[<server.version>].with[denizen_version].as[<server.denizen_version>]>
    - flag server tag_parser_samples:<[samples]>
    - ~discordconnect id:tag_parser tokenfile:data/tagparser_token.txt
    - flag server tag_parser_bot_running

tag_parse_core_task:
    type: task
    debug: false
    definitions: author|channel|to_parse|reply_to|handle_output|handle_fail
    script:
    - define name <[author].id>/<[author].name>
    - define origin <[channel].group>/<[channel]>/<[channel].group.name>/<[channel].name>
    - if !<server.flag[tag_parser_validchannels].contains[<[channel].id>]>:
        - define found <server.flag[tag_parser_barredchannels].find[<[channel].id>]>
        - if <[found]> > 0:
            - ~run <[handle_output]> def.reply_to:<[reply_to]> "Cannot parse that in this channel! <server.flag[tag_parser_barredchannelmessages].get[<[found]>]>"
        - else:
            - if <[handle_fail].exists>:
                - run <[handle_fail]> def.reply_to:<[reply_to]>
        - stop
    - if <[to_parse].trim> == link:
        - ~run <[handle_output]> def.reply_to:<[reply_to]> "def.message:The source script for **dTagParser** can be found at <&lt><server.flag[tag_parser_samples].as_map.get[link]><&gt>"
        - stop
    - waituntil rate:1s !<server.has_flag[tag_parser_bot_processing_now]>
    - flag server tag_parser_bot_processing_now duration:1m
    - define tag <[to_parse].replace_text[<n>].with[<&sp>].trim>
    - inject tag_parser_process_tag_task player:<server.players.get[1]> npc:<server.npcs.get[1]>
    - inject tag_parser_crunch_result_task
    - ~run <[handle_output]> def.reply_to:<[reply_to]> "def.message:Tag parse results for `<[tag].replace_text[`].with[']>`<&co> <[result].get[recording].if_null[]><n>```<n><[final_result].replace_text[`].with[']>```"
    - flag server tag_parser_bot_processing_now:!

tag_parser_message_reply_task:
    type: task
    debug: false
    definitions: reply_to|message
    script:
    - ~discordmessage id:tag_parser reply:<[reply_to]> <[message]>
    - wait 1t

tag_parser_interaction_reply_task:
    type: task
    debug: false
    definitions: reply_to|message
    script:
    - ~discordinteraction reply interaction:<[reply_to]> <[message]>
    - wait 1t

tag_parser_interaction_fail_task:
    type: task
    debug: false
    definitions: reply_to
    script:
    - ~discordinteraction delete interaction:<[reply_to]>
    - wait 1t

tag_parser_process_tag_task:
    type: task
    debug: false
    definitions: name|origin|tag|channel
    script:
    - log "<util.time_now.format> [<[name]>] in [<[origin]>] wants to process tag <[tag]>" file:<server.flag[tag_parser_logpath]>
    - flag server tag_parser_result_temp:!
    # If you ever enable this on your own copy of the bot I will break your knees. Don't do it. Official dTagParser instance only.
    - if <server.has_flag[dtagparser_allowed_to_record_ever]>:
        - inject dtag_determine_shouldrecord
    - else:
        - define should_record false
    - ~run tag_run_task def:<list_single[<[tag]>].include[<[should_record]>]>
    - define result <server.flag[tag_parser_result_temp].if_null[<map.with[failed].as[FAILED=FLAG_MISSING]>]>
    - flag server tag_parser_result_temp:!
    - log "<util.time_now.format> [<[name]>] in [<[origin]>] got result <[result]>" file:<server.flag[tag_parser_logpath]>

tag_run_task:
    type: task
    definitions: tag|record
    debug: false
    script:
    - define samples <server.flag[tag_parser_samples].as_map.with[sample_player].as[<player>].with[sample_npc].as[<npc>]>
    - foreach <[samples].keys> as:key:
        - define <[key]> <[samples].get[<[key]>]>
    - define help "Tell me any valid Denizen tags, like <&lt>player.name<&gt> and I'll parse them for you! Alternately, tell me a valid definition name (like 'samples') and I'll tell you its contents."
    - if !<[tag].contains_text[<&lt>]> && <[<[tag]>].exists>:
        - flag server tag_parser_result_temp.valid:<[<[tag]>]>
    - else:
        - if <[record]>:
            - inject dtag_record_start
        - inject tag_parser_task
        - if <[record]>:
            - inject dtag_record_finish
        - flag server tag_parser_result_temp.valid:<[result]>

tag_parser_task:
    type: task
    definitions: tag
    script:
    - define result <[tag].parsed>

tag_parser_crunch_result_task:
    type: task
    debug: false
    definitions: tag|result
    script:
    - define final_result <empty>
    - if <[result].contains[valid]>:
        - define final_result <[result].get[valid]><n>
    - if <[result].contains[failed]>:
        - foreach <[result].get[failed]>:
            - if <[value].starts_with[Error=]>:
                - define final_result "<[final_result]>Had error: <[value].after[Error=]><n>"
            - else if <[value].starts_with[Exception=]>:
                - define final_result "<[final_result]>Had internal exception: <[value].after[Exception=]><n>"
            - else if <[value].starts_with[FAILED=]>:
                - define fail_reason <[value].after[FAILED=]>
                - if <[fail_reason]> == FLAG_MISSING:
                    - define final_result "<[final_result]>Got no result value.<n>"
                - else:
                    - define final_result "<[final_result]>Got failure '<[fail_reason]>'.<n>"
    - if <[final_result].trim.length> == 0:
        - define final_result <empty>
    - if <[final_result].length> > 1000 || <[tag].length.add[<[final_result].length>]> > 1500:
        - define tag (Spam)
        - define final_result "Input too long, refused."
    - if <[final_result].to_list.filter[is[==].to[<n>]].size> > 10:
        - define final_result "Newline spam, refused."

# This is enabled via /ex run tag_parser_generate_command
# only needs to be ran once ever
tag_parser_generate_command:
    type: task
    debug: false
    script:
    - definemap options:
        1:
            type: string
            name: tag
            description: The tag to be parsed.
            required: true
    - ~discordcommand id:tag_parser create name:parsetag "description:Parses a Denizen tag insta-magically on Discord." options:<[options]>
    - narrate "Command registered"