创建自定义自动化

是否可以毫无问题地创建自己的脚本?只需将其放入文件夹中,并且随着插件更新,不会发生任何事情?或者我必须创建一个与此插件交互的插件?例如,我需要一个脚本来创建主题。有一个帖子创建脚本,但我需要创建自动主题。谢谢!

2 个赞

是的,它需要一个插件来定义自定义脚本。

类似这样写在 plugin.rb 中:

after_initialize do
  if defined?(DiscourseAutomation)
    add_automation_scriptable("my_custom_script") do
      # ...
    end
  end
end
3 个赞

我试过了,但它显示:

找不到自动化 testing 的脚本 lb-script,请确保已安装相关插件

也许我错了,但我应该创建一个单独的文件,还是可以把所有东西都放在 plugin.rb 文件里?

1 个赞

给我看些代码,否则我很难帮助你 :slight_smile:

1 个赞

我刚刚尝试使用了从 Automation 插件的脚本文件夹中复制的后置脚本:

after_initialize do
    if defined?(DiscourseAutomation)
        add_automation_scriptable("lb-script") do
            version 1

            placeholder :creator_username
          
            field :creator, component: :user
            field :topic, component: :text, required: true
            field :post, component: :post, required: true
          
            triggerables %i[recurring point_in_time]
          
            script do |context, fields, automation|
              creator_username = fields.dig("creator", "value") || Discourse.system_user.username
          
              placeholders = { creator_username: creator_username }.merge(context["placeholders"] || {})
          
              creator = User.find_by!(username: creator_username)
          
              PostCreator.new(
                creator,
                topic_id: fields.dig("topic", "value"),
                raw: fields.dig("post", "value"),
              ).create!
            end
        end
    end
end

我不太清楚你在这里做什么,这里有一个外部插件的完整示例:discourse-assign/plugin.rb at main · discourse/discourse-assign · GitHub

1 个赞

我不明白与我所做的有什么区别。显然,我使用了后置脚本只是为了看看它是否有效。但是当我创建自动化时,它找不到任何触发器,并且说它找不到脚本。但是代码应该允许我在两个触发器之间进行选择来创建帖子,对吗?我肯定错了,但我不太明白。

您的新插件已启用,您确定吗?

1 个赞

是的,当然。它已完全启用。

您能试试本地分配,看看能否在列表中显示吗?如果不能,那就有别的问题了。

我可以确认,即使未启用 Assign 插件,Random Assign 也能正常工作。可能是我遗漏了什么,我不想浪费您的时间。

3 个赞

我弄清楚问题出在哪里了。

我将 lb-script 改为了 lb_script,现在可以正常工作了。显然,不能使用连字符(-)符号。

3 个赞