3

我的问题是第一组返回会在 $sub1 可用于字符串匹配之前删除它,因此脚本不会继续。我试图将脚本的其余部分包含在第一组返回中并且它可以工作,但是......我收到多条消息给用户和其他 2 组返回的通道。

无论如何要修复它,以便它不会向用户和频道发送多条消息,因为“foreach sub”中的每个 sub 正在为 pm user 和 pm channel 生成一条线,该线可以是 1 或 2 条消息到200 条消息,取决于数据库中有多少匹配项。

bind pubm - * bind_pubm
    proc bind_pubm {nick uhost handle channel text} {
        global SQL; sql:start

        set regexp {^!sub (.*) *$}
        if {[regexp -nocase -- $regexp $text -> subscription]} {
            if {[string match -nocase *HDTV* $subscription] || [string match -nocase *S??E??* $subscription]} {

            set return [mysqlsel $SQL(conn) "select sub from DB_Subs where category='TV'" -list] 
            foreach {sub} $return {     ; # Lists all the subs in the DB for matching.
            regsub -all -- {%} $sub "*" sub1    ; # Replaces % in SQL to * for the String match later.
            }

            if {[string match -nocase $sub1* $subscription]} {  ; # Matches if a sub in the previous list matches the regexp subscription fromthe beginging of proc.
                set return [mysqlsel $SQL(conn) "select user from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -list]
                    foreach line $return {
                        foreach {user} $line break
                            if {[onchan $user $beta]} {     ; # If the user is on the channel it PM each user.
                                putnow "PRIVMSG $nick : Subscription found matching your request."
                            } else {
                        }
                    }

            set return [mysqlsel $SQL(conn) "select count(DISTINCT user) from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -flatlist] ; # Counts the users for the Channel message.
                foreach {users} $return break
                    putnow "PRIVMSG $beta :SUBSCRIPTION: $users Users With Subscriptions for $subscription"
            } else {
            }
        } else {
        }
    }
}

很难解释我到底想要完成什么。

最后我试图达到的结果是......

  • 从正则表达式设置 $subscription
  • 列出数据库中的所有潜艇
  • 将数据库中 sub 中的所有 % 转换为 * 以进行以下匹配
  • 尝试将 sub 与 $subscription 匹配
  • 如果它们匹配,则继续下一个 SELECT
  • 从 sub 类似于 %sub% 的数据库中选择所有“用户”
  • 然后向每个用户发送与选择匹配的消息
  • last set return 统计与 select 匹配的用户数并向频道发送消息

使用Donal建议的解决方案后。一切似乎都表现得像它应该有的一个小问题。代码的 [string match -nocase [get_subscription $SQL(conn)]* $subscription] 部分没有将每个都保存为要检查的变量。哪一行首先出现,它改为使用并停止而不是完成列表以查看是否还有更多匹配项。一些条目以不同的方式添加,但应提供相同的结果。例如,某些条目被添加为 The.TV.Show.S01 或 The%TV%Show%S01 这意味着它应该匹配两个部分并获得准确的计数和用户。

4

1 回答 1

1

这可能很困难,因为你有太多的东西。尝试将代码分解成更小的部分来完成明确定义的任务。这称为重构,它是使代码易于理解的重要部分。

以下是一些建议的重构:

proc get_subscription {conn} {
    set return [mysqlsel $conn "select sub from DB_Subs where category='TV'" -list] 
    foreach {sub} $return {
        regsub -all -- {%} $sub "*" sub1
    }
    return $sub1
}

proc notify_subscription_user {conn nick sub beta} {
    set return [mysqlsel $conn "select user from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -list]
    foreach line $return {
        lassign $line user
        if {[onchan $user $beta]} {
            putnow "PRIVMSG $nick : Subscription found matching your request."
        }
    }
}

proc send_subscription_message {conn sub beta subscription_text} {
    set return [mysqlsel $conn "select count(DISTINCT user) from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -flatlist]
    lassign $return users
    putnow "PRIVMSG $beta :SUBSCRIPTION: $users Users With Subscriptions for $subscription_text"
}

有了这些,我们就可以将其余的代码改写成这样(删除空else 子句、将表达式拆分为行、组合嵌套if测试;所有基本的东西):

bind pubm - * bind_pubm
proc bind_pubm {nick uhost handle channel text} {
    global SQL; sql:start

    if {
        [regexp -nocase -- {^!sub (.*) *$} $text -> subscription]
        && ([string match -nocase *HDTV* $subscription]
            || [string match -nocase *S??E??* $subscription])
        && [string match -nocase [get_subscription $SQL(conn)]* $subscription]
    } then {
        notify_subscription_user $SQL(conn) $nick $sub $beta
        send_subscription_message $SQL(conn) $sub $beta $subscription
    }
}

这能解决您的问题吗?我不知道,但它应该给你一个更好的基础。

于 2013-03-05T09:47:02.540 回答