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 " ,
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 :
2023-11-25 01:26:27 +00:00
if key == " The secret phrase " and value != 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 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 } ``` " )
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 message { rules } \n when you are finished come back and press the button " , ephemeral = True , components = [
disnake . ui . Button ( label = " Apply " , style = disnake . ButtonStyle . success , custom_id = " Apply " ) ,
] )
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 " :
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 ) )
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 :
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 = [ ] )
2023-11-25 01:26:27 +00:00
elif inter . component . custom_id == " Deny " :
2023-11-25 03:46:45 +00:00
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 = [ ] )
2023-11-23 16:29:56 +00:00
2023-11-25 01:26:27 +00:00
bot . run ( os . getenv ( " TOKEN " ) )