-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (76 loc) · 2.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
Discord OCR BOT
Created By: RKuangDev
Made with PyTesseract and DiscordPy
'''
#libraries
import io
import os
from keep_alive import keep_alive
import discord
import datetime
import pytesseract
import requests
from PIL import Image
from PIL import ImageFilter
from discord.ext import commands
#Initiation
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
# client = discord.Client(intents=discord.Intents.all()) #
imageFileTypes = ['png', 'jpg', 'jpeg']
#Grabbing List of Channel IDs
# def storedChannelIDs():
# idList = []
# with open ("channel ID.txt", "r") as file:
# for id in file:
# idList.append(int(id.strip()))
# return idList
#OCR Function Proccess Image and Print Text
def OCRImage(imageLink):
response = requests.get(imageLink)
img = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(img, lang='eng+kor+jpn+tha')
return text
# def getToken():
# with open("token.txt","r") as tokenFile:
# token = tokenFile.readline()
# return token
token = os.getenv("DISCORD_TOKEN") # renderで入力かな
#Calling Channel Id function + Prompt
# よくわからないので全部消す
# token = getToken()
# channelIDList = storedChannelIDs()
# print("\nLoaded Channel IDs:")
# for x in channelIDList:
# print(x)
@client.event
async def on_message(message):
# #Check if message's channel id in channel id filter
# if message.channel.id in channelIDList:
#checks for attachments
if message.attachments:
# print(message.content)
# print(message.attachments[0])
#stores url to image
link = message.attachments[0].url
linkTemp = message.attachments[0].url
#checks if attachment is image type
fileType = linkTemp.split(".")
#if it is an image type
if fileType[-1].lower().split("?")[0] in imageFileTypes:
#Log Url to console
print(f"[{datetime.datetime.now()}] {link}")
#calls OCR function
content = OCRImage(link)
#if content is not empty send embed
if content:
#Embed formatting
embed=discord.Embed()
embed.colour = 0x725E7A
embed.set_author(name="Discord OCR")
embed.add_field(name="Content:", value=content, inline=True)
# embed.set_footer(text="Made By: RKuangDev - GitHub")
await message.channel.send(embed=embed)
# Web サーバの立ち上げ
keep_alive()
client.run(token,bot=True)