Need help for censoring a mention in a string

Hello, I just got promoted to member a few days ago, ty god

I’m trying to modify a script that censors a mention for discord (yes I’m modifying a feedback script.)

I tried using string.gsub

this is my script I have tried so far…

Server:

local webhookURL = "https://discord.com/api/webhooks/(no u cant use my webhook)"
local filteringFunction = game.ReplicatedStorage.FilteringFunction

local ChatService = game:GetService("Chat")
local HTTP = game:GetService("HttpService")

function filteringFunction.OnServerInvoke(player, msg)
   local Prevention = string.gsub(msg,"@everyone","[PREVENTION]") and string.gsub(msg,"@here","[PREVENTION]")
   local FilteredMessage = ChatService:FilterStringForBroadcast(Prevention, player)	
   --Filter the message, before sending it to the webhook.

   local payload = HTTP:JSONEncode({
   	content = FilteredMessage,
   	username = "From "..player.Name.." (UserId: "..player.UserId..")"
   })

   HTTP:PostAsync(webhookURL, payload)
   return "Feedback recieved!"
end

no changes made for another script, but here
Client:

local maxCharacters = 500 -- Maximum Characters

local player = game.Players.LocalPlayer
local open = false

local feedbackMain = script.Parent.Frame

feedbackMain.MaxChar.Text = maxCharacters - #feedbackMain.Input.Text
feedbackMain.Input.Changed:Connect(function()
	feedbackMain.MaxChar.Text = maxCharacters - #feedbackMain.Input.Text
	
end)

local db = false
feedbackMain.Submit.MouseButton1Click:Connect(function()
	if not db and maxCharacters - #feedbackMain.Input.Text >= 0 then
		db = true
		local msg = feedbackMain.Input.Text
		feedbackMain.Input.Text = "Sending Message..."
		local response = game.ReplicatedStorage.FilteringFunction:InvokeServer(msg)
		feedbackMain.Input.Text = response
		wait(5)
		if feedbackMain.Input.Text == response then
			feedbackMain.Input.Text = ""
		end
		db = false
	end
end)

all helps are appreciated.

1 Like

Welcome to the Dev Forum! I hope you spend some quality time here! :slight_smile:

The solution is rather simple. Take a look at the following.

local MENTIONS = {"@everyone", "@here"}
local message = "Hi @everyone!"

for i, phrase in pairs(MENTIONS) do
	if (string.match(message, phrase)) then
		print("REJECTED")
	end
end
-- OUTPUT:
-->> REJECTED

However, you could also prevent any mentions this way:

local UNALLOWED = " @" -- white space before @
local message = "Hi @everyone!"

if (string.match(message, UNALLOWED)) then print("REJECTED") end

-- OUTPUT:
-->> REJECTED

Alternatively, you could replace @ character with your custom choice:

local UNALLOWED = " @" -- white space before @
local REPLACEMENT = " /"
local message = "Hi @everyone!"

message = message:gsub(UNALLOWED, REPLACEMENT)
print(message)

-- OUTPUT:
-->> Hi /everyone!

More about string library here: string | Roblox Creator Documentation.

EDIT You should perhaps also notify the player when their feedback was rejected.

1 Like

This example:

doesn’t make much sense. Why putting a white space before @ character when Discord will still interpret Hello@everyone as ping of everyone?

You can do this instead:

local UNALLOWED = "@"
local REPLACEMENT = "!"
local message = "Hello@everyone!"

message = message:gsub(UNALLOWED, REPLACEMENT)
print(message)

It will work on ‘Hello @everyone’ as well as on ‘Hello@everyone!’.

1 Like

well, there can be space on it,
yeah, there’s no need to put a space at the front but it’s okay.

We are talking about help with censoring mention in a string that would be published to Discord. The third example (I have only looked on that one) provided by @EssenceExplorer won’t work in all cases (so it won’t always censor it).

You say that his and my remixed example would work the same.

And the answer is no because my example would replace all mentions and @EssenceExplorer’s - only when there is space in front of them. But the thing is that Discord interprets ‘Hello@everyone’ (without space) as mention too. That means that the 3rd example provided by Essence won’t work in all cases.

1 Like

Also, you don’t need to filter text sent to non - Roblox websites. We all know this filter is a pain sometimes, but luckily Discord does not have this policy. (Since it’s 13+)

1 Like

it supposes to censor mentions (like @everyone)

Yeah ik, but your first script had a FilterStringAsync.

1 Like

Thanks for the feedback. I didn’t know mention can be done without space. Anyways, @RocketB0ii can always set replacements and spaces customly. My intention was to provide universal pseudo code to give OP the idea on how it can be achieved.

2 Likes

also ty for your script, it’s working tho
at least i learned something new.

1 Like