-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
39 lines (26 loc) · 1019 Bytes
/
bot.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
from discord.ext import commands
from google.cloud import vision
import os
import requests
class Ocr(commands.Bot):
def __init__(self, **kwargs):
super().__init__(command_prefix="!", help_command=None, **kwargs)
self.vision_client = (
vision.ImageAnnotatorClient()
) # Google Vision API authentication needed
ocr_bot = Ocr()
@ocr_bot.event
async def on_ready():
print(f"Activated as {ocr_bot.user.name}, {ocr_bot.user.id}")
@ocr_bot.command()
async def ocr(ctx: commands.Context):
if not ctx.message.attachments[0]:
return await ctx.send("Pls attach your image")
image = vision.Image(content=requests.get(ctx.message.attachments[0].url).content)
response = ocr_bot.vision_client.text_detection(image=image)
texts = response.text_annotations
if not texts:
return await ctx.send("I couldn't find any text in your image!")
text = texts[0].description
await ctx.send(f"```{text}```")
ocr_bot.run(os.environ["BOT_TOKEN"])