Skip to content

Commit 2a51f02

Browse files
authored
Slashcommands (#22)
* More slash commands * Fix flake * Finish with slashcommands * Fix isort, I hope
1 parent 862cb8d commit 2a51f02

7 files changed

Lines changed: 253 additions & 232 deletions

File tree

slao_bot/cogs/bomberman.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections import defaultdict
2-
from typing import Dict
2+
from typing import Dict, Optional
33

4+
import discord
45
import tenacity
5-
from discord import Colour, Embed
6+
from discord import Colour, Embed, app_commands
67
from discord.ext import commands
7-
from discord.ext.commands import Context
88
from utils.wcl_client import WCLClient
99

1010

@@ -14,27 +14,28 @@ class Bomberman(commands.Cog):
1414
_bomb_damage: Dict[str, int]
1515
_other_damage: Dict[str, int]
1616

17-
def __init__(self, bot):
18-
"""Cog to check potions used during a raid.
19-
20-
:param bot:
21-
"""
22-
self.bot = bot
17+
def __init__(self):
18+
"""Cog to check potions used during a raid."""
2319
self._engineers = defaultdict()
2420
self._bomb_damage = defaultdict()
2521
self._other_damage = defaultdict()
2622

27-
@commands.command(name='bomb')
28-
async def bomb_command(self, ctx: Context, report_id: str) -> None:
29-
"""Get data about bombs and bombs-like items used. Format: <prefix>bomb SOME_REPORT_ID."""
30-
await self.process_bombs(ctx, report_id)
23+
@app_commands.command(description='Использование гранат и схожих расходников')
24+
@app_commands.describe(report_id='WCL report ID')
25+
async def engineers(self, interaction: discord.Interaction, report_id: str) -> None:
26+
"""Get data about bombs and bombs-like items used."""
27+
# noinspection PyUnresolvedReferences
28+
await interaction.response.defer()
29+
30+
embed = await self.process_bombs(report_id)
31+
await interaction.edit_original_response(embed=embed)
3132

32-
async def process_bombs(self, ctx: Context, report_id: str) -> None:
33+
async def process_bombs(self, report_id: str) -> Optional[discord.Embed]:
3334
async with WCLClient() as client:
3435
try:
3536
rs = await client.get_bombs(report_id)
3637
except tenacity.RetryError:
37-
return
38+
return None
3839

3940
self.make_engineers(rs['reportData']['report']['engineers']['data']['entries'])
4041
self._bomb_damage = self.calculate_damage(rs['reportData']['report']['bombs']['data']['entries'])
@@ -60,12 +61,7 @@ async def process_bombs(self, ctx: Context, report_id: str) -> None:
6061
self._bomb_damage.clear()
6162
self._other_damage.clear()
6263

63-
if ctx.message.author != self.bot.user:
64-
await ctx.send(embed=embed)
65-
else:
66-
embeds = ctx.message.embeds
67-
embeds.append(embed)
68-
await ctx.message.edit(embeds=embeds)
64+
return embed
6965

7066
def calculate_damage(self, entries: Dict) -> Dict:
7167
damage = {}
@@ -141,4 +137,4 @@ def _add_damage(raider: Dict, result: Dict) -> Dict:
141137

142138

143139
async def setup(bot):
144-
await bot.add_cog(Bomberman(bot))
140+
await bot.add_cog(Bomberman())

slao_bot/cogs/gear.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from itertools import chain
44
from typing import Any, Dict, Iterable, List, Optional, Set
55

6+
import discord
67
import tenacity
7-
from discord import Colour, Embed
8+
from discord import Colour, Embed, app_commands
89
from discord.ext import commands
9-
from discord.ext.commands import Context
10+
from slaobot import SlaoBot
1011
from utils import enchants
1112
from utils.constants import SLOT_NAMES, Role
1213
from utils.models import Raider
@@ -74,19 +75,18 @@ def _check_enchants(self, raider_name: str, item: Dict) -> None:
7475

7576

7677
class Gear(commands.Cog):
77-
def __init__(self, bot: commands.Bot):
78-
"""Cog to check gems and enchants.
7978

80-
:param bot: Bot instance
81-
"""
82-
self.bot = bot
79+
@app_commands.command(description='Камни и энчанты')
80+
@app_commands.describe(report_id='WCL report ID')
81+
async def gear(self, interaction: discord.Interaction, report_id: str) -> None:
82+
"""Get data about missing or low-level gems and enchants."""
83+
# noinspection PyUnresolvedReferences
84+
await interaction.response.defer()
8385

84-
@commands.command(name='gear')
85-
async def gear_command(self, ctx: Context, report_id: str) -> None:
86-
"""Get data about potions used. Format: <prefix>gear SOME_REPORT_ID."""
87-
await self.process_gear(ctx, report_id)
86+
embed = await self.process_gear(report_id)
87+
await interaction.edit_original_response(embed=embed)
8888

89-
async def process_gear(self, ctx: Context, report_id: str) -> None:
89+
async def process_gear(self, report_id: str) -> Optional[discord.Embed]:
9090
async with WCLClient() as client:
9191
try:
9292
rs = await client.get_table_summary(report_id)
@@ -128,12 +128,7 @@ async def process_gear(self, ctx: Context, report_id: str) -> None:
128128
inline=False,
129129
)
130130

131-
if ctx.message.author != self.bot.user:
132-
await ctx.send(embed=embed)
133-
else:
134-
embeds = ctx.message.embeds
135-
embeds.append(embed)
136-
await ctx.message.edit(embeds=embeds)
131+
return embed
137132

138133
@staticmethod
139134
def _print_raiders(gear_issues: Dict[str, Set]) -> Optional[str]:
@@ -171,5 +166,5 @@ def _make_raiders(rs: Dict[str, Any]) -> Dict[Role, List[Raider]]:
171166
return raiders_by_role
172167

173168

174-
async def setup(bot: commands.Bot) -> None:
175-
await bot.add_cog(Gear(bot))
169+
async def setup(bot: SlaoBot) -> None:
170+
await bot.add_cog(Gear())

slao_bot/cogs/potions.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from collections import defaultdict
22
from dataclasses import dataclass, field
3-
from typing import Any, Dict, List
3+
from typing import Any, Dict, List, Optional
44

5+
import discord
56
import tenacity
6-
from discord import Colour, Embed
7+
from discord import Colour, Embed, app_commands
78
from discord.ext import commands
8-
from discord.ext.commands import Context
9+
from slaobot import SlaoBot
910
from utils.constants import POT_IMAGES
1011
from utils.wcl_client import WCLClient
1112

@@ -73,29 +74,18 @@ def calculate_total(self) -> None:
7374

7475

7576
class Potions(commands.Cog):
76-
def __init__(self, bot):
77-
"""Cog to check potions used during a raid.
7877

79-
:param bot:
80-
"""
81-
self.bot = bot
78+
@app_commands.command(description='Использование зелий и иже с ними')
79+
@app_commands.describe(report_id='WCL report ID')
80+
async def potions(self, interaction: discord.Interaction, report_id: str) -> None:
81+
"""Get data about potions used. """
82+
# noinspection PyUnresolvedReferences
83+
await interaction.response.defer()
8284

83-
@staticmethod
84-
def raiders_by_id(rs: Dict[str, Any]) -> Dict[int, str]:
85-
raiders = rs['reportData']['report']['table']['data']['composition']
86-
result = defaultdict()
87-
88-
for raider in raiders:
89-
result[raider['id']] = raider['name']
90-
91-
return result
92-
93-
@commands.command(name='pot')
94-
async def pot_command(self, ctx: Context, report_id: str) -> None:
95-
"""Get data about potions used. Format: <prefix>pot SOME_REPORT_ID."""
96-
await self.process_pots(ctx, report_id)
85+
embed = await self.process_pots(report_id)
86+
await interaction.edit_original_response(embed=embed)
9787

98-
async def process_pots(self, ctx: Context, report_id: str) -> None:
88+
async def process_pots(self, report_id: str) -> Optional[discord.Embed]:
9989
async with WCLClient() as client:
10090
try:
10191
rs = await client.get_pots(report_id)
@@ -104,7 +94,7 @@ async def process_pots(self, ctx: Context, report_id: str) -> None:
10494
except tenacity.RetryError:
10595
return
10696

107-
raiders_by_id = self.raiders_by_id(table_summary)
97+
raiders_by_id = self._raiders_by_id(table_summary)
10898

10999
consumables = RaidConsumables()
110100
consumables.clear_data()
@@ -132,12 +122,17 @@ async def process_pots(self, ctx: Context, report_id: str) -> None:
132122
value=self._print_pot_total(consumables.combat_total),
133123
inline=False)
134124

135-
if ctx.message.author != self.bot.user:
136-
await ctx.send(embed=embed)
137-
else:
138-
embeds = ctx.message.embeds
139-
embeds.append(embed)
140-
await ctx.message.edit(embeds=embeds)
125+
return embed
126+
127+
@staticmethod
128+
def _raiders_by_id(rs: Dict[str, Any]) -> Dict[int, str]:
129+
raiders = rs['reportData']['report']['table']['data']['composition']
130+
result = defaultdict()
131+
132+
for raider in raiders:
133+
result[raider['id']] = raider['name']
134+
135+
return result
141136

142137
@staticmethod
143138
def _print_pot_usage(entries: Dict[str, int]) -> str:
@@ -164,5 +159,5 @@ def _print_pot_total(entries: Dict[str, List]) -> str:
164159
return result if len(result) > 0 else 'Вагонимся'
165160

166161

167-
async def setup(bot):
168-
await bot.add_cog(Potions(bot))
162+
async def setup(bot: SlaoBot):
163+
await bot.add_cog(Potions())

0 commit comments

Comments
 (0)