-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·141 lines (122 loc) · 4.68 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/python3
from discord import File, Embed
from discord.ext import commands
from discord.utils import find
from io import BytesIO
from datetime import datetime
from aiohttp_requests import requests
import asyncio
import aiohttp
from config import *
from token_value import token
class CamBot():
def __init__(self):
self.prefix = prefix
self.bot = commands.Bot(command_prefix=self.prefix)
async def auto_send_occupied(self, occupied):
chann = find(lambda r: r.name == news_channel,
self.bot.get_all_channels())
if occupied:
e = Embed(title=occupied_news)
await chann.send(embed=e)
else:
e = Embed(title=empty_news)
await chann.send(embed=e)
def catch(self):
@self.bot.event
async def on_ready():
task = asyncio.create_task(check_occupied(self))
await task
@self.bot.event
async def on_command_error(ctx, exc):
command = ctx.message.content[1:].split(" ")[0]
if command not in self.bot.all_commands:
return
ctx.message.content = "{pref}help {cmd}".format(pref=self.prefix,
cmd=command)
await self.bot.process_commands(ctx.message)
@self.bot.command()
async def occupied(ctx):
""" Tells if there is someone in the room """
try:
a = await requests.get(cam_url + occupied_endpoint)
except aiohttp.client_exceptions.ClientOSError:
await ctx.send("```{}```".format(error_unreachable))
return
a = await a.json()
if a:
await ctx.send("```{}```".format(occupied_message))
else:
await ctx.send("```{}```".format(empty_message))
@self.bot.command()
async def image(ctx):
""" Send image of the room """
if find(lambda r: r.name == cam_role, ctx.author.roles) is None:
await ctx.send("```{}```".format(image_error_role))
return
if str(ctx.channel) != cam_channel:
await ctx.send("```{}```".format(image_error_channel))
return
try:
a = await requests.get(cam_url+image_endpoint)
except aiohttp.client_exceptions.ClientOSError:
await ctx.send("```{}```".format(error_unreachable))
return
a = await a.content.read()
with BytesIO(a) as image:
await ctx.send(file=File(image, "room.png"))
@self.bot.command()
async def today(ctx):
""" Sends occupation periods during the day """
try:
a = await requests.get(cam_url+stats_endpoint)
except aiohttp.client_exceptions.ClientOSError:
await ctx.send("```{}```".format(error_unreachable))
return
a = await a.text()
periods = []
for line in a.split("\n"):
if "|" in line:
debut, fin = [l.strip() for l in line.split("|")]
debut = datetime.fromisoformat(debut)
fin = datetime.fromisoformat(fin)
if fin.date() == datetime.now().date():
l = debut.strftime("%H:%M")
l += " → "
l += fin.strftime("%H:%M")
periods.append(l)
else:
debut = datetime.fromisoformat(line)
if debut.date() != datetime.now().date():
l = debut.strftime("%d/%m/%y %H:%M")
else:
l = debut.strftime("%H:%M")
l += " → "
l += now_term
periods.append(l)
msg = "\n".join(periods)
if len(msg) != 0:
e = Embed(title=occupation_title, description=msg)
else:
e = Embed(title=occupation_title,
description=no_occupation)
await ctx.send(embed=e)
def start(self):
self.catch()
self.bot.run(token)
async def check_occupied(bot):
test = 2
while 1:
try:
a = await requests.get(cam_url+occupied_endpoint)
except aiohttp.client_exceptions.ClientOSError:
await asyncio.sleep(5)
continue
a = await a.json()
if test != a:
test = a
await bot.auto_send_occupied(test)
await asyncio.sleep(1)
if __name__ == "__main__":
bot = CamBot()
bot.start()