Garde-Havoc/bot/cogs/minecraft.py

285 lines
9.4 KiB
Python
Raw Normal View History

2024-07-24 14:46:46 +02:00
import discord
from discord.ext import commands
import enum
2024-10-09 18:29:51 +02:00
from transitions import Machine
2024-12-18 22:17:51 +01:00
from cogs.spawner import get_server
import utils
color = discord.Color.from_rgb(181, 24, 60)
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
class Whitelist:
2024-07-24 14:46:46 +02:00
"A workflow machine for managing Whitelist states"
2024-10-09 18:29:51 +02:00
On = None
Off = None
toggle = None
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
class States(enum.Enum):
NOTHING = 0
INIT = 1
SAFE = 2
FIGHT = 3
SUDDENDEATH = 4
END = 5
2024-07-24 14:46:46 +02:00
class Minecraft(commands.Cog):
2024-10-09 18:29:51 +02:00
def __init__(self, bot: commands.Bot):
2024-07-24 14:46:46 +02:00
self.bot = bot
2024-10-09 18:29:51 +02:00
transitions = [
['init_game', States.NOTHING, States.INIT],
['start_game', States.INIT, States.SAFE],
['start_fight', States.SAFE, States.FIGHT],
['start_last_round', States.FIGHT, States.SUDDENDEATH],
['end_game', States.FIGHT, States.END],
['end_game', States.SUDDENDEATH, States.END],
['reset', States.END, States.INIT],
['abort', '*', States.NOTHING]
]
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
self.machine = Machine(states=States, transitions=transitions, initial=States.NOTHING)
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
@commands.hybrid_command(name='init')
async def init(self, ctx: commands.Context, server_name: str):
2024-07-24 14:46:46 +02:00
"""
2024-10-09 18:29:51 +02:00
Initialize a Border Wars session
2024-07-24 14:46:46 +02:00
Parameters
----------
ctx: commands.Context
The context of the command invocation
2024-10-09 18:29:51 +02:00
server_name: str
Server on which the Session should be initialized
2024-07-24 14:46:46 +02:00
"""
2024-12-18 22:17:51 +01:00
c = get_server(server_name)
2024-10-09 18:29:51 +02:00
if not c:
await ctx.send("---The server doesn't run---")
return
cmds = [
"/effect give @a minecraft:resistance infinite 255 true",
"/effect give @a minecraft:saturation infinite 4 true",
"/tp @a 0 200 0",
"/gamemode adventure @a",
"/worldborder center 0 0",
"/worldborder set 5",
"/whitelist off"
]
2024-12-18 22:17:51 +01:00
c.rcon.rconnect()
c.rcon.sendcmd(cmds)
embed = discord.Embed(
title=f"Border Wars @ {server_name.title()}",
description='''
Session initialized
Get ready to get a good armorset.
**Explanation**
This phase last as long as the moderator wants.
Every player is immune to damage and can't die by hunger.
Look out for a good direction to scout.
Happy Border Wars!
''',
color=color,
timestamp=utils.now()
)
file = discord.File("../assets/init.png", filename="init.png")
embed.set_thumbnail(url="attachment://init.png")
await ctx.send(file=file, embed=embed)
2024-10-09 18:29:51 +02:00
@commands.hybrid_command(name='safe')
async def safe(self, ctx: commands.Context, server_name: str):
2024-07-24 14:46:46 +02:00
"""
2024-10-09 18:29:51 +02:00
Switches to Safe Phase on a Border Wars session
2024-07-24 14:46:46 +02:00
Parameters
----------
ctx: commands.Context
The context of the command invocation
2024-10-09 18:29:51 +02:00
server_name: str
Server on which the Session should be initialized
2024-07-24 14:46:46 +02:00
"""
2024-12-18 22:17:51 +01:00
c = get_server(server_name)
2024-10-09 18:29:51 +02:00
if not c:
await ctx.send("---The server doesn't run---")
return
2024-12-18 22:17:51 +01:00
2024-07-24 14:46:46 +02:00
cmds = [
2024-10-09 18:29:51 +02:00
'''/title @a subtitle ["",{"text":"bei ","color":"blue"},{"text":"BORDER WARS!","bold":true,"color":"red"}]''',
'''/title @a title {"text":"Viel Glück!","bold":true,"color":"blue"}''',
"playsound minecraft:entity.wither.spawn ambient @a 0 64 080",
"/worldborder set 1000",
"/gamerule keepInventory true",
"/gamemode survival @a",
"/effect clear @a"
2024-07-24 14:46:46 +02:00
]
2024-12-18 22:17:51 +01:00
c.rcon.sendcmd(cmds)
embed = discord.Embed(
title=f"Border Wars @ {server_name.title()}",
description='''
Switched to Safe Phase
Hide from other players and scout for resources.
**Explanation**
This phase lasts 30 minutes, after that the fighting phase begins.
Every player who dies will keep there inventory.
Get a good armorset and be ready to fight.
Happy Border Wars!
''',
color=color,
timestamp=utils.now()
)
file = discord.File("../assets/safe.png", filename="safe.png")
embed.set_thumbnail(url="attachment://safe.png")
await ctx.send(file=file, embed=embed)
2024-10-09 18:29:51 +02:00
@commands.hybrid_command(name='fight')
async def fight(self, ctx: commands.Context, server_name: str):
2024-07-24 14:46:46 +02:00
"""
2024-10-09 18:29:51 +02:00
Switches to Fight Phase on a Border Wars session
2024-07-24 14:46:46 +02:00
Parameters
----------
ctx: commands.Context
The context of the command invocation
2024-10-09 18:29:51 +02:00
server_name: str
Server on which the Session should be initialized
2024-07-24 14:46:46 +02:00
"""
2024-12-18 22:17:51 +01:00
c = get_server(server_name)
2024-10-09 18:29:51 +02:00
if not c:
await ctx.send("---The server doesn't run---")
return
cmds = [
'''/title @a subtitle {"text":"ÜBERLEBEN!","bold":true,"color":"red"}''',
'''/title @a title {"text":"Möge der beste","color":"blue"}''',
"/playsound minecraft:item.totem.use ambient @a 0 64 0 80",
"/worldborder set 75 3600",
"/gamerule keepInventory false"
]
2024-12-18 22:17:51 +01:00
c.rcon.sendcmd(cmds)
embed = discord.Embed(
title=f"Border Wars @ {server_name.title()}",
description='''
Switched to Fight Phase
Now the real fun begins.
**Explanation**
This phase lasts 60 minutes, after that the game ends.
Every player can die now and therefor will lose.
Look out for other players.
Happy Border Wars!
''',
color=color,
timestamp=utils.now()
)
file = discord.File("../assets/fight.png", filename="fight.png")
embed.set_thumbnail(url="attachment://fight.png")
await ctx.send(file=file, embed=embed)
2024-10-09 18:29:51 +02:00
@commands.hybrid_command(name='death')
async def death(self, ctx: commands.Context, server_name: str):
2024-07-24 14:46:46 +02:00
"""
2024-10-09 18:29:51 +02:00
Switches to Sudden Death Phase on a Border Wars session
2024-07-24 14:46:46 +02:00
Parameters
----------
ctx: commands.Context
The context of the command invocation
2024-10-09 18:29:51 +02:00
server_name: str
Server on which the Session should be initialized
2024-07-24 14:46:46 +02:00
"""
2024-12-18 22:17:51 +01:00
c = get_server(server_name)
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
if not c:
await ctx.send("---The server doesn't run---")
return
cmds = [
'''/title @a title ["",{"text":"Sudden ","color":"dark_blue"},{"text":"DEATH!","bold":true,"color":"red"}]''',
"/playsound minecraft:entity.ender_dragon.growl ambient @a 0 64 0 80",
"/worldborder set 5 600"
]
2024-12-18 22:17:51 +01:00
c.rcon.sendcmd(cmds)
embed = discord.Embed(
title=f"Border Wars @ {server_name.title()}",
description='''
Switched to Sudden Death
Only one can be the Winner.
**Explanation**
This phase lasts as long as only one player survives.
The worldborder will now shrink 'till zero zero.
Good Luck.
Happy Border Wars!
''',
color=color,
timestamp=utils.now()
)
file = discord.File("../assets/death.png", filename="death.png")
embed.set_thumbnail(url="attachment://death.png")
await ctx.send(file=file, embed=embed)
2024-10-09 18:29:51 +02:00
@commands.hybrid_command(name='end')
async def end(self, ctx: commands.Context, server_name: str, playername: str):
2024-07-24 14:46:46 +02:00
"""
2024-10-09 18:29:51 +02:00
Ends a Border Wars session
2024-07-24 14:46:46 +02:00
Parameters
----------
ctx: commands.Context
The context of the command invocation
2024-10-09 18:29:51 +02:00
server_name: str
Server on which the Session should be initialized
playername: str
Player which is announced as the Winner
2024-07-24 14:46:46 +02:00
"""
2024-12-18 22:17:51 +01:00
c = get_server(server_name)
2024-10-09 18:29:51 +02:00
if not c:
await ctx.send("---The server doesn't run---")
return
cmds = [
"/worldborder center 0 0",
"/worldborder set 75",
"/gamerule keepInventory true",
'''/title @a subtitle ["",{"text":"''' + playername + '''","bold":true,"color":"red"},{"text":" gewinnt","color":"dark_blue"}]''',
'''/title @a title {"text":"ENDE!","color":"dark_blue"}''',
"/playsound minecraft:entity.ender_dragon_death ambient @a 0 64 0 80",
]
2024-12-18 22:17:51 +01:00
c.rcon.sendcmd(cmds)
embed = discord.Embed(
title=f"Border Wars @ {server_name.title()}",
description=f'''
*Game Over*
Congratulations **{playername}**.
Hope you had fun @ **Border Wars!**
''',
color=color,
timestamp=utils.now()
)
file = discord.File("../assets/end.png", filename="end.png")
embed.set_thumbnail(url="attachment://end.png")
await ctx.send(file=file, embed=embed)