Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Last active January 26, 2022 19:31
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Rapptz/dbfd8cd945a9245e5504a54c2b9eda03 to your computer and use it in GitHub Desktop.
Save Rapptz/dbfd8cd945a9245e5504a54c2b9eda03 to your computer and use it in GitHub Desktop.
from discord.ext import commands
import discord
class TicTacToeButton(discord.ui.Button['TicTacToe']):
def __init__(self, x: int, y: int):
super().__init__(style=discord.ButtonStyle.secondary, label='\u200b', row=y)
self.x = x
self.y = y
async def callback(self, interaction: discord.Interaction):
assert self.view is not None
view: TicTacToe = self.view
state = view.board[self.y][self.x]
if state in (view.X, view.O):
return
if view.current_player == view.X:
self.style = discord.ButtonStyle.danger
self.label = 'X'
self.disabled = True
view.board[self.y][self.x] = view.X
view.current_player = view.O
content = "It is now O's turn"
else:
self.style = discord.ButtonStyle.success
self.label = 'O'
self.disabled = True
view.board[self.y][self.x] = view.O
view.current_player = view.X
content = "It is now X's turn"
winner = view.check_board_winner()
if winner is not None:
if winner == view.X:
content = 'X won!'
elif winner == view.O:
content = 'O won!'
else:
content = "It's a tie!"
for child in view.children:
assert isinstance(child, discord.ui.Button) # just to shut up the linter
child.disabled = True
view.stop()
await interaction.response.edit_message(content=content, view=view)
class TicTacToe(discord.ui.View):
X = -1
O = 1
Tie = 2
def __init__(self):
super().__init__()
self.current_player = self.X
self.board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
for x in range(3):
for y in range(3):
self.add_item(TicTacToeButton(x, y))
def check_board_winner(self):
for across in self.board:
value = sum(across)
if value == 3:
return self.O
elif value == -3:
return self.X
# Check vertical
for line in range(3):
value = self.board[0][line] + self.board[1][line] + self.board[2][line]
if value == 3:
return self.O
elif value == -3:
return self.X
# Check diagonals
diag = self.board[0][2] + self.board[1][1] + self.board[2][0]
if diag == 3:
return self.O
elif diag == -3:
return self.X
diag = self.board[0][0] + self.board[1][1] + self.board[2][2]
if diag == 3:
return self.O
elif diag == -3:
return self.X
# If we're here, we need to check if a tie was made
if all(i != 0 for row in self.board for i in row):
return self.Tie
return None
bot = commands.Bot(command_prefix=commands.when_mentioned_or('$'))
@bot.command()
async def tic(ctx: commands.Context):
await ctx.send('Tic Tac Toe: X goes first', view=TicTacToe())
bot.run(token)
@drapespy
Copy link

gaming!

@jay3332
Copy link

jay3332 commented May 26, 2021

ok

@GitwithGario
Copy link

sick

@wyattford
Copy link

wyattford commented May 29, 2021

Installed the latest python version. Working now :)

@thehatkid
Copy link

discord gaming 😳

@Mad880
Copy link

Mad880 commented Jun 6, 2021

interesting

@AstraTheCat
Copy link

I tried copy pasting the code in my test bot, without changing almost nothing (just added the token from my json file) and when I go to run it, VSCode tells me that "Discord has no attribute "ui"

I tried upgrading discord.py, as i thought it was just outdated, but still, it isn't working

@Pranav-Saxena
Copy link

U need to the install the master branch of discord.py first

@PYworkCode
Copy link

U need to the install the master branch of discord.py first

Do you have any idea how to do it in replit, because im using imports there, and i keep getting same error "Discord has no attribute 'ui'"

@NixonXC
Copy link

NixonXC commented Jul 3, 2021

U need to the install the master branch of discord.py first

how to do that

@rye-bbread
Copy link

U need to the install the master branch of discord.py first

how to do that

pip install -U git+https://github.com/Rapptz/discord.py

@nicco382
Copy link

i got a error saying object of type TicTacToe is not JSON serializable

@Yoplayer-1
Copy link

Yoplayer-1 commented Jul 20, 2021

it's just OP.
Does anyone know how to make a dropdown?

@Yoplayer-1
Copy link

U need to the install the master branch of discord.py first

Do you have any idea how to do it in replit, because im using imports there, and i keep getting same error "Discord has no attribute 'ui'"

Try deleting the module and reinstalling it using shell. Or ask it to someone in Repl Talk

@Pranav-Saxena
Copy link

it's just OP.
Does anyone know how to make a dropdown?

selects have been added in master branch. You can go through the master docs to get started

@SidmoGoesBrrr
Copy link

i got a error saying object of type TicTacToe is not JSON serializable

same

@Pancham1603
Copy link

gayming

@Yoplayer-1
Copy link

it's just OP.
Does anyone know how to make a dropdown?

selects have been added in master branch. You can go through the master docs to get started

I can't find the file can you give me the link?

@Pranav-Saxena
Copy link

I can't find the file can you give me the link?

https://discordpy.readthedocs.io/en/master/api.html

@vinller
Copy link

vinller commented Sep 3, 2021

It keeps sending this error what do I do?

Ignoring exception in view <TicTacToe timeout=180.0 children=9> for item <TicTacToeButton style=<ButtonStyle.danger: 4> url=None disabled=True label='X' emoji=None row=0>:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ui/view.py", line 359, in _scheduled_task
    await item.callback(interaction)
  File "main.py", line 49, in callback
    winner = view.check_board_winner()
  File "main.py", line 87, in check_board_winner
    value = sum(across)
TypeError: 'str' object is not callable

@ubeydtalha
Copy link

ubeydtalha commented Nov 2, 2021

Thanks for example ^^

But, weirdly when I try ctx.send("d",view=ViewClass()) it raises TypeError: send() got an unexpected keyword argument 'view' although I use discord.py 2.0.0a. (Edit: view parameter is in source codes)
I solved with sending message firstly and edit its ,by giving view parameter.

msg = await ctx.send("d")
await message.edit(view=ViewClass()) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment