DIscord.pyでブラックジャックを作りたいが、ボタンが反応しない

python

1import discord 2from discord.ext import commands 3from discord.ui import Button, View 4import random 5 6bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())7 8class Card:9 suits = ['♤', '♡', '♢', '♧']10 ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']11 12 def __init__(self, suit, rank):13 self.suit = suit 14 self.rank = rank 15 16 def __str__(self):17 return f'| {self.rank} {self.suit} |'18 19 def value(self):20 if self.rank in 'JQK':21 return 1022 elif self.rank == 'A':23 return 1124 else:25 return int(self.rank)26 27class Player:28 def __init__(self):29 self.hand = []30 31class Dealer:32 def __init__(self):33 self.hand = []34 35 36async def blackjack(ctx):37 dealer = Dealer()38 dealer.hand = [draw_card(), draw_card()]39 player = Player()40 player.hand = [draw_card(), draw_card()]41 42 view = BlackjackView(dealer, player)43 message = await ctx.send(embed=game_embed(dealer, player), view=view)44 view.message = message 45 46 47 48class BlackjackView(View):49 def __init__(self, dealer, player):50 super().__init__()51 self.dealer = dealer 52 self.player = player 53 54 async def interaction_check(self, interaction: discord.Interaction) -> bool:55 return interaction.user == self.player or interaction.user == self.bot.user 56 57 58 59 async def on_timeout(self) -> None:60 await self.message.edit(content='タイムアウトしました。', view=None)61 62 63 @discord.ui.button(label='ヒット', style=discord.ButtonStyle.green)64 async def hit(self, button: Button, interaction: discord.Interaction):65 self.player.hand.append(draw_card())66 if hand_value(self.player.hand) > 21:67 await interaction.response.send_message('バストしました。ディーラーの勝ちです。', ephemeral=True)68 self.stop()69 else:70 await interaction.response.edit_message(embed=game_embed(self.dealer, self.player))71 72 @discord.ui.button(label='スタンド', style=discord.ButtonStyle.red)73 async def stand(self, button: Button, interaction: discord.Interaction):74 while hand_value(self.dealer.hand) < 17:75 self.dealer.hand.append(draw_card())76 77 if hand_value(self.dealer.hand) > 21 or hand_value(self.player.hand) > hand_value(self.dealer.hand):78 result = 'プレイヤーの勝ちです。'79 elif hand_value(self.player.hand) == hand_value(self.dealer.hand):80 result = '引き分けです。'81 else:82 result = 'ディーラーの勝ちです。'83 84 await interaction.response.send_message(result, ephemeral=True)85 self.stop()86 87def draw_card():88 suit = random.choice(Card.suits)89 rank = random.choice(Card.ranks)90 return Card(suit, rank)91 92def hand_value(hand):93 value = sum(card.value() for card in hand)94 if value > 21 and any(card.rank == 'A' for card in hand):95 value -= 1096 return value 97 98def game_embed(dealer, player):99 embed = discord.Embed(title='ブラックジャック')100 embed.add_field(name='ディーラー', value=' '.join(str(card) for card in dealer.hand[:1]) + ' | ? |')101 embed.add_field(name='プレイヤー', value=' '.join(str(card) for card in player.hand))102 return embed 103 104 105@bot.event106async def on_ready():107 print(f'{bot.user} がオンラインです。')108 109@bot.command()110async def blackjack(ctx):111 dealer = Dealer()112 dealer.hand = [draw_card(), draw_card()]113 player = Player()114 player.hand = [draw_card(), draw_card()]115 116 view = BlackjackView(dealer, player)117 118 message = await ctx.send(embed=game_embed(dealer, player), view=view)119 view.message = message 120 121 122 123bot.run('TOKEN')124

コメントを投稿

0 コメント