Garde-Havoc/bot/cogs/minecraft.py
2024-10-09 18:29:51 +02:00

272 lines
8.1 KiB
Python

import discord
from discord.ext import commands
from mcrcon import MCRcon
import enum
from transitions import Machine
from cogs.spawner import containers
class Whitelist:
"A workflow machine for managing Whitelist states"
On = None
Off = None
toggle = None
class RCON(MCRcon):
def __init__(self, ip: str, secret: str, port: int = 31066):
super().__init__(ip, secret, port)
self.connect()
def whitelist(self):
self.whitelist.toggle()
cmds = "/whitelist {}".format(self.whitelist.current_state.id)
print(cmds)
def sendcmd(self, cmds) -> None:
if isinstance(cmds, str):
return self.command(str)
if isinstance(cmds, list):
return [self.command(cmd) for cmd in cmds]
def __del__(self):
self.disconnect()
class States(enum.Enum):
NOTHING = 0
INIT = 1
SAFE = 2
FIGHT = 3
SUDDENDEATH = 4
END = 5
class Minecraft(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.servers = dict()
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]
]
self.machine = Machine(states=States, transitions=transitions, initial=States.NOTHING)
@commands.hybrid_command(name='init')
async def init(self, ctx: commands.Context, server_name: str):
"""
Initialize a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
server_name: str
Server on which the Session should be initialized
"""
server_name = server_name.title()
c = None
for container in containers:
if server_name == container.name:
c = container
break
if not c:
await ctx.send("---The server doesn't run---")
return
conn = RCON(str(c.ip), c.rcon_pass, c.rcon_port)
self.servers[server_name] = conn
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"
]
conn.sendcmd(cmds)
await ctx.send("init Border Wars Game")
@commands.hybrid_command(name='safe')
async def safe(self, ctx: commands.Context, server_name: str):
"""
Switches to Safe Phase on a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
server_name: str
Server on which the Session should be initialized
"""
server_name = server_name.title()
c = None
for container in containers:
if server_name == container.name:
c = container
break
if not c:
await ctx.send("---The server doesn't run---")
return
conn = self.servers.get(server_name)
if not conn:
await ctx.send("---Border Wars Session not Initialized---")
return
cmds = [
'''/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"
]
conn.sendcmd(cmds)
await ctx.send("Switched to Safe Phase")
@commands.hybrid_command(name='fight')
async def fight(self, ctx: commands.Context, server_name: str):
"""
Switches to Fight Phase on a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
server_name: str
Server on which the Session should be initialized
"""
server_name = server_name.title()
c = None
for container in containers:
if server_name == container.name:
c = container
break
if not c:
await ctx.send("---The server doesn't run---")
return
conn = self.servers.get(server_name)
if not conn:
await ctx.send("---Border Wars Session not Initialized---")
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"
]
conn.sendcmd(cmds)
await ctx.send("Switched to Fight Phase")
@commands.hybrid_command(name='death')
async def death(self, ctx: commands.Context, server_name: str):
"""
Switches to Sudden Death Phase on a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
server_name: str
Server on which the Session should be initialized
"""
server_name = server_name.title()
c = None
for container in containers:
if server_name == container.name:
c = container
break
if not c:
await ctx.send("---The server doesn't run---")
return
conn = self.servers.get(server_name)
if not conn:
await ctx.send("---Border Wars Session not Initialized---")
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"
]
conn.sendcmd(cmds)
await ctx.send("Switched to Sudden Death Phase")
@commands.hybrid_command(name='end')
async def end(self, ctx: commands.Context, server_name: str, playername: str):
"""
Ends a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
server_name: str
Server on which the Session should be initialized
playername: str
Player which is announced as the Winner
"""
server_name = server_name.title()
c = None
for container in containers:
if server_name == container.name:
c = container
break
if not c:
await ctx.send("---The server doesn't run---")
return
conn = self.servers.get(server_name)
if not conn:
await ctx.send("---Border Wars Session not Initialized---")
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",
]
conn.sendcmd(cmds)
await ctx.send("Ended Border Wars Session")