async def ttt_game_ai(self, ctx: SlashContext): components = [[ Button(label="Easy", style=ButtonStyle.blue, custom_id="ttt_easy"), Button(label="Impossible", style=ButtonStyle.blue, custom_id="ttt_imp"), ]] embed = Embed( title="Tic Tac Toe Game", description="**Choose a difficult:**" "\n`Easy` - Bot random clicks on free cell" "\n`Impossible` - Bot with minimax AI.", color=await self.bot.get_embed_color(ctx.guild_id), timestamp=datetime.utcnow(), ) embed.set_thumbnail(url=ctx.bot.user.avatar_url) message = await ctx.send(embed=embed, components=components) try: button_ctx: ComponentContext = await self.bot.wait_for( "button_click", check=lambda __ctx: __ctx.author_id == ctx.author_id and __ctx. origin_message.id == message.id, timeout=60, ) except TimeoutError: return await message.delete() if button_ctx.custom_id == "ttt_easy": mode = TicTacToeMode.easy else: mode = TicTacToeMode.impossible ttt = TicTacToeAI(self.bot, button_ctx, mode=mode) await ttt.start(edit_origin=True, message=message)
def _get_bot_menu_components(self): return [ Select( placeholder="Reload extensions", custom_id="select_reload_extensions", options=[ SelectOption(label=extension[5:], value=extension) for extension in self.bot.extensions ], max_values=len(self.bot.extensions), ), [ Button( style=ButtonStyle.blue, label="Reload bot", custom_id="button_reload_bot", ), Button( style=ButtonStyle.blue, label="Sync commands", custom_id="button_sync_commands", ), Button(style=ButtonStyle.red, label="Exit", custom_id="button_exit"), ], ]
async def invite_bot(self, ctx: SlashContext): content = get_content("INVITE_COMMAND", lang=await self.bot.get_guild_bot_lang(ctx.guild_id)) components = [[ Button( style=ButtonStyle.URL, label=content["INVITE_BUTTON_NO_PERMS"], url=self.bot.no_perms_invite_link, ), Button( style=ButtonStyle.URL, label=content["INVITE_BUTTON_ADMIN"], url=self.bot.admin_invite_link, ), Button( style=ButtonStyle.URL, label=content["INVITE_BUTTON_RECOMMENDED"], url=self.bot.recommended_invite_link, ), ]] await ctx.send( content["CLICK_TO_INVITE_TEXT"], components=components, )
def _render_start_components(self): components = [ Button(label=" ", custom_id=f"pairs|{ind}") for ind in range(24) ] components.append( Button(label="❌", style=ButtonStyle.blue, custom_id="pairs|close")) self.raw_components = components return spread_to_rows(components)
async def invite_to_game(self, ctx, member, game_name): lang = await self.bot.get_guild_bot_lang(ctx.guild_id) content = get_content("FUNC_INVITE_TO_GAME", lang) button_label_agree = content["BUTTON_AGREE"] button_label_decline = content["BUTTON_DECLINE"] invite_text = content["INVITE_MESSAGE_CONTENT"].format( member.mention, ctx.author.name, game_name) components = [[ Button(style=ButtonStyle.green, label=button_label_agree, custom_id="agree"), Button( style=ButtonStyle.red, label=button_label_decline, custom_id="decline", ), ]] message = await ctx.send(content=invite_text, components=components) if member.bot: embed = Embed( title=game_name, description= f"{ctx.author.display_name} VS {member.display_name}", color=await self.bot.get_embed_color(ctx.guild.id), ) await message.edit(content=" ", embed=embed) return message, True try: button_ctx: ComponentContext = await self.bot.wait_for( "button_click", check=lambda _ctx: _ctx.author_id == member.id, timeout=60, ) accepted_invite = content["AGREE_MESSAGE_CONTENT"] await button_ctx.send(accepted_invite, hidden=True) except TimeoutError: timeout_error = content["TIMEOUT_MESSAGE_CONTENT"].format( member.display_name) await message.edit(content=timeout_error, components=[]) return message, False if button_ctx.custom_id == "agree": embed = Embed( title=game_name, description= f"{ctx.author.display_name} VS {member.display_name}", color=await self.bot.get_embed_color(ctx.guild.id), ) await message.edit(content=" ", embed=embed) return message, True declined_invite = content["DECLINE_MESSAGE_CONTENT"].format( member.display_name) await message.edit(content=declined_invite, components=[]) return message, False
def render_gameboard(self, disable: bool = False): if self.board is None: self.board = deepcopy(BoardTemplate) components = [] for i in range(3): components.insert(i, []) for x in range(3): if self.board[i][x] == GameState.empty: style = ButtonStyle.gray emoji = None elif self.board[i][x] == GameState.ai: style = ButtonStyle.red emoji = self.emoji_circle else: style = ButtonStyle.green emoji = self.emoji_cross components[i].insert( x, Button( label=" " if not emoji else "", emoji=emoji, style=style, custom_id=f"{i} {x}", disabled=disable or style != ButtonStyle.gray, ), ) return components
async def private_voice_create_menu(self, ctx: SlashContext): await ctx.defer(hidden=True) guild_data = await self.bot.mongo.get_guild_data(ctx.guild_id) content = get_content("PRIVATE_VOICE", guild_data.configuration.language) components = [ [ Button(emoji=self.bot.get_emoji(959124362840113182), custom_id="voice_close"), Button(emoji=self.bot.get_emoji(959124362890461254), custom_id="voice_open"), Button(emoji=self.bot.get_emoji(959124362890461325), custom_id="voice_hide"), Button(emoji=self.bot.get_emoji(959124362890473582), custom_id="voice_unhide"), Button( emoji=self.bot.get_emoji(959124362798174319), custom_id="voice_change_room_name" ), ], [ Button(emoji=self.bot.get_emoji(959124362882068550), custom_id="voice_ban"), Button(emoji=self.bot.get_emoji(959124362835931236), custom_id="voice_unban"), Button(emoji=self.bot.get_emoji(959124362974343169), custom_id="voice_kick"), Button(emoji=self.bot.get_emoji(959124362823340052), custom_id="voice_transfer"), Button( emoji=self.bot.get_emoji(959124362835927080), custom_id="voice_set_room_limit" ), ], ] category = await ctx.guild.create_category(content["PRIVATE_ROOMS"]) voice_channel = await category.create_voice_channel(content["CREATE_ROOM"]) overwrites = { ctx.guild.default_role: PermissionOverwrite( send_messages=False, use_slash_commands=False ) } text_channel: TextChannel = await category.create_text_channel( content["ROOM_CONTROL"], overwrites=overwrites ) await guild_data.create_private_voice(text_channel.id, voice_channel.id) embed = Embed( title=content["ROOM_CONTROL_TITLE"], description="".join(content["ROOM_CONTROL_DESCRIPTION"]), color=DiscordColors.EMBED_COLOR, ) await text_channel.send(embed=embed, components=components) await ctx.send(content["SUCCESSFULLY_CREATED"])
async def autorole_button_add_role( self, ctx: SlashContext, name: str, role: Role, label: str = None, style: int = None, emoji: str = None, ): await ctx.defer(hidden=True) guild_data = await self.bot.mongo.get_guild_data(ctx.guild_id) content = get_content("AUTOROLE_BUTTON", guild_data.configuration.language) if not label and not emoji: return await ctx.send(content["AT_LEAST_LABEL_EMOJI_TEXT"]) autoroles = guild_data.autoroles if not autoroles: return await ctx.send(content["NO_AUTOROLES"]) autorole = None for autorole in autoroles: if autorole.name == name: break if not autorole: return await ctx.send(content["NO_AUTOROLES"]) if autorole.type != "buttons": return await ctx.send(content["NOT_BUTTONS_AUTOROLE"]) button = Button( label=label, emoji=self.get_emoji(emoji) if emoji else None, style=style or ButtonStyle.gray, custom_id=f"autorole_button|{role.id}", ) original_message = await ctx.channel.fetch_message( int(autorole.message_id)) original_components = original_message.components if not original_components: original_components = [button] else: for row in original_components: if len(row) < 5 and not isinstance(row[0], Select): row.append(button) break else: if len(original_components) == 5: return await ctx.send(content["LIMIT_25_BUTTONS"]) else: original_components.append([button]) await original_message.edit(components=original_components) await ctx.send(content["COMMAND_ROLE_ADDED_TEXT"], hidden=True) await autorole.update_component( [actionrow.to_dict() for actionrow in original_components])
def _render_components(self, *, disabled: bool = False): components = [None] * len(self.board) for num in self.board: i = self.board.index(num) if num == 0: components[i] = Button( label=" ", style=ButtonStyle.gray, custom_id="tiles|0", disabled=disabled, ) else: components[i] = Button( label=f"{num}", style=ButtonStyle.blue, custom_id=f"tiles|{num}", disabled=disabled, ) components = spread_to_rows(components, 4) return components
def __init__( self, bot: AsteroidBot, ctx: SlashContext, member: Member, message: ComponentMessage, total_rounds: int, ) -> None: self.bot = bot self.ctx = ctx self.member = member self.message = message self.total_rounds = total_rounds self.guild_id = ctx.guild_id self.components = [[ Button(style=ButtonStyle.blue, custom_id="rock", emoji="🪨"), Button(style=ButtonStyle.green, custom_id="paper", emoji="🧾"), Button(style=ButtonStyle.red, custom_id="scissors", emoji="✂️"), ]]
def _render_start_components(self): raw_components = [] for num in self.board: if num == 0: raw_components.append( Button( label=" ", style=ButtonStyle.gray, disabled=True, custom_id=f"mm|empty{uuid1()}", ) ) else: raw_components.append( Button( emoji=emoji[str(num)], style=ButtonStyle.gray, custom_id=f"mm|{num}", disabled=True, ) ) self.components = spread_to_rows(raw_components) return self.components
def __init__(self, bot: AsteroidBot): self.bot = bot self.normal_components = [ [ Button(style=ButtonStyle.grey, label="1", custom_id="1"), Button(style=ButtonStyle.grey, label="2", custom_id="2"), Button(style=ButtonStyle.grey, label="3", custom_id="3"), Button(style=ButtonStyle.blue, label="×", custom_id="*"), Button(style=ButtonStyle.red, label="Exit", custom_id="Exit"), ], [ Button(style=ButtonStyle.grey, label="4", custom_id="4"), Button(style=ButtonStyle.grey, label="5", custom_id="5"), Button(style=ButtonStyle.grey, label="6", custom_id="6"), Button(style=ButtonStyle.blue, label="÷", custom_id="/"), Button(style=ButtonStyle.red, label="⌫", custom_id="⌫"), ], [ Button(style=ButtonStyle.grey, label="7", custom_id="7"), Button(style=ButtonStyle.grey, label="8", custom_id="8"), Button(style=ButtonStyle.grey, label="9", custom_id="9"), Button(style=ButtonStyle.blue, label="+", custom_id="+"), Button(style=ButtonStyle.red, label="Clear", custom_id="Clear"), ], [ Button(style=ButtonStyle.grey, label="00", custom_id="00"), Button(style=ButtonStyle.grey, label="0", custom_id="0"), Button(style=ButtonStyle.grey, label=".", custom_id="."), Button(style=ButtonStyle.blue, label="-", custom_id="-"), Button(style=ButtonStyle.green, label="=", custom_id="="), ], [ Button(style=ButtonStyle.green, label="❮", custom_id="❮"), Button(style=ButtonStyle.green, label="❯", custom_id="❯"), Button( style=ButtonStyle.grey, label="Change to scientific mode", emoji="\U0001f9d1\u200D\U0001f52c", custom_id="scientific_mode", ), ], ] self.scientific_components = [ [ Button(style=ButtonStyle.grey, label="(", custom_id="("), Button(style=ButtonStyle.grey, label=")", custom_id=")"), Button(style=ButtonStyle.grey, label="π", custom_id=str(pi)), Button(style=ButtonStyle.blue, label="×", custom_id="*"), Button(style=ButtonStyle.red, label="Exit", custom_id="Exit"), ], [ Button(style=ButtonStyle.grey, label="X²", disabled=True), Button(style=ButtonStyle.grey, label="X³", disabled=True), Button(style=ButtonStyle.grey, label="Xˣ", disabled=True), Button(style=ButtonStyle.blue, label="÷", custom_id="/"), Button(style=ButtonStyle.red, label="⌫", custom_id="⌫"), ], [ Button(style=ButtonStyle.grey, label="e", custom_id=str(e)), Button(style=ButtonStyle.grey, label="τ", custom_id=str(tau)), Button(style=ButtonStyle.grey, label="000", custom_id="000"), Button(style=ButtonStyle.blue, label="+", custom_id="+"), Button(style=ButtonStyle.red, label="Clear", custom_id="Clear"), ], [ Button(style=ButtonStyle.grey, label="√", custom_id="√"), Button(style=ButtonStyle.grey, label=" ", disabled=True), Button(style=ButtonStyle.grey, label=" ", disabled=True), Button(style=ButtonStyle.blue, label="-", custom_id="-"), Button(style=ButtonStyle.green, label="=", custom_id="="), ], [ Button(style=ButtonStyle.green, label="❮", custom_id="❮"), Button(style=ButtonStyle.green, label="❯", custom_id="❯"), Button( style=ButtonStyle.grey, label="Change to normal modeㅤ", emoji="\U0001f468\u200D\U0001f3eb", custom_id="normal_mode", ), ], ]
async def _start_random(self, ctx: SlashContext, _list: list = None): lang = await self.bot.get_guild_bot_lang(ctx.guild_id) content = get_content("FUNC_RANDOM_ITEMS", lang) if _list is None: _list = content["ITEMS_LIST"] components = [ Button( style=ButtonStyle.blue, label=content["SELECT_BUTTON"], custom_id="toggle", ), Select( placeholder=content["SELECT_ITEMS_TEXT"], options=[ SelectOption(label=item, value=item) for item in _list ], max_values=len(_list), ), [ Button( label=content["START_RANDOM_BUTTON"], custom_id="start_random", style=ButtonStyle.green, ), Button( label=content["EXIT_BUTTON"], custom_id="exit", style=ButtonStyle.red, ), ], ] selected = None is_exception = False is_removed = False embed = Embed( title=content["EMBED_TITLE"], color=await self.bot.get_embed_color(ctx.guild_id), ) message = await ctx.send(embed=embed, components=components) message_for_update = await ctx.send(content["SECOND_MESSAGE_CONTENT"]) while True: try: button_ctx = await self._get_button_ctx( ctx, message, message_for_update) except TimeoutError: return if isinstance(button_ctx.component, Select): selected = button_ctx.values if is_exception: _selected = _list.copy() for item in selected: _selected.remove(item) selected = _selected embed.description = content["SELECTED_ITEMS_TEXT"] + ", ".join( selected) await button_ctx.edit_origin(embed=embed) elif button_ctx.custom_id == "toggle": is_exception = not is_exception button_ctx.component.label = (content["EXCEPTION_BUTTON"] if is_exception else content["SELECT_BUTTON"]) selected = None is_removed = False embed.description = "" await button_ctx.edit_origin( embed=embed, components=button_ctx.origin_message.components) elif button_ctx.custom_id == "start_random": if not is_exception and selected is not None: item = choice(selected) await message_for_update.edit(content=item) elif is_exception and selected is not None: if not is_removed: is_removed = True items = _list.copy() for item in selected: items.remove(item) item = choice(selected) await message_for_update.edit(content=item) elif is_exception: selected = _list item = choice(selected) await message_for_update.edit(content=item) elif button_ctx.custom_id == "exit": await message.delete() await message_for_update.delete() return
async def open_control_menu(self, ctx: SlashContext): components = [ [ Button( style=ButtonStyle.green, label="Check Bot updates", custom_id="update_bot", emoji="📥", ), Button( style=ButtonStyle.gray, label="Check discord.py updates", custom_id="update_discord.py", emoji=self.bot.get_emoji(964091772416454676), ), Button( style=ButtonStyle.gray, label="Check discord-slash updates", custom_id="update_discord-slash", emoji=self.bot.get_emoji(963104705406439425), ), ], [ Button( style=ButtonStyle.red, label="Reload Bot", custom_id="reload_bot", emoji=self.bot.get_emoji(963104705888804976), ), Button( style=ButtonStyle.gray, label="Reload all extensions", custom_id="reload_cogs", emoji="🧱", ), Button( style=ButtonStyle.gray, label="Reload localization", custom_id="reload_locales", emoji="🇺🇸", ), ], [ Button( style=ButtonStyle.blue, label="Sync commands", custom_id="sync_commands", emoji=self.bot.get_emoji(963104705406439425), ), ], [ Select( placeholder="Reload extensions", custom_id="reload_extensions", options=[ SelectOption(label=extension[5:], value=extension) for extension in self.bot.extensions ], max_values=len(self.bot.extensions), ), ], ] embed = Embed(title="Bot Control Menu", color=DiscordColors.EMBED_COLOR) await ctx.send(embed=embed, components=components)
from copy import deepcopy from enum import IntEnum from typing import List, Union from discord import Client, Embed from discord.ext.commands import Bot from discord_slash import Button, ButtonStyle, ComponentContext, ComponentMessage, SlashContext components = { 1: [[ Button(style=ButtonStyle.gray, label="←", custom_id="back", disabled=True), Button(style=ButtonStyle.gray, label="→", custom_id="next"), ]], 2: [[ Button(style=ButtonStyle.gray, label="<<", custom_id="first", disabled=True), Button(style=ButtonStyle.gray, label="←", custom_id="back", disabled=True), Button(style=ButtonStyle.gray, label="→", custom_id="next"), Button(style=ButtonStyle.gray, label=">>", custom_id="last"), ]], 3: [[ Button(style=ButtonStyle.gray, label="←",