Here's an example implementation of the vc_game command using discord.py.
import discord
from discord.ext import commands
import random
# Initialize the bot
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Store game data
games = {}
class VCGameView(discord.ui.View):
def __init__(self, ctx):
super().__init__(timeout=None)
self.ctx = ctx
self.game_data = games[ctx.channel.id]
self.game_data['view'] = self
# Initial buttons
self.add_item(discord.ui.Button(label='Make Teams', custom_id='make_teams', style=discord.ButtonStyle.primary))
self.add_item(discord.ui.Button(label='Start', custom_id='start', style=discord.ButtonStyle.success, disabled=True))
@discord.ui.button(label='Make Teams', custom_id='make_teams', style=discord.ButtonStyle.primary)
async def make_teams(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.ctx.author:
return
# Remove initial buttons
self.clear_items()
# Add team count input
self.add_item(TeamCountModal(self.ctx))
# Send a message to input team count
await interaction.response.send_message('Enter the number of teams:', view=self, ephemeral=True)
@discord.ui.button(label='Start', custom_id='start', style=discord.ButtonStyle.success, disabled=True)
async def start(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.ctx.author:
return
# Remove initial buttons
self.clear_items()
# Add game control buttons
self.add_item(discord.ui.Button(label='+1 points', custom_id='add_point', style=discord.ButtonStyle.success))
self.add_item(discord.ui.Button(label='-1 points', custom_id='remove_point', style=discord.ButtonStyle.danger))
self.add_item(discord.ui.Button(label='Next', custom_id='next', style=discord.ButtonStyle.primary))
self.add_item(discord.ui.Button(label='End Game', custom_id='end_game', style=discord.ButtonStyle.danger))
# Start the game
self.game_data['current_team'] = 0
await interaction.response.edit_message(content=f'Team {self.game_data["teams"][self.game_data["current_team"]]["name"]} goes first.', view=self)
async def interaction_check(self, interaction: discord.Interaction):
if interaction.data['custom_id'] == 'add_point':
self.game_data['teams'][self.game_data['current_team']]['points'] += 1
await interaction.response.edit_message(content=f'Team {self.game_data["teams"][self.game_data["current_team"]]["name"]} - {self.game_data["teams"][self.game_data["current_team"]]["points"]} points', view=self)
elif interaction.data['custom_id'] == 'remove_point':
self.game_data['teams'][self.game_data['current_team']]['points'] -= 1
await interaction.response.edit_message(content=f'Team {self.game_data["teams"][self.game_data["current_team"]]["name"]} - {self.game_data["teams"][self.game_data["current_team"]]["points"]} points', view=self)
elif interaction.data['custom_id'] == 'next':
self.game_data['current_team'] = (self.game_data['current_team'] + 1) % len(self.game_data['teams'])
await interaction.response.edit_message(content=f'Team {self.game_data["teams"][self.game_data["current_team"]]["name"]} goes next.', view=self)
elif interaction.data['custom_id'] == 'end_game':
winner = max(self.game_data['teams'], key=lambda x: x['points'])
await interaction.response.edit_message(content=f'Game over! Team {winner["name"]} wins with {winner["points"]} points!', view=None)
del games[interaction.channel.id]
return True
class TeamCountModal(discord.ui.Modal):
def __init__(self, ctx):
super().__init__(title='Enter Team Count')
self.ctx = ctx
self.add_item(discord.ui.TextInput(label='Team Count', placeholder='2', required=True))
async def callback(self, interaction: discord.Interaction):
team_count = int(self.children[0].value)
games[self.ctx.channel.id]['team_count'] = team_count
# Ask for team assignment method
view = TeamAssignmentView(self.ctx)
await interaction.response.send_message('How do you want to assign teams?', view=view, ephemeral=True)
class TeamAssignmentView(discord.ui.View):
def __init__(self, ctx):
super().__init__(timeout=None)
self.ctx = ctx
# Add team assignment buttons
self.add_item(discord.ui.Button(label='Random', custom_id='random', style=discord.ButtonStyle.primary))
self.add_item(discord.ui.Button(label='Manual', custom_id='manual', style=discord.ButtonStyle.success))
@discord.ui.button(label='Random', custom_id='random', style=discord.ButtonStyle.primary)
async def random(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.ctx.author:
return
# Remove team assignment buttons
self.clear_items()
# Add player input
self.add_item(PlayerInputModal(self.ctx, 'random'))
# Send a message to input players
await interaction.response.send_message('Enter the players (@ mentions):', view=self, ephemeral=True)
@discord.ui.button(label='Manual', custom_id='manual', style=discord.ButtonStyle.success)
async def manual(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.ctx.author:
return
# Remove team assignment buttons
self.clear_items()
# Add player input
self.add_item(PlayerInputModal(self.ctx, 'manual'))
# Send a message to input players
await interaction.response.send_message('Enter the players (@ mentions):', view=self, ephemeral=True)
class PlayerInputModal(discord.ui.Modal):
def __init__(self, ctx, assignment_method):
super().__init__(title='Enter Players')
self.ctx = ctx
self.assignment_method = assignment_method
self.add_item(discord.ui.TextInput(label='Players', placeholder='@player1 @player2', required=True))
async def callback(self, interaction: discord.Interaction):
players = [player.strip() for player in self.children[0].value.split()]
team_count = games[self.ctx.channel.id]['team_count']
if self.assignment_method == 'random':
# Randomly assign players to teams
random.shuffle(players)
teams = [players[i::team_count] for i in range(team_count)]
games[self.ctx.channel.id]['teams'] = [{'name': f'Team {i+1}', 'players': team, 'points': 0} for i, team in enumerate(teams)]
elif self.assignment_method == 'manual':
# Manually assign players to teams
view = ManualTeamAssignmentView(self.ctx, players)
await interaction.response.send_message('Assign players to teams:', view=view, ephemeral=True)
return
# Enable the start button
games[self.ctx.channel.id]['view'].children[1].disabled = False
await interaction.response.send_message('Teams assigned successfully!', ephemeral=True)
class ManualTeamAssignmentView(discord.ui.View):
def __init__(self, ctx, players):
super().__init__(timeout=None)
self.ctx = ctx
self.players = players
self.teams = [[] for _ in range(games[ctx.channel.id]['team_count'])]
# Add player assignment buttons
for i, player in enumerate(players):
self.add_item(discord.ui.Button(label=player, custom_id=str(i), style=discord.ButtonStyle.primary))
# Add team selection dropdown
self.add_item(TeamSelectDropdown(self.ctx))
@discord.ui.button(label='Done', custom_id='done', style=discord.ButtonStyle.success)
async def done(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user != self.ctx.author:
return
# Create team data
games[self.ctx.channel.id]['teams'] = [{'name': f'Team {i+1}', 'players': team, 'points': 0} for i, team in enumerate(self.teams)]
# Enable the start button
games[self.ctx.channel.id]['view'].children[1].disabled = False
await interaction.response.send_message('Teams assigned successfully!', ephemeral=True)
class TeamSelectDropdown(discord.ui.Select):
def __init__(self, ctx):
super().__init__(placeholder='Select a team', options=[discord.SelectOption(label=f'Team {i+1}', value=str(i)) for i in range(games[ctx.channel.id]['team_count'])])
async def callback(self, interaction: discord.Interaction):
team_index = int(self.values[0])
player_index = int(interaction.message.components[0].children[int(interaction.data['custom_id'])].label.split('@')[1].strip('>'))
games[interaction.message.channel.id]['view'].children[int(interaction.data['custom_id'])].style = discord.ButtonStyle.success
await interaction.response.edit_message(view=games[interaction.message.channel.id]['view'])
games[interaction.message.channel.id]['teams'][team_index].append(self.view.players[player_index])
@bot.slash_command(name='vc_game', description='Start a VC game')