我正在尝试创建一个基于 pytesseract 的discord机器人,该机器人基本上可以扫描以图片形式发布的任何邮件,然后将其读取,与禁令匹配并将其删除。不知道我在做什么,但无法正常工作。希望对下面的代码发表评论。
import discord
from PIL import Image
from pytesseract import image_to_string
import requests
from io import BytesIO
import re
# Variables
bot = discord.Client()
# Discord
@bot.event
async def on_ready():
print("Filter Bot Started: " + bot.user.name)
@bot.event
async def on_message(message):
txt_urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', message.content)
for url in txt_urls:
if check_url(url) == False:
await bot.delete_message(message)
print("Link deleted!")
break
# Extracting Text
def check_url(url):
try:
response = requests.get(url)
while True:
r = requests.head(url)
if 300 < r.status_code < 400:
url = r.headers['location']
text = url.lower()
if "discord" in text:
return False
break
else:
break
print("Url not an redirect")
img = Image.open(BytesIO(response.content))
img_text = image_to_string(img)
img_text = img_text.lower()
if "Badword" in img_text or "FWORD" in img_text:
print("Prohibited word found!")
return False
else:
print("Link passed the test")
return True
except:
return True
# Run Bot
bot.run('BOT TOKEN XXXXX')```