|
| 1 | +from typing import Union |
| 2 | +from discord_slash.context import ComponentContext |
| 3 | +from discord_slash.model import ButtonStyle |
| 4 | +from cogs.character.embeds import Embeds |
| 5 | +from main import AIKyaru |
| 6 | +from utils import Unit |
| 7 | +from utils.custom_id import pref_custom_id, un_pref_custom_id |
| 8 | +from discord.ext import commands |
| 9 | +from discord.ext.commands import Context |
| 10 | +from discord_slash import cog_ext, SlashContext |
| 11 | +from discord_slash.utils.manage_commands import create_option |
| 12 | +from discord_slash.utils.manage_components import create_button, create_actionrow |
| 13 | +from copy import deepcopy |
| 14 | + |
| 15 | +type_buttons = create_actionrow( |
| 16 | + *[ |
| 17 | + create_button( |
| 18 | + style=ButtonStyle.green, |
| 19 | + label="簡介", |
| 20 | + emoji={"name": "info", "id": 850732950600679464}, |
| 21 | + ), |
| 22 | + create_button( |
| 23 | + style=ButtonStyle.gray, |
| 24 | + label="專武", |
| 25 | + emoji={"name": "ue", "id": 850732950642884608}, |
| 26 | + ), |
| 27 | + create_button( |
| 28 | + style=ButtonStyle.blue, |
| 29 | + label="技能", |
| 30 | + emoji={"name": "skill", "id": 850732950847881226}, |
| 31 | + ), |
| 32 | + create_button( |
| 33 | + style=ButtonStyle.gray, |
| 34 | + label="攻擊", |
| 35 | + emoji={"name": "icon_skill_attack", "id": 605337612835749908}, |
| 36 | + ), |
| 37 | + create_button( |
| 38 | + style=ButtonStyle.red, |
| 39 | + label="RANK推薦", |
| 40 | + emoji={"name": "rank", "id": 850732950525575178}, |
| 41 | + ), |
| 42 | + ] |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +class Character(commands.Cog): |
| 47 | + def __init__(self, bot: AIKyaru): |
| 48 | + self.bot = bot |
| 49 | + self.embedMaker = Embeds(bot) |
| 50 | + |
| 51 | + @commands.command(name="info", brief="角色資訊", description="查詢角色資訊", aliases=["i"], usage="<角色關鍵字>") |
| 52 | + async def cmd_info(self, ctx: Context, *, keyword: str): |
| 53 | + await self._init_embed(ctx, keyword, 1) |
| 54 | + |
| 55 | + @commands.command(name="ue", brief="角色專武", description="查詢角色專屬武器", usage="<角色關鍵字>") |
| 56 | + async def cmd_ue(self, ctx: Context, *, keyword: str): |
| 57 | + await self._init_embed(ctx, keyword, 2) |
| 58 | + |
| 59 | + @commands.command(name="skill", brief="角色技能", description="查詢角色技能", usage="<角色關鍵字>") |
| 60 | + async def cmd_skill(self, ctx: Context, *, keyword: str): |
| 61 | + await self._init_embed(ctx, keyword, 3) |
| 62 | + |
| 63 | + @commands.command(name="attack", brief="角色攻擊模式", description="查詢角色攻擊模式", aliases=["atk"], usage="<角色關鍵字>") |
| 64 | + async def cmd_attack(self, ctx: Context, *, keyword: str): |
| 65 | + await self._init_embed(ctx, keyword, 4) |
| 66 | + |
| 67 | + @commands.command(name="rank", brief="RANK推薦", description="查詢RANK推薦", usage="<角色關鍵字>") |
| 68 | + async def cmd_rank(self, ctx: Context, *, keyword: str): |
| 69 | + await self._init_embed(ctx, keyword, 5) |
| 70 | + |
| 71 | + @cog_ext.cog_slash( |
| 72 | + name="character", |
| 73 | + description="查詢角色資訊", |
| 74 | + options=[create_option(name="角色", description="可以是角色名稱或關鍵字", option_type=3, required=True)], |
| 75 | + connector={"角色": "keyword"}, |
| 76 | + ) |
| 77 | + async def cog_menu(self, ctx: SlashContext, keyword: str): |
| 78 | + type = await ctx.state.get_user(keys=["config", "character_default_type"]) or 1 |
| 79 | + |
| 80 | + await self._init_embed(ctx, keyword, type) |
| 81 | + |
| 82 | + @cog_ext.cog_component() |
| 83 | + async def pref_character(self, ctx: ComponentContext): |
| 84 | + await self._handle_button(ctx) |
| 85 | + |
| 86 | + async def _init_embed(self, ctx: Union[Context, SlashContext], keyword: str, type: int): |
| 87 | + unit = self.bot.config.get_character(keyword) |
| 88 | + if not unit: |
| 89 | + return await ctx.send(f"找不到跟`{keyword}`有關的角色...") |
| 90 | + |
| 91 | + await ctx.send(**self.create_embed(unit, type)) |
| 92 | + |
| 93 | + async def _handle_button(self, ctx: ComponentContext): |
| 94 | + # i = unit_id, t = type |
| 95 | + data = un_pref_custom_id(custom_id="character", data=ctx.custom_id) |
| 96 | + unit = self.bot.config.get_character_by_id(data["i"]) |
| 97 | + |
| 98 | + await ctx.edit_origin(**self.create_embed(unit, data["t"])) |
| 99 | + |
| 100 | + def create_embed(self, unit: Unit, type: int): |
| 101 | + if type == 1: |
| 102 | + embed = self.embedMaker.profile(unit) |
| 103 | + elif type == 2: |
| 104 | + embed = self.embedMaker.unique_equipment(unit) |
| 105 | + elif type == 3: |
| 106 | + embed = self.embedMaker.skill(unit) |
| 107 | + elif type == 4: |
| 108 | + embed = self.embedMaker.atk_pattern(unit) |
| 109 | + elif type == 5: |
| 110 | + embed = self.embedMaker.rank(unit) |
| 111 | + |
| 112 | + # set button |
| 113 | + buttons = deepcopy(type_buttons) |
| 114 | + buttons["components"][type - 1]["disabled"] = True |
| 115 | + |
| 116 | + for i, j in enumerate(buttons["components"]): |
| 117 | + # i = unit_id, t = type |
| 118 | + j["custom_id"] = pref_custom_id(custom_id="character", data={"i": unit.id, "t": i + 1}) |
| 119 | + |
| 120 | + return {"embed": embed, "components": [buttons]} |
| 121 | + |
| 122 | + |
| 123 | +def setup(bot): |
| 124 | + bot.add_cog(Character(bot)) |
0 commit comments