This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mogirin.py
78 lines (58 loc) · 2.2 KB
/
mogirin.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
from __future__ import annotations
import json
import os
import re
import discord
import gspread
def find_ticket_number(content: str) -> str | None:
if m := re.search(r"\d+", content):
return m.group(0)
return None
class TicketNumberNotFound(Exception):
...
class TicketAlreadyCollected(Exception):
...
class TicketCollector:
def __init__(self, spreadsheet_id: str):
self.searcher = TicketSheetSearcher.from_id(spreadsheet_id)
async def collect(
self, ticket_number: str, member: discord.Member, role: discord.Role
):
if not (ticket_cell := self.searcher.find_cell(ticket_number)):
raise TicketNumberNotFound
if self.searcher.query_already_collected(ticket_cell):
raise TicketAlreadyCollected
else:
await RoleAttacher.attach(member, role)
self.searcher.register_as_collected(ticket_cell)
class TicketSheetSearcher:
def __init__(self, worksheet: gspread.Worksheet):
self.worksheet = worksheet
@classmethod
def from_id(cls, spreadsheet_id: str) -> TicketSheetSearcher:
info = json.loads(os.getenv("SERVICE_ACCOUNT_INFO_AS_STR"))
client = gspread.service_account_from_dict(info)
spreadsheet = client.open_by_key(spreadsheet_id)
return cls(spreadsheet.sheet1)
def find_cell(self, ticket_number: str) -> gspread.Cell | None:
return self.worksheet.find(ticket_number, in_column=1)
def query_already_collected(
self, ticket_number_cell: gspread.Cell
) -> bool:
collection_status_cell = self.worksheet.cell(
*self._status_cell_indice(ticket_number_cell)
)
return bool(collection_status_cell.value)
def register_as_collected(self, ticket_number_cell: gspread.Cell):
self.worksheet.update_cell(
*self._status_cell_indice(ticket_number_cell), "✅"
)
@staticmethod
def _status_cell_indice(
ticket_number_cell: gspread.Cell,
) -> tuple[int, int]:
return ticket_number_cell.row, ticket_number_cell.col + 2
class RoleAttacher:
@staticmethod
async def attach(member: discord.Member, role: discord.Role):
await member.add_roles(role)