I m working on a discord bot in Python that will look for specific trigger words on a blacklist and censor them. I ve got it mostly working, but words already being censored gives it issues.
My solution was to search for "||" (for those unfamiliar, putting text between sets of || puts it behind spoiler tags on discord) and then only run the function if the word is NOT between them. The issue I m running into is actually finding the position of || .
This is my first real attempt at using Python, and its mostly been via googling, so sorry if this is something obvious ^^
Edit for Clarity: an example ideal input-output would be
In: "Omg I m so dead"
Out: "Omg I m so d||ead||"
which currently functions
In: "||Omg I m so dead||"
Ideal Out: "||Omg I m so dead||
does not currently function and returns the error
My first attempt was using
censor_pos = [m.span() for m in re.finditer( || , mge, flags=0)]
where mge is the message. This returned every postion in the string (aka dead would return (0,0)(1,1)(2,2)(3,3),(4,4)) I realized this was because | was a special character.
Next I tried changing it to
censor_pos = [m.span() for m in re.finditer(re.escape( || ), mge, flags=0)]
(adding re.escape) which returned
TypeError: expected string or bytes-like object, got NoneType
Interestingly, I did try printing censor_pos to the console to see what was going on, and it does seem to be getting the correct position, the error occurs after.
Because I know it could be an issue elsewhere in the code, here s the full function I m running it in:
def Censor_Trigger(mge, word): #function to censor trigger words given a message and a trigger word
censor_pos = [m.span() for m in re.finditer(re.escape( || ), mge, flags=0)] #finds all instances of "||" to know which parts of message are censored
trigger_pos = [m.span() for m in re.finditer(word, mge, flags=re.IGNORECASE)] #finds all instances of a trigger word, giving positions as tuples in an array
censored_msg = mge
print (censor_pos)
print (trigger_pos)
y = 0 #variable to account for position change when censoring multiple instances of the same word
for x in trigger_pos:
a = 0
is_censored = False
for z in censor_pos:
if 0<=a and (a+1)<(len(censor_pos)):
if censor_pos[a][1]<=x[0]<=censor_pos[a+1][0]:
is_censored = True
return
else:
is_censored = False
a=a+1
if is_censored == False:
censored_msg = censored_msg[:x[1]+(y*4)] + "||" + censored_msg[x[1]+(y*4):]
censored_msg = censored_msg[:(x[0]+1+(y*4))] + "||" + censored_msg[(x[0]+1+(y*4)):]
y=y+1
print(censored_msg)
else:
return
return censored_msg
并且要求他们选择住所
trigger_words = ["dead", "trigger", "example", "another"] #list of triggers
for x in trigger_words: #runs the function to censor trigger words for all words in the trigger list
Mess = Censor_Trigger(Mess, x)
完全错误是
[2023-09-03 19:26:01] [ERROR ] discord.client: Ignoring exception in on_message
Traceback (most recent call last):
File "C:UsersuserAppDataLocalProgramsPythonPython311Libsite-packagesdiscordclient.py", line 441, in _run_event
await coro(*args, **kwargs)
File "C:UsersuserDesktopDiscord Bot FunTest Code.py", line 17, in on_message
Mess = Censor_Trigger(Mess, x)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersuserDesktopDiscord Bot FunTest Code.py", line 37, in Censor_Trigger
censor_pos = [m.span() for m in re.finditer(re.escape( || ), mge, flags=0)] #finds all instances of "||" to know which parts of message are censored
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersuserAppDataLocalProgramsPythonPython311Lib
e\__init__.py", line 223, in finditer
return _compile(pattern, flags).finditer(string)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got NoneType