Repository navigation
Expand file tree
/
Copy pathview.py
More file actions
170 lines (137 loc) · 5.89 KB
/
Copy pathview.py
File metadata and controls
170 lines (137 loc) · 5.89 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Viewing of games should happen only in dms.
import math
import os
import cairosvg
import chess
import chess.engine
import chess.pgn
import chess.svg
import discord
from discord import SelectOption
from discord import app_commands as slash
from discord.ext import commands
from discord.ui import Button, View, Select
from dotenv import load_dotenv
from sqlalchemy import or_
from dbs import Game, ReviewSession
from utility import session_scope
load_dotenv()
path = os.environ.get("path")
"""It is not possible to fetch a message using just the message ID without specifying the
channel ID. In Discord's API, the combination of the message ID and the channel ID uniquely
identifies a message within a server."""
class Viewfn(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
async def button_i(self, i, game_ids):
embed = discord.Embed(title="Game")
b1 = Button(emoji="⏪", custom_id="b1")
b2 = Button(emoji="◀", custom_id="b2")
b3 = Button(emoji="▶", custom_id="b3")
b4 = Button(emoji="⏩", custom_id="b4")
view = View().add_item(b1).add_item(b2).add_item(b3).add_item(b4)
svg_obj = chess.svg.board(board=chess.Board())
png_obj = cairosvg.svg2png(bytestring=svg_obj)
with open(f"{path}/views/initial.png", "wb") as file:
file.write(png_obj) # type:ignore
file = discord.File(f"{path}/views/initial.png")
message = await i.user.send(embed=embed, view=view, file=file)
# Till this line we just send a board to the user in the starting position
b1.callback = b2.callback = b3.callback = b4.callback = lambda i: self.game_i(
i, message.id, message.channel.id
)
# The review row stores which game is open and how far into it the user
# is -- not a second copy of the board and move list.
selected = int(i.data["values"][0][1:])
with session_scope() as session:
session.add(
ReviewSession(
user_id=i.user.id,
channel_id=message.channel.id,
message_id=message.id,
game_id=game_ids[selected],
ply=0,
)
)
async def game_i(self, i, message_id, channel_id):
await i.response.send_message(
content="okey", delete_after=0.0001, ephemeral=True
)
action = i.data["custom_id"]
with session_scope() as session:
review = (
session.query(ReviewSession)
.filter(
ReviewSession.message_id == message_id,
ReviewSession.channel_id == channel_id,
)
.one_or_none()
)
if review is None:
return
last = review.game.length
# Navigation is just moving a cursor; the board for any position is
# replayed from the game's move list.
review.ply = {
"b1": 0,
"b2": max(review.ply - 1, 0),
"b3": min(review.ply + 1, last),
"b4": last,
}[action]
board = review.game.board_at(review.ply)
ply = review.ply
await self.render(message_id, channel_id, board, ply)
async def render(self, message_id, channel_id, board, ply):
channel = self.bot.get_channel(channel_id)
message = await channel.fetch_message(message_id)
svg_obj = chess.svg.board(board=board, orientation=board.turn)
png_obj = cairosvg.svg2png(bytestring=svg_obj)
with open(f"{path}/views/{message_id}{channel_id}.png", "wb") as file:
file.write(png_obj) # type:ignore
file = discord.File(f"{path}/views/{message_id}{channel_id}.png")
embed = message.embeds[0]
embed.title = "Move " + str(math.ceil(ply / 2))
embed.set_image(url=f"attachment:/{path}/views/{message_id}{channel_id}.png")
await message.edit(embed=embed, attachments=[file])
@slash.command(name="view", description="View past games")
async def view(self, ctx, member: discord.Member):
await ctx.response.send_message(
content="okey", delete_after=0.0001, ephemeral=True
)
#User should input a member, in DMs it should show them an embed with 5 columns: white, black,
#result, start date & time, green arrow buttons. Only after they click a button,
#a board of that game will load in their DMs, and they can view it
#Retrieve past games for the specified member from the database
with session_scope() as session:
games = (
session.query(Game)
.filter(
or_(Game.white_id == member.id, Game.black_id == member.id),
Game.server_id == ctx.guild.id,
)
.order_by(Game.end_time.desc())
.limit(25) # Discord allows at most 25 select options
.all()
)
if not games:
await ctx.user.send(f"{member.name} has no finished games in this server")
return
options = []
game_ids = []
for index, game in enumerate(games):
w = await self.bot.fetch_user(game.white_id)
b = await self.bot.fetch_user(game.black_id)
options.append(
SelectOption(
label=f"{w.name} | {b.name} | {game.result.value} | {game.start_time.strftime('%d/%m/%y %H:%M')}",
value=f"B{index}",
)
)
game_ids.append(game.id)
select = Select(placeholder="Select a game", options=options)
select.callback = lambda i: self.button_i(i, game_ids)
view = View()
view.add_item(select)
await ctx.user.send(view=view)
async def setup(bot: commands.Bot):
await bot.add_cog(Viewfn(bot))