Skip to content

Commit

Permalink
Fixed message NoneType if no interactions happens before timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
PieTw3lve committed Jul 20, 2024
1 parent 69724aa commit 3b479fd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from lightbulb.ext import tasks

VERSION = '1.3.2'
VERSION = '1.3.3'

## Functions ##

Expand Down
9 changes: 5 additions & 4 deletions src/extensions/economy/blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ def cards(self):
return deck

class BlackJackView(miru.View):
def __init__(self, embed: hikari.Embed, author: hikari.User, bet: int, deck: Deck, player: Player, dealer: Dealer) -> None:
def __init__(self, ctx: lightbulb.Context, embed: hikari.Embed, bet: int, deck: Deck, player: Player, dealer: Dealer) -> None:
super().__init__(timeout=60.0)
self.ctx = ctx
self.embed = embed
self.author = author
self.author = ctx.author
self.bet = bet
self.deck = deck
self.player = player
Expand Down Expand Up @@ -262,7 +263,7 @@ async def on_timeout(self) -> None:
self.embed.edit_field(0, "Dealer's Hand", f'{" ".join(self.dealer.cards())}\nValue: {self.dealer.hand.score()}')
self.embed.edit_field(1, "Your Hand", f'{" ".join(self.player.cards())}\nValue: {self.player.hand.score()}')
self.embed.set_footer(None)
await self.message.edit(self.embed, components=[])
await self.ctx.edit_last_response(self.embed, components=[])
economy.add_loss(self.author.id, self.bet)

async def view_check(self, ctx: miru.ViewContext) -> bool:
Expand Down Expand Up @@ -310,7 +311,7 @@ async def blackjack(ctx: lightbulb.Context, bet: int) -> None:
economy.remove_money(ctx.author.id, bet, True)
return await ctx.respond(embed)

view = BlackJackView(embed, ctx.user, bet, deck, player, dealer)
view = BlackJackView(ctx, embed, bet, deck, player, dealer)

await ctx.respond(embed, components=view.build())
client = ctx.bot.d.get('client')
Expand Down
23 changes: 12 additions & 11 deletions src/extensions/economy/connect_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def hasWon(self, player):
return False

class Connect4DuelView(miru.View):
def __init__(self, author: hikari.User, opponent: hikari.User, bet: int) -> None:
def __init__(self, ctx: lightbulb.Context, opponent: hikari.User, bet: int) -> None:
super().__init__(timeout=120.0)
self.author = author
self.ctx = ctx
self.author = ctx.author
self.opponent = opponent
self.bet = bet
self.accepted = False
Expand All @@ -95,8 +96,7 @@ async def decline(self, ctx: miru.ViewContext, button: miru.Button) -> None:
embed.set_thumbnail(ctx.user.avatar_url if ctx.user.avatar_url != None else ctx.user.default_avatar_url)
embed.add_field(name='__Game Info__', value=f'Win Condition: Bet: 🪙 {self.bet}')


await self.message.edit(embed, components=[])
await ctx.edit_response(embed, components=[])
self.stop()

async def on_timeout(self) -> None:
Expand All @@ -110,17 +110,18 @@ async def on_timeout(self) -> None:
embed.set_thumbnail(self.author.avatar_url)
embed.add_field(name='__Game Info__', value=f'Bet: 🪙 {self.bet}')

await self.message.edit(embed, components=[])
await self.ctx.edit_last_response(embed, components=[])

async def view_check(self, ctx: miru.ViewContext) -> bool:
return ctx.user.id == self.opponent.id

class Connect4GameView(miru.View):
def __init__(self, game: Connect4, embed: hikari.Embed, author: hikari.User, opponent: hikari.User, bet: int) -> None:
def __init__(self, ctx: lightbulb.Context, game: Connect4, embed: hikari.Embed, opponent: hikari.User, bet: int) -> None:
super().__init__(timeout=None)
self.ctx = ctx
self.game = game
self.embed = embed
self.author = author
self.author = ctx.author
self.opponent = opponent
self.bet = bet

Expand Down Expand Up @@ -187,7 +188,7 @@ async def on_timeout(self) -> None:
economy.add_money(self.opponent.id, self.bet*2, True)
economy.add_loss(self.author.id, self.bet)
self.embed.set_footer(None)
await self.message.edit(self.embed, components=[])
await self.ctx.edit_last_response(self.embed, components=[])
self.stop()

async def view_check(self, ctx: miru.ViewContext) -> bool:
Expand Down Expand Up @@ -221,7 +222,7 @@ async def connect4(ctx: lightbulb.Context, user: hikari.User, bet: int) -> None:
embed.add_field(name='__Game Info__', value=f'Bet: 🪙 {bet}')
embed.set_footer('The duel request will timeout in 2 minutes!')

view = Connect4DuelView(ctx.author, user, bet)
view = Connect4DuelView(ctx, user, bet)

await ctx.respond(embed, components=view.build())

Expand All @@ -238,9 +239,9 @@ async def connect4(ctx: lightbulb.Context, user: hikari.User, bet: int) -> None:
embed.set_thumbnail(ctx.user.avatar_url if ctx.user.avatar_url != None else ctx.user.default_avatar_url)
embed.set_footer('You have 60 seconds to choose an option or you will automatically forfeit!')

view = Connect4GameView(game, embed, ctx.author, user, bet)
view = Connect4GameView(ctx, game, embed, user, bet)

message = await ctx.edit_last_response(embed, components=view.build())
await ctx.edit_last_response(embed, components=view.build())

client = ctx.bot.d.get('client')
client.start_view(view)
Expand Down
12 changes: 6 additions & 6 deletions src/extensions/economy/rps.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def rps(ctx: lightbulb.Context, user: hikari.User, bet: int, wins: int) ->
embed.add_field(name='__Game Info__', value=f'Win Condition: First to `{wins}`!\nBet: 🪙 {bet}')
embed.set_footer('The duel request will timeout in 2 minutes!')

view = DuelView(ctx.author, user, bet, wins)
view = DuelView(ctx, user, bet, wins)

await ctx.respond(embed, components=view.build())

Expand Down Expand Up @@ -92,9 +92,10 @@ async def rps(ctx: lightbulb.Context, user: hikari.User, bet: int, wins: int) ->
await ctx.edit_last_response(embed, components=[])

class DuelView(miru.View):
def __init__(self, author: hikari.User, opponent: hikari.User, bet: int, wins: int) -> None:
def __init__(self, ctx: lightbulb.Context, opponent: hikari.User, bet: int, wins: int) -> None:
super().__init__(timeout=120.0, autodefer=True)
self.author = author
self.ctx = ctx
self.author = ctx.author
self.opponent = opponent
self.bet = bet
self.wins = wins
Expand Down Expand Up @@ -122,8 +123,7 @@ async def decline(self, ctx: miru.ViewContext, button: miru.Button) -> None:
embed.set_thumbnail(ctx.user.avatar_url if ctx.user.avatar_url != None else ctx.user.default_avatar_url)
embed.add_field(name='__Game Info__', value=f'Win Condition: First to `{self.wins}`!\nBet: 🪙 {self.bet}')


await self.message.edit(embed, components=[])
await ctx.edit_response(embed, components=[])
self.stop()

async def on_timeout(self) -> None:
Expand All @@ -137,7 +137,7 @@ async def on_timeout(self) -> None:
embed.set_thumbnail(self.author.avatar_url)
embed.add_field(name='__Game Info__', value=f'Win Condition: First to `{self.wins}`!\nBet: 🪙 {self.bet}')

await self.message.edit(embed, components=[])
await self.ctx.edit_last_response(embed, components=[])

async def view_check(self, ctx: miru.ViewContext) -> bool:
return ctx.user.id == self.opponent.id
Expand Down
16 changes: 9 additions & 7 deletions src/extensions/economy/russian_roulette.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def russian_roulette(ctx: lightbulb.Context, capacity: int, bet: int):
embed.add_field(name='__Player List__', value=f'{", ".join(players)}', inline=True)
embed.set_footer(text=f'The game will timeout in 2 minutes!')

view = RRLobbyView(ctx.author, players, capacity, bet)
view = RRLobbyView(ctx, players, capacity, bet)

await ctx.respond(embed, components=view.build())

Expand All @@ -54,9 +54,10 @@ async def russian_roulette(ctx: lightbulb.Context, capacity: int, bet: int):
client.start_view(view) # starts game

class RRLobbyView(miru.View):
def __init__(self, author: hikari.User, players: dict, capacity: int, amount: int) -> None:
def __init__(self, ctx: lightbulb.Context, players: dict, capacity: int, amount: int) -> None:
super().__init__(timeout=120.0)
self.game = {'author': author, 'players': players, 'capacity': capacity, 'amount': amount, 'pool': amount}
self.ctx = ctx
self.game = {'author': ctx.author, 'players': players, 'capacity': capacity, 'amount': amount, 'pool': amount}
self.gameStart = False

@miru.button(label='Start', style=hikari.ButtonStyle.SUCCESS, row=1)
Expand Down Expand Up @@ -131,14 +132,15 @@ async def on_timeout(self) -> None:
embed.add_field(name='__Game info__', value=f"Initial Bet: 🪙 {self.game['amount']:,}\nBet Pool: 🪙 {self.game['pool']:,}\nChamber Capacity: `{self.game['capacity']:,}`", inline=True)
embed.add_field(name='__Player List__', value=f'`{", ".join(self.game["players"])}`', inline=True)

await self.message.edit(embed, components=[])
await self.ctx.edit_last_response(embed, components=[])

async def view_check(self, ctx: miru.ViewContext) -> bool:
return True

class RRGameView(miru.View):
def __init__(self, game: dict, timeout: float) -> None:
def __init__(self, ctx: lightbulb.Context, game: dict, timeout: float) -> None:
super().__init__(timeout=timeout)
self.ctx = ctx
self.game = game
self.playerIter = iter(game['players'].items())
self.player = next(self.playerIter)[1]
Expand Down Expand Up @@ -211,8 +213,8 @@ async def on_timeout(self) -> None:
economy.add_money(player['id'], self.game['pool'] / (len(self.game['players']) - 1), False)
economy.add_gain(player['id'], self.game['amount'])
economy.add_loss(self.player['id'], self.game['amount'])
await self.message.edit(embed, components=[])

await self.ctx.edit_last_response(embed, components=[])

async def view_check(self, ctx: miru.ViewContext) -> bool:
return ctx.user.id == self.player['id']
Expand Down
9 changes: 5 additions & 4 deletions src/extensions/fun/hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def hangman(ctx: lightbulb.Context, word: str, theme: str) -> None:
embed.add_field(name='__Guessers__', value=f'None', inline=True)
embed.set_footer(text=f'The game will timeout in 2 minutes!')

view = HangmanLobbyView(ctx.author, guessers, word, theme)
view = HangmanLobbyView(ctx, guessers, word, theme)

await ctx.respond(embed, components=view.build(), reply=False)

Expand Down Expand Up @@ -74,9 +74,10 @@ async def hangman(ctx: lightbulb.Context, word: str, theme: str) -> None:
client.start_view(view) # starts game

class HangmanLobbyView(miru.View):
def __init__(self, author: hikari.User, guessers: dict, word: int, theme: int) -> None:
def __init__(self, ctx: lightbulb.Context, guessers: dict, word: int, theme: int) -> None:
super().__init__(timeout=120.0)
self.game = {'author': author, 'guessers': guessers, 'word': word, 'theme': theme}
self.ctx = ctx
self.game = {'author': ctx.author, 'guessers': guessers, 'word': word, 'theme': theme}
self.gameStart = False

@miru.button(label='Start', style=hikari.ButtonStyle.SUCCESS, row=1)
Expand Down Expand Up @@ -120,7 +121,7 @@ async def on_timeout(self) -> None:
embed.set_image('assets/img/fun/hangman/0.png')
embed.set_footer(text=f'The game will timeout in 2 minutes!')

await self.message.edit(embed, components=[])
await self.ctx.edit_last_response(embed, components=[])
self.stop()

class HangmanGameView(miru.View):
Expand Down

0 comments on commit 3b479fd

Please sign in to comment.