import json
import discord
from discord.ext import commands
from discord import app_commands
# Load the list of verified users from users.json file
with open("users.json", "r") as f:
verified_users = json.load(f)
intents = discord.Intents.default()
intents.members = True
class CustomBot(commands.Bot):
def __init__(self, *, intents: discord.Intents, command_prefix: str):
super().__init__(intents=intents, command_prefix=command_prefix)
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
self.tree.copy_global_to(guild=discord.Object(id=GUILD_ID))
await self.tree.sync(guild=discord.Object(id=GUILD_ID))
bot = CustomBot(intents=intents, command_prefix="!")
@bot.command()
async def sync(ctx) -> None:
cmds = await ctx.bot.tree.sync()
await ctx.send(f"Synced {len(cmds)} commands")
# Define a slash command for verification
@bot.tree.command(name="verify", description="Verify your account")
async def verify(interaction: discord.Interaction):
# Check if the user is an admin
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("Only admins can use this command.", ephemeral=True)
return
# Create an embedded message with a button to verify
embed = discord.Embed(
title="Verification",
description="Click the button to verify your account.",
color=0x00ff00,
)
verify_button = discord.ui.Button(
label="Verify",
custom_id="verify_button",
style=discord.ButtonStyle.green,
)
view = discord.ui.View()
view.add_item(verify_button)
await interaction.response.send_message(embed=embed, view=view)
# Wait for the user to click the verify button
interaction = await bot.wait_for(
"button_click", check=lambda i: i.custom_id == "verify_button"
)
# Check if the user has already been verified
user_id = str(interaction.user.id)
if user_id in verified_users["uid"]:
await interaction.response.send_message("You are already verified.", ephemeral=True)
return
# Store the user's information in the users.json file
verified_users["username"].append(str(interaction.user))
verified_users["uid"].append(user_id)
with open("users.json", "w") as f:
json.dump(verified_users, f, indent=4)
# Grant the user the "verified" role
role = discord.utils.get(interaction.guild.roles, name="verified")
await interaction.user.add_roles(role)
# Send a confirmation message to the user
await interaction.response.send_message("You have been verified!", ephemeral=True)
# Run the bot
bot.run("token")