Garde-Havoc/bot/cogs/minecraft.py

272 lines
8.1 KiB
Python
Raw Normal View History

2024-07-24 14:46:46 +02:00
import discord
from discord.ext import commands
from mcrcon import MCRcon
import enum
2024-10-09 18:29:51 +02:00
from transitions import Machine
from cogs.spawner import containers
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
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)
2024-10-09 18:29:51 +02:00
def sendcmd(self, cmds) -> None:
2024-07-24 14:46:46 +02:00
if isinstance(cmds, str):
2024-10-09 18:29:51 +02:00
return self.command(str)
2024-07-24 14:46:46 +02:00
if isinstance(cmds, list):
2024-10-09 18:29:51 +02:00
return [self.command(cmd) for cmd in cmds]
2024-07-24 14:46:46 +02:00
def __del__(self):
self.disconnect()
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
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]
]
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-10-09 18:29:51 +02:00
server_name = server_name.title()
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
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):
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-10-09 18:29:51 +02:00
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
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-10-09 18:29:51 +02:00
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):
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-10-09 18:29:51 +02:00
server_name = server_name.title()
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
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):
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-10-09 18:29:51 +02:00
server_name = server_name.title()
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00
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):
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-10-09 18:29:51 +02:00
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")
2024-07-24 14:46:46 +02:00
2024-10-09 18:29:51 +02:00