application-bot/applicationbot.py

210 lines
8.8 KiB
Python
Raw Normal View History

2023-11-23 16:29:56 +00:00
import disnake
from disnake.ext import commands
from disnake import TextInputStyle
2023-11-25 01:26:27 +00:00
from dotenv import load_dotenv
2023-11-25 03:46:45 +00:00
from rcon.source import Client
2023-11-25 01:26:27 +00:00
import os
load_dotenv()
2023-11-23 16:29:56 +00:00
class ApplicationModal(disnake.ui.Modal):
def __init__(self):
# The details of the modal, and its components
components = [
disnake.ui.TextInput(
label="Username (Minecraft)",
placeholder="DraconicDragon3",
custom_id="Username",
style=TextInputStyle.short,
max_length=16,
),
disnake.ui.TextInput(
label="Tell us a bit about yourself",
2023-11-25 01:26:27 +00:00
placeholder="Three Sentence Minimum Please!",
2023-11-23 16:29:56 +00:00
custom_id="Tell us a bit about yourself",
style=TextInputStyle.paragraph,
),
disnake.ui.TextInput(
label="The secret phrase",
2023-11-25 03:48:16 +00:00
placeholder="JUST THE WORD",
2023-11-23 16:29:56 +00:00
custom_id="The secret phrase",
style=TextInputStyle.short,
max_length=32,
),
]
super().__init__(title="Apply to join the server", components=components)
# The callback received when the user input is completed.
async def callback(self, inter: disnake.ModalInteraction):
embed_color = 0x00ff00
embed_title = "New Application"
applicationdenied = False
application = inter.text_values.items()
for key, value in application:
2024-05-14 20:49:36 +00:00
if key == "The secret phrase" and value.lower() != os.getenv("PHRASE"):
2023-11-23 16:29:56 +00:00
applicationdenied = True
embed_color = disnake.Colour.red()
embed_title = "Auto-Denied Application"
embed = disnake.Embed(title=embed_title, color=embed_color)
embed.add_field(
name="User",
2023-11-25 03:46:45 +00:00
value=f"<@{inter.user.id}>",
inline=False,
)
embed.add_field(
name="User ID",
value=inter.user.id,
2023-11-23 16:29:56 +00:00
inline=False,
)
for key, value in application:
embed.add_field(
name=key,
value=value,
inline=False,
)
2023-11-25 03:46:45 +00:00
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
role = guild.get_role(int(os.getenv("APPLICANT_ROLE_ID")))
member = await guild.fetch_member(inter.user.id)
await member.add_roles(role)
2023-11-23 16:29:56 +00:00
if applicationdenied:
await inter.response.send_message("Your Application has been denied, you may not reapply", ephemeral=True)
2023-11-25 01:26:27 +00:00
await bot.get_channel(int(os.getenv("LOG_CHANNEL"))).send(embed=embed)
2023-11-23 16:29:56 +00:00
else:
await inter.response.send_message("Your Application has been successfully submitted, you will receive your response eventually", ephemeral=True)
2023-11-25 01:26:27 +00:00
await bot.get_channel(int(os.getenv("LOG_CHANNEL"))).send(embed=embed, components=[
2023-11-23 16:29:56 +00:00
disnake.ui.Button(label="Approve", style=disnake.ButtonStyle.success, custom_id="Approve"),
disnake.ui.Button(label="Deny", style=disnake.ButtonStyle.danger, custom_id="Deny"),
],)
2023-11-25 01:26:27 +00:00
bot = commands.Bot(command_prefix=".", test_guilds=[int(os.getenv("GUILD_ID"))])
2023-11-23 16:29:56 +00:00
2023-11-25 03:46:45 +00:00
@bot.slash_command(
name="whitelist",
description="Forceibly add or remove a user from the whitelist",
2023-11-25 03:46:45 +00:00
)
async def whitelist(
inter: disnake.AppCmdInter,
action: str = commands.Param(choices=["add", "remove"]),
username: str = commands.Param(name="minecraft-username"),
user: disnake.Member = commands.Param(name="discord-user")):
2023-11-25 03:46:45 +00:00
await inter.response.defer()
with Client(os.getenv("RCON_IP"), int(os.getenv("RCON_PORT")), passwd=os.getenv("RCON_PASSWORD")) as client:
response = client.run('whitelist', action, username)
2023-11-25 03:46:45 +00:00
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
role = guild.get_role(int(os.getenv("WHITELISTED_ROLE_ID")))
if action == "add":
await user.add_roles(role)
else:
await user.remove_roles(role)
2023-11-25 03:46:45 +00:00
await inter.edit_original_response(content=f"Respose:\n```{response}```")
2023-11-23 16:29:56 +00:00
2023-11-25 03:46:45 +00:00
@bot.slash_command(
name="execute",
description="Execute a command on the server",
)
async def execute(inter: disnake.AppCmdInter, command:str):
await inter.response.defer()
if inter.user.id != int(os.getenv("ADMIN_ID")):
await inter.edit_original_response(content="Only the server admin may run this command")
return
with Client(os.getenv("RCON_IP"), int(os.getenv("RCON_PORT")), passwd=os.getenv("RCON_PASSWORD")) as client:
response = client.run(command)
if response == "":
response = "No Response"
await inter.edit_original_response(content=f"Respose:\n```{response}```")
@bot.slash_command(
name="apply",
description="Apply to join the server",
)
2023-11-23 16:29:56 +00:00
async def apply(inter: disnake.AppCmdInter):
2023-11-25 03:46:45 +00:00
rules = os.getenv("RULES_LINK")
await inter.response.send_message(f"Ok! thank you for your interest, before we begin lets go over the rules by looking at this site {rules}\nwhen you are finished come back and press the button", ephemeral=True, components=[
disnake.ui.Button(label="Apply", style=disnake.ButtonStyle.success, custom_id="Apply"),
])
@bot.slash_command(
name="publicapply",
description="Creates a button to open the application menu",
)
async def apply(inter: disnake.AppCmdInter):
await inter.response.send_message(components=[
2023-11-25 03:46:45 +00:00
disnake.ui.Button(label="Apply", style=disnake.ButtonStyle.success, custom_id="Apply"),
])
2023-11-23 16:29:56 +00:00
2024-05-14 20:49:36 +00:00
@bot.slash_command(
name="reapply",
description="Allow a user to reapply to the server"
)
async def reapply(inter: disnake.AppCmdInter, reason:str, user:disnake.Member):
await inter.response.defer()
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
role = guild.get_role(int(os.getenv("APPLICANT_ROLE_ID")))
dmuser = await bot.fetch_user(int(user.id))
if role in user.roles:
await user.remove_roles(role)
try:
await dmuser.send(f"you have been given a second chance to apply, because {reason}")
await inter.response.edit_original_response("User Notified and application reopened!")
except:
await inter.response.edit_original_response("Failed to notify user, application reopened")
else:
await inter.response.edit_original_response("That user hasn't applied yet!")
2023-11-23 16:29:56 +00:00
@bot.listen("on_button_click")
async def button_listener(inter: disnake.MessageInteraction):
2023-11-25 03:46:45 +00:00
if inter.component.custom_id == "Apply":
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
approle = guild.get_role(int(os.getenv("APPLICANT_ROLE_ID")))
whitelistedrole = guild.get_role(int(os.getenv("WHITELISTED_ROLE_ID")))
member = await guild.fetch_member(inter.user.id)
if any(r in member.roles for r in (approle, whitelistedrole)):
await inter.response.send_message("You have already applied, you many not apply again!", ephemeral=True)
else:
await inter.response.send_modal(modal=ApplicationModal())
2023-11-25 03:46:45 +00:00
return
ogmsg = inter.message.embeds
embed = ogmsg[0]
dmstat = ""
2023-11-25 03:46:45 +00:00
user = await bot.fetch_user(int(embed.fields[1].value))
2023-11-23 16:29:56 +00:00
if inter.component.custom_id == "Approve":
2023-11-25 03:46:45 +00:00
with Client(os.getenv("RCON_IP"), int(os.getenv("RCON_PORT")), passwd=os.getenv("RCON_PASSWORD")) as client:
response = client.run('whitelist', 'add', embed.fields[2].value)
print(response)
if response == "That player does not exist":
status = "Error: Player Doesn't Exist"
try:
await user.send("You have been Whitelisted, however your username is wrong, please ping a member of the whitelist team with the correct username")
except:
dmstat = ", Failed to DM user"
2023-11-25 03:46:45 +00:00
else:
try:
await user.send("You have been Whitelisted!")
except:
dmstat = ", Failed to DM user"
2024-05-14 20:49:36 +00:00
status = "Approved and Whitelisted"
2023-11-25 03:46:45 +00:00
embed.add_field(
name="Status",
value=f"{status}{dmstat}",
2023-11-25 03:46:45 +00:00
inline=False,
)
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
role = guild.get_role(int(os.getenv("WHITELISTED_ROLE_ID")))
member = await guild.fetch_member(int(embed.fields[1].value))
await member.add_roles(role)
await inter.response.edit_message(embed=embed, components=[])
2023-11-25 01:26:27 +00:00
elif inter.component.custom_id == "Deny":
try:
await user.send("You have failed the application process, you may not reapply")
except:
dmstat = ", Failed to DM user"
2023-11-25 03:46:45 +00:00
embed.add_field(
name="Status",
value=f"Denied{dmstat}",
2023-11-25 03:46:45 +00:00
inline=False,
)
await inter.response.edit_message(embed=embed, components=[])
2023-11-23 16:29:56 +00:00
2023-11-25 01:26:27 +00:00
bot.run(os.getenv("TOKEN"))