application-bot/applicationbot.py
2023-11-24 22:46:45 -05:00

166 lines
No EOL
6.8 KiB
Python

import disnake
from disnake.ext import commands
from disnake import TextInputStyle
from dotenv import load_dotenv
from rcon.source import Client
import os
load_dotenv()
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",
placeholder="Three Sentence Minimum Please!",
custom_id="Tell us a bit about yourself",
style=TextInputStyle.paragraph,
),
disnake.ui.TextInput(
label="The secret phrase",
placeholder="",
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:
if key == "The secret phrase" and value != os.getenv("PHRASE"):
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",
value=f"<@{inter.user.id}>",
inline=False,
)
embed.add_field(
name="User ID",
value=inter.user.id,
inline=False,
)
for key, value in application:
embed.add_field(
name=key,
value=value,
inline=False,
)
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)
if applicationdenied:
await inter.response.send_message("Your Application has been denied, you may not reapply", ephemeral=True)
await bot.get_channel(int(os.getenv("LOG_CHANNEL"))).send(embed=embed)
else:
await inter.response.send_message("Your Application has been successfully submitted, you will receive your response eventually", ephemeral=True)
await bot.get_channel(int(os.getenv("LOG_CHANNEL"))).send(embed=embed, components=[
disnake.ui.Button(label="Approve", style=disnake.ButtonStyle.success, custom_id="Approve"),
disnake.ui.Button(label="Deny", style=disnake.ButtonStyle.danger, custom_id="Deny"),
],)
bot = commands.Bot(command_prefix=".", test_guilds=[int(os.getenv("GUILD_ID"))])
@bot.slash_command(
name="whitelist",
description="Forceibly add a user to the whitelist",
)
async def whitelist(inter: disnake.AppCmdInter, username:str, user:disnake.Member):
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', 'add', username)
guild = bot.get_guild(int(os.getenv("GUILD_ID")))
role = guild.get_role(int(os.getenv("WHITELISTED_ROLE_ID")))
await user.add_roles(role)
await inter.edit_original_response(content=f"Respose:\n```{response}```")
@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",
)
async def apply(inter: disnake.AppCmdInter):
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 message {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.listen("on_button_click")
async def button_listener(inter: disnake.MessageInteraction):
if inter.component.custom_id == "Apply":
await inter.response.send_modal(modal=ApplicationModal())
return
ogmsg = inter.message.embeds
embed = ogmsg[0]
user = await bot.fetch_user(int(embed.fields[1].value))
if inter.component.custom_id == "Approve":
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:
pass
else:
try:
await user.send("You have been Whitelisted!")
except:
pass
status = "Aprroved and Whitelisted"
embed.add_field(
name="Status",
value=status,
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=[])
elif inter.component.custom_id == "Deny":
embed.add_field(
name="Status",
value="Denied",
inline=False,
)
try:
await user.send("You have failed the application process, you may not reapply")
except:
pass
await inter.response.edit_message(embed=embed, components=[])
bot.run(os.getenv("TOKEN"))