Skip to content

Commit

Permalink
Python Black Format
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Mar 24, 2024
1 parent c5307ea commit 457b9f9
Show file tree
Hide file tree
Showing 15 changed files with 805 additions and 278 deletions.
28 changes: 18 additions & 10 deletions cogs/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def load_config(self):
with open(config_path) as config_file:
config = json.load(config_file)
return config["PALWORLD_SERVERS"]

async def run_command(self, server_name):
try:
response = await self.rcon_util.rcon_command(server_name, "ShowPlayers")
return response
except Exception as e:
print(f"Error sending command to {server_name}: {e}")
return ""

async def monitor_player_activity(self):
while True:
for server_name, server_info in self.servers.items():
Expand All @@ -52,10 +52,10 @@ async def announce_player_changes(self, server_name, current_players):

def extract_players(self, player_data):
players = set()
lines = player_data.split('\n')[1:]
lines = player_data.split("\n")[1:]
for line in lines:
if line.strip():
parts = line.split(',')
parts = line.split(",")
if len(parts) == 3:
name, _, steamid = parts
players.add((name.strip(), steamid.strip()))
Expand All @@ -64,24 +64,32 @@ def extract_players(self, player_data):
async def announce_player_join(self, server_name, player):
name, steamid = player
if "CONNECTION_CHANNEL" in self.servers[server_name]:
announcement_channel_id = self.servers[server_name]["CONNECTION_CHANNEL"]
channel = self.bot.get_channel(announcement_channel_id)
channel_id = self.servers[server_name]["CONNECTION_CHANNEL"]
channel = self.bot.get_channel(channel_id)
if channel:
now = datetime.datetime.now()
timestamp = now.strftime("%m-%d-%Y at %I:%M:%S %p")
embed = nextcord.Embed(title="Player Joined", description=f"Player joined {server_name}: {name} (SteamID: {steamid})", color=nextcord.Color.blurple())
embed = nextcord.Embed(
title="Player Joined",
description=f"Player joined {server_name}: {name} (SteamID: {steamid})",
color=nextcord.Color.blurple(),
)
embed.set_footer(text=f"Time: {timestamp}")
await channel.send(embed=embed)

async def announce_player_leave(self, server_name, player):
name, steamid = player
if "CONNECTION_CHANNEL" in self.servers[server_name]:
announcement_channel_id = self.servers[server_name]["CONNECTION_CHANNEL"]
channel = self.bot.get_channel(announcement_channel_id)
channel_id = self.servers[server_name]["CONNECTION_CHANNEL"]
channel = self.bot.get_channel(channel_id)
if channel:
now = datetime.datetime.now()
timestamp = now.strftime("%m-%d-%Y at %I:%M:%S %p")
embed = nextcord.Embed(title="Player Left", description=f"Player left {server_name}: {name} (SteamID: {steamid})", color=nextcord.Color.red())
embed = nextcord.Embed(
title="Player Left",
description=f"Player left {server_name}: {name} (SteamID: {steamid})",
color=nextcord.Color.red(),
)
embed.set_footer(text=f"Time: {timestamp}")
await channel.send(embed=embed)

Expand Down
74 changes: 59 additions & 15 deletions cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,30 @@ def __init__(self, bot):
self.current_page = 0

async def generate_help_embed(self):
embed = nextcord.Embed(title="Help Menu", description="List of all available commands.", color=nextcord.Color.blue())
embed.set_footer(text=f"{constants.FOOTER_TEXT}: Page {self.current_page + 1}", icon_url=constants.FOOTER_IMAGE)

commands = self.bot.all_slash_commands if hasattr(self.bot, 'all_slash_commands') else []
embed = nextcord.Embed(
title="Help Menu",
description="List of all available commands.",
color=nextcord.Color.blue(),
)
embed.set_footer(
text=f"{constants.FOOTER_TEXT}: Page {self.current_page + 1}",
icon_url=constants.FOOTER_IMAGE,
)

commands = (
self.bot.all_slash_commands
if hasattr(self.bot, "all_slash_commands")
else []
)
start = self.current_page * 9
end = min(start + 9, len(commands))

for command in commands[start:end]:
embed.add_field(name=f"`/{command.name}`", value=command.description or "No description", inline=True)

embed.add_field(
name=f"`/{command.name}`",
value=command.description or "No description",
inline=True,
)
return embed

@nextcord.ui.button(label="Previous", style=nextcord.ButtonStyle.grey)
Expand All @@ -30,7 +44,11 @@ async def previous_button_callback(self, button, interaction):

@nextcord.ui.button(label="Next", style=nextcord.ButtonStyle.grey)
async def next_button_callback(self, button, interaction):
if (self.current_page + 1) * 9 < len(self.bot.all_slash_commands if hasattr(self.bot, 'all_slash_commands') else []):
if (self.current_page + 1) * 9 < len(
self.bot.all_slash_commands
if hasattr(self.bot, "all_slash_commands")
else []
):
self.current_page += 1
await self.update_help_message(interaction)

Expand All @@ -53,15 +71,41 @@ async def help(self, interaction: nextcord.Interaction):
@nextcord.slash_command(description="Information about the Palworld bot.")
async def about(self, interaction: nextcord.Interaction):

embed = nextcord.Embed(title="Palworld Bot", color=nextcord.Color.blue(), url=constants.TITLE_URL)
embed = nextcord.Embed(
title="Palworld Bot", color=nextcord.Color.blue(), url=constants.TITLE_URL
)
embed.set_footer(text=constants.FOOTER_TEXT, icon_url=constants.FOOTER_IMAGE)
embed.add_field(name="About", value="The bot is an open-source project available [here](https://github.com/dkoz/palworld-bot). You can find more info on our readme. I'm always looking for code contributions and support! If there is something wrong with the bot itself, please let me know!", inline=False)
embed.add_field(name="Creator", value="This bot was created by [Kozejin](https://kozejin.dev). Feel free to add `koz#1337` on discord if you have any questions.", inline=False)
embed.add_field(name="Support", value="If you wish to support the bot, you can join our [Discord](https://discord.gg/3HUq8cJSrX).", inline=False)

discord_button = Button(label="Discord", url="https://discord.gg/3HUq8cJSrX", style=nextcord.ButtonStyle.link)
github_button = Button(label="GitHub", url="https://github.com/dkoz/palworld-bot", style=nextcord.ButtonStyle.link)
kofi_button = Button(label="Support", url="https://ko-fi.com/kozejin", style=nextcord.ButtonStyle.link)
embed.add_field(
name="About",
value="The bot is an open-source project available [here](https://github.com/dkoz/palworld-bot). You can find more info on our readme. I'm always looking for code contributions and support! If there is something wrong with the bot itself, please let me know!",
inline=False,
)
embed.add_field(
name="Creator",
value="This bot was created by [Kozejin](https://kozejin.dev). Feel free to add `koz#1337` on discord if you have any questions.",
inline=False,
)
embed.add_field(
name="Support",
value="If you wish to support the bot, you can join our [Discord](https://discord.gg/3HUq8cJSrX).",
inline=False,
)

discord_button = Button(
label="Discord",
url="https://discord.gg/3HUq8cJSrX",
style=nextcord.ButtonStyle.link,
)
github_button = Button(
label="GitHub",
url="https://github.com/dkoz/palworld-bot",
style=nextcord.ButtonStyle.link,
)
kofi_button = Button(
label="Support",
url="https://ko-fi.com/kozejin",
style=nextcord.ButtonStyle.link,
)

view = View()
view.add_item(discord_button)
Expand Down
42 changes: 33 additions & 9 deletions cogs/kits.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ def load_config(self):
config = json.load(config_file)
self.servers = config["PALWORLD_SERVERS"]

async def autocomplete_server(self, interaction: nextcord.Interaction, current: str):
choices = [server for server in self.servers if current.lower() in server.lower()]
async def autocomplete_server(
self, interaction: nextcord.Interaction, current: str
):
choices = [
server for server in self.servers if current.lower() in server.lower()
]
await interaction.response.send_autocomplete(choices)

@nextcord.slash_command(name="kit", description="Give a kit to a player.", default_member_permissions=nextcord.Permissions(administrator=True))
async def givekit(self, interaction: nextcord.Interaction, steamid: str = nextcord.SlashOption(description="SteamID/UID of the player."), kit_name: str = nextcord.SlashOption(description="The name of the kit.", autocomplete=True), server: str = nextcord.SlashOption(description="Select a server", autocomplete=True)):
@nextcord.slash_command(
name="kit",
description="Give a kit to a player.",
default_member_permissions=nextcord.Permissions(administrator=True),
)
async def givekit(
self,
interaction: nextcord.Interaction,
steamid: str = nextcord.SlashOption(description="SteamID/UID of the player."),
kit_name: str = nextcord.SlashOption(
description="The name of the kit.", autocomplete=True
),
server: str = nextcord.SlashOption(
description="Select a server", autocomplete=True
),
):
await interaction.response.defer()

packages_path = os.path.join('gamedata', 'kits.json')
packages_path = os.path.join("gamedata", "kits.json")
with open(packages_path) as packages_file:
kits = json.load(packages_file)

Expand All @@ -39,17 +57,23 @@ async def givekit(self, interaction: nextcord.Interaction, steamid: str = nextco
asyncio.create_task(self.rcon_util.rcon_command(server, command))
await asyncio.sleep(1)

embed = nextcord.Embed(title=f"Package Delivery - {server}", color=nextcord.Color.green())
embed = nextcord.Embed(
title=f"Package Delivery - {server}", color=nextcord.Color.green()
)
embed.description = f"Delivering {kit_name} kit to {steamid}."
await interaction.followup.send(embed=embed)

@givekit.on_autocomplete("server")
async def on_autocomplete_rcon(self, interaction: nextcord.Interaction, current: str):
async def on_autocomplete_rcon(
self, interaction: nextcord.Interaction, current: str
):
await self.autocomplete_server(interaction, current)

@givekit.on_autocomplete("kit_name")
async def on_autocomplete_packages(self, interaction: nextcord.Interaction, current: str):
packages_path = os.path.join('gamedata', 'kits.json')
async def on_autocomplete_packages(
self, interaction: nextcord.Interaction, current: str
):
packages_path = os.path.join("gamedata", "kits.json")
with open(packages_path) as packages_file:
kits = json.load(packages_file)
choices = [name for name in kits if current.lower() in name.lower()][:25]
Expand Down
Loading

0 comments on commit 457b9f9

Please sign in to comment.