33
44import discord
55import tenacity
6- from cogs .bomberman import Bomberman
7- from cogs .gear import Gear
8- from cogs .potions import Potions
96from discord import Colour , Embed , Message , app_commands
107from discord .ext import commands
118from slaobot import SlaoBot
129from utils .constants import ZONE_IMAGES , Role
1310from utils .format import bold , make_execution
11+ from utils .raidview import RaidView
1412from utils .report import Report
1513from utils .wcl_client import WCLClient
1614
1715
18- class RaidView (discord .ui .View ):
19- def __init__ (self , bot : SlaoBot ):
20- super ().__init__ (timeout = None )
21- self .bot : SlaoBot = bot
22-
23- # noinspection PyUnusedLocal
24- @discord .ui .button (label = 'Refresh' , style = discord .ButtonStyle .gray , custom_id = 'raid_view:refresh' , emoji = '🔄' )
25- async def refresh (self , interaction : discord .Interaction , button : discord .ui .Button ):
26- report_id = self ._validate_interaction (interaction )
27- if report_id is None :
28- await self ._reply_with_error (interaction )
29- return
30-
31- author_icon = interaction .message .embeds [0 ].thumbnail .url
32-
33- cog : Optional [RaidReport ] = self .bot .get_cog ('RaidReport' )
34- if not cog :
35- return
36-
37- # noinspection PyUnresolvedReferences
38- await interaction .response .defer ()
39-
40- embed = await cog .process_report (report_id , author_icon )
41- await interaction .edit_original_response (embed = embed )
42-
43- # noinspection PyUnusedLocal
44- @discord .ui .button (label = 'Potions' , style = discord .ButtonStyle .gray , custom_id = 'raid_view:potions' , emoji = '🧪' )
45- async def potions (self , interaction : discord .Interaction , button : discord .ui .Button ):
46- report_id = self ._validate_interaction (interaction )
47- if report_id is None :
48- await self ._reply_with_error (interaction )
49- return
50-
51- cog : Optional [Potions ] = self .bot .get_cog ('Potions' )
52- if not cog :
53- return
54-
55- # noinspection PyUnresolvedReferences
56- await interaction .response .defer ()
57-
58- # Remove potions embed if exists
59- embeds = interaction .message .embeds
60- for item in embeds :
61- if item .title == 'Потная катка' :
62- embeds .remove (item )
63- await interaction .message .edit (embeds = embeds )
64- return
65-
66- # Add potion embeds if not exists
67- embed = await cog .process_pots (report_id )
68- embeds .append (embed )
69- await interaction .edit_original_response (embeds = embeds )
70-
71- # noinspection PyUnusedLocal
72- @discord .ui .button (label = 'Gear' , style = discord .ButtonStyle .gray , custom_id = 'raid_view:gear' , emoji = '🛂' )
73- async def gear (self , interaction : discord .Interaction , button : discord .ui .Button ):
74- report_id = self ._validate_interaction (interaction )
75- if report_id is None :
76- await self ._reply_with_error (interaction )
77- return
78-
79- cog : Optional [Gear ] = self .bot .get_cog ('Gear' )
80- if not cog :
81- return
82-
83- # noinspection PyUnresolvedReferences
84- await interaction .response .defer ()
85-
86- # Remove gear embed if exists
87- embeds = interaction .message .embeds
88- for item in embeds :
89- if item .title == 'Камни и зачаровывание' :
90- embeds .remove (item )
91- await interaction .message .edit (embeds = embeds )
92- return
93-
94- # Add gear embeds if not exists
95- embed = await cog .process_gear (report_id )
96- embeds .append (embed )
97- await interaction .edit_original_response (embeds = embeds )
98-
99- # noinspection PyUnusedLocal
100- @discord .ui .button (label = 'Bombs' , style = discord .ButtonStyle .gray , custom_id = 'raid_view:bombs' , emoji = '💣' )
101- async def bombs (self , interaction : discord .Interaction , button : discord .ui .Button ):
102- report_id = self ._validate_interaction (interaction )
103- if report_id is None :
104- await self ._reply_with_error (interaction )
105- return
106-
107- cog : Optional [Bomberman ] = self .bot .get_cog ('Bomberman' )
108- if not cog :
109- return
110-
111- # noinspection PyUnresolvedReferences
112- await interaction .response .defer ()
113-
114- # Remove gear embed if exists
115- embeds = interaction .message .embeds
116- for item in embeds :
117- if item .title == 'Бомбим!' :
118- embeds .remove (item )
119- await interaction .message .edit (embeds = embeds )
120- return
121-
122- # Add gear embed if not exists
123- embed = await cog .process_bombs (report_id )
124- embeds .append (embed )
125- await interaction .edit_original_response (embeds = embeds )
126-
127- @staticmethod
128- def _validate_interaction (interaction : discord .Interaction ) -> Optional [str ]:
129- if interaction .message is None :
130- return None
131-
132- # There should be an embed. Either waiting or report
133- embed : Embed = interaction .message .embeds [0 ]
134- if not embed :
135- return None
136-
137- # Embed should have URL. I hope it is a WCL URL with report ID
138- report_id = embed .url .split ('/' )[- 1 ]
139- if not report_id :
140- return None
141-
142- return report_id
143-
144- @staticmethod
145- async def _reply_with_error (interaction : discord .Interaction ) -> None :
146- # noinspection PyUnresolvedReferences
147- await interaction .response .send_message (
148- 'Embed or URL is missing. Something went wrong. Better call Doc!' ,
149- ephemeral = True ,
150- )
151-
152-
15316class RaidReport (commands .Cog ):
15417 def __init__ (self , bot : SlaoBot ):
15518 """Cog to provide basic statistics about raid.
15619
15720 :param bot: Bot instance
15821 """
15922 self .bot : SlaoBot = bot
160- self .bot .add_view (RaidView (self . bot ))
23+ self .bot .add_view (RaidView ())
16124
16225 @app_commands .command (description = 'Отчёт по логу на WCL' )
16326 @app_commands .describe (report_id = 'WCL report ID' )
@@ -166,10 +29,8 @@ async def report(self, interaction: discord.Interaction, report_id: str) -> None
16629 # noinspection PyUnresolvedReferences
16730 await interaction .response .defer ()
16831
169- author_icon = 'https://cdn.discordapp.com/icons/620682853709250560/6c53810d8a4e2b75069208a472465694.png'
170-
171- embed = await self .process_report (report_id , author_icon )
172- await interaction .edit_original_response (embed = embed , view = RaidView (self .bot ))
32+ embed = await self .process_interaction (report_id )
33+ await interaction .edit_original_response (embed = embed , view = RaidView ())
17334
17435 @commands .Cog .listener ()
17536 async def on_message (self , message : Message ) -> None :
@@ -191,22 +52,19 @@ async def on_message(self, message: Message) -> None:
19152
19253 # Send interstitial embed to store report_id
19354 embed = self ._make_waiting_embed (report_id , author_icon )
194- waiting_embed = await ctx .send (embed = embed )
55+ waiting_message = await ctx .send (embed = embed )
19556
196- embed = self .process_report (report_id , author_icon )
57+ embed = await self .process_interaction (report_id , author_icon = author_icon )
19758 if embed is None :
198- await waiting_embed .edit (view = RaidView (self . bot ))
59+ await waiting_message .edit (view = RaidView ())
19960
200- await waiting_embed .edit (embed = embed , view = RaidView (self . bot ))
61+ await waiting_message .edit (embed = embed , view = RaidView ())
20162
202- async def process_report (self , report_id : str , author_icon : str ) -> Optional [discord .Embed ]:
63+ async def process_interaction (self , report_id : str , ** kwargs : Any ) -> Optional [discord .Embed ]:
20364 """Process a single report
20465
20566 :param report_id: WarcraftLogs report ID
206- :param author_icon:
20767 """
208- report_url = f'https://classic.warcraftlogs.com/reports/{ report_id } '
209-
21068 async with WCLClient () as client :
21169 try :
21270 rs = await client .get_rankings (report_id )
@@ -221,7 +79,10 @@ async def process_report(self, report_id: str, author_icon: str) -> Optional[dis
22179 else :
22280 report_tag = 'No tag'
22381
82+ report_url = f'https://classic.warcraftlogs.com/reports/{ report_id } '
22483 report_description = Report .make_report_description (rs )
84+ author_icon = kwargs .get ('author_icon' , self .bot .user .avatar .url )
85+
22586 embed = Embed (title = f'{ report_title } - { report_tag } ' ,
22687 url = report_url ,
22788 description = report_description ,
0 commit comments