LinkBlock - URL Protection - notification messages from a bot

Please explain how you can make the notification messages come from a bot

You need to go to the LinkBlock action and double click on the Execute Code (LinkBlock) Sub-Action.

Scroll down to lines 58 and lines 63 in that code, replace the ‘False’ with ‘True’ as is shown in the picture below. Then press Save and Compile and lastly, ensure you press Save at the top of StreamerBot

1 Like

Hey, I probably discovered a bug, when I provide permit to a user, his message will be deleted after a minute, but I want it to stay in Chat as he received permit. Can this be changed?

i reworked the script, so any URL is blocked, regardless of formating, at least any url twitch actually recognize as url

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class CPHInline
{
    private bool IsTwitchEmote(string input)
    {
        // Regular expression to match Twitch emote URLs
        string emotePattern = @"https:\/\/(www\.)?twitch\.tv\/emote\/\w+";
        return Regex.IsMatch(input, emotePattern);
    }

    private bool IsWhitelistedUrl(string url, List<string> whitelist)
    {
        // Convert wildcard entries in the whitelist to regex patterns
        foreach (string entry in whitelist)
        {
            // Escape special regex characters and replace '*' with '.*' to match any character
            string pattern = "^" + Regex.Escape(entry).Replace("\\*", ".*") + "$";

            // Check if the URL matches any of the whitelist patterns
            if (Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase))
            {
                return true;
            }
        }

        return false;
    }

    public bool Execute()
    {
        string permitUser = CPH.GetGlobalVar<string>("permitUser", false);

        // Whitelist containing allowed URL patterns (supports wildcards)
        List<string> whitelist = new List<string>()
        {
            "https://www.twitch.tv/YourTwitchName/clip/*",  // Allow any clip URL from this user
            "www.twitch.tv/YourTwitchName/clip/*",
            "https://discord.gg/YourDiscordLink",         	// Specific Discord invite URL
            "discord.gg/YourDiscordLink"                  	// Allow without HTTPS for flexibility
        };

        // Check if the current user is permitted to post URLs
        if (string.IsNullOrEmpty(permitUser) || args["user"].ToString() != permitUser)
        {
            // Pattern to detect URLs in the chat message
            string pattern = @"((https?:\/\/)?([\w\-]+\.)+[a-zA-Z]{2,})(\/[\w\-.~:\/?#[\]@!$&'()*+,;=%]*)?";
            string input = args["message"].ToString();
            RegexOptions options = RegexOptions.Multiline;

            // Search for all URLs in the message
            foreach (Match m in Regex.Matches(input, pattern, options))
            {
                // Check if the URL is whitelisted
                bool allowed = IsWhitelistedUrl(m.Value, whitelist);

                // If not allowed and not a Twitch emote, delete the message
                if (!allowed && !IsTwitchEmote(m.Value))
                {
                    string messageId = args["msgId"].ToString();
                    CPH.TwitchDeleteChatMessage(messageId, false);
                    CPH.SendAction("Please refrain from posting links without permission, " + args["user"].ToString() + ". Thank you.");
                    break;  // Stop processing after the first non-whitelisted URL
                }
            }
        }

        return true;
    }
}