-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
58 lines (47 loc) · 1.8 KB
/
bot.py
File metadata and controls
58 lines (47 loc) · 1.8 KB
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
"""Entry points for Discord bot commands."""
import os
import discord
from discord.ext import commands
from api import download_image
from otr import read_image_text
from schedule import Schedule
from ui import ScheduleView
token = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
intents.guild_scheduled_events = True
bot = commands.Bot(command_prefix='!', intents=intents)
bot.last_texts = []
@bot.command()
async def echo(context: commands.Context, *, message:str):
"""Echoes back argument to Discord message"""
await context.send(message)
@bot.command()
async def debug(context: commands.Context):
"""Prints all the last text read from the last images."""
await context.send('Last images processed:')
for text in bot.last_texts:
await context.send(text)
@bot.command()
async def read_schedule(context: commands.Context):
"""Reads image attachments into Schedules."""
bot.last_texts = []
image_attachments = context.message.attachments
count = len(image_attachments)
if count == 0:
await context.send('No images found 😔')
return
await context.send(f'Processing {count} image{"" if count == 1 else "s"}...')
for image_attachment in image_attachments:
image = await download_image(image_attachment.url)
text = read_image_text(image)
bot.last_texts.append(text)
schedule = Schedule(text)
if schedule.error is not None:
await context.send(schedule.error)
return # Nothing to do if the schedule didn't parse correctly.
if context.guild is None:
await context.send(str(schedule))
return # Don't offer to create events outside of a guild.
await context.send(str(schedule), view=ScheduleView(schedule))
bot.run(token)