From 86f780f4afdccc2a4640db5460c4560e2ae112b5 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 9 Oct 2025 17:30:28 +1100 Subject: [PATCH] feat: Cleanup code and command perms --- bot.py | 100 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 44 deletions(-) diff --git a/bot.py b/bot.py index decb07f..be1fd28 100644 --- a/bot.py +++ b/bot.py @@ -36,50 +36,10 @@ if LOCAL: faucet_roles = 'faucet.json' verified_roles = 'roles.json' -# Commands -@tree.command(name="faucet", description="Get a free domain") -async def faucet(ctx, email:str): - # Check if a DM - if ctx.guild is None: - await ctx.response.send_message("You can not claim from the faucet in DMs") - return - - - roles = {} - if os.path.exists(faucet_roles): - with open(faucet_roles, 'r') as f: - roles = json.load(f) - if str(ctx.guild.id) in roles: - if roles[str(ctx.guild.id)] in [role.id for role in ctx.user.roles]: - await ctx.response.send_message("The faucet will gift you a domain when someone approves your request",ephemeral=True) - message = await ctx.channel.send(f"Faucet request from {ctx.user.name} (<@{ctx.user.id}>)\n\nThis is a gift from the faucet. You will receive a domain when someone approves your request.\n\nPlease approve this gift by reacting to this message with a 👍") - faucet_messages.append({ - "id": message.id, - "email": email, - "user": ctx.user.id, - "time": datetime.datetime.now() - }) - print(faucet_messages) - await message.add_reaction("👍") - return - await ctx.response.send_message("You can't claim from the faucet",ephemeral=True) - -@tree.command(name="setfaucetrole", description="Change the role that can use the faucet") -async def faucetrole(ctx,role:discord.Role): - if ctx.user.id != ADMINID: - await ctx.response.send_message("You don't have permission to do that",ephemeral=True) - return - await ctx.response.send_message("Faucet role set to " + role.name + " for server " + ctx.guild.name,ephemeral=True) - roles = {} - if os.path.exists(faucet_roles): - with open(faucet_roles, 'r') as f: - roles = json.load(f) - - roles[str(ctx.guild.id)] = role.id - with open(faucet_roles, 'w') as f: - json.dump(roles, f) +# region Admin Commands @tree.command(name="setverifiedrole", description="Set the role that verified users get") +@app_commands.default_permissions(administrator=True) async def setverifiedrole(ctx,role:discord.Role): # Check user has manage guild permission if not ctx.user.guild_permissions.manage_guild: @@ -106,7 +66,26 @@ async def setverifiedrole(ctx,role:discord.Role): json.dump(roles, f) await ctx.response.send_message("Verified role set to " + role.name + " for server " + ctx.guild.name,ephemeral=True) + +@tree.command(name="setfaucetrole", description="Change the role that can use the faucet") +@app_commands.default_permissions(administrator=True) +async def faucetrole(ctx,role:discord.Role): + if ctx.user.id != ADMINID: + await ctx.response.send_message("You don't have permission to do that",ephemeral=True) + return + await ctx.response.send_message("Faucet role set to " + role.name + " for server " + ctx.guild.name,ephemeral=True) + roles = {} + if os.path.exists(faucet_roles): + with open(faucet_roles, 'r') as f: + roles = json.load(f) + roles[str(ctx.guild.id)] = role.id + with open(faucet_roles, 'w') as f: + json.dump(roles, f) + +# endregion + +# region User Commands @tree.command(name="verify", description="Verifies your ownership of a Handshake name and sets your nickname.") async def verify(ctx:discord.Interaction, domain:str): # Verify not a DM @@ -165,8 +144,36 @@ async def verify(ctx:discord.Interaction, domain:str): await ctx.followup.send(message,ephemeral=True) +@tree.command(name="faucet", description="Get a free domain") +async def faucet(ctx, email:str): + # Check if a DM + if ctx.guild is None: + await ctx.response.send_message("You can not claim from the faucet in DMs") + return -# When the bot is ready + + roles = {} + if os.path.exists(faucet_roles): + with open(faucet_roles, 'r') as f: + roles = json.load(f) + if str(ctx.guild.id) in roles: + if roles[str(ctx.guild.id)] in [role.id for role in ctx.user.roles]: + await ctx.response.send_message("The faucet will gift you a domain when someone approves your request",ephemeral=True) + message = await ctx.channel.send(f"Faucet request from {ctx.user.name} (<@{ctx.user.id}>)\n\nThis is a gift from the faucet. You will receive a domain when someone approves your request.\n\nPlease approve this gift by reacting to this message with a 👍") + faucet_messages.append({ + "id": message.id, + "email": email, + "user": ctx.user.id, + "time": datetime.datetime.now() + }) + print(faucet_messages) + await message.add_reaction("👍") + return + await ctx.response.send_message("You can't claim from the faucet",ephemeral=True) + +# endregion + +# region Bot Events @client.event async def on_ready(): global ADMINID @@ -189,7 +196,10 @@ async def on_message(message): if message.author == client.user: return if not message.guild: - await message.channel.send('Invite this bot into your server by using this link:\nhttps://discord.com/api/oauth2/authorize?client_id=1073940877984153692&permissions=402653184&scope=bot') + await message.channel.send('Invite this bot into your server by using this link:') + # Generate a link to add the bot to a server + await message.channel.send(f"https://discord.com/api/oauth2/authorize?client_id={client.application_id}&permissions=402653184&scope=bot") + # On reaction @client.event @@ -241,6 +251,8 @@ async def on_reaction_add(reaction, user): await reaction.message.edit(content="Approved by " + user.name) return + +# endregion if __name__ == "__main__": if not TOKEN: