application-bot/applicationbot.py

210 lines
No EOL
8.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="JUST THE WORD",
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.lower() != 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 or remove a user from the whitelist",
)
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")):
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)
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)
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 int(os.getenv("ADMIN_ROLE_ID") not in user.roles:
await inter.edit_original_response(content="Only the server admins 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 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=[
disnake.ui.Button(label="Apply", style=disnake.ButtonStyle.success, custom_id="Apply"),
])
@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!")
@bot.listen("on_button_click")
async def button_listener(inter: disnake.MessageInteraction):
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())
return
ogmsg = inter.message.embeds
embed = ogmsg[0]
dmstat = ""
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:
dmstat = ", Failed to DM user"
else:
try:
await user.send("You have been Whitelisted!")
except:
dmstat = ", Failed to DM user"
status = "Approved and Whitelisted"
embed.add_field(
name="Status",
value=f"{status}{dmstat}",
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":
try:
await user.send("You have failed the application process, you may not reapply")
except:
dmstat = ", Failed to DM user"
embed.add_field(
name="Status",
value=f"Denied{dmstat}",
inline=False,
)
await inter.response.edit_message(embed=embed, components=[])
bot.run(os.getenv("TOKEN"))