Untitled

 avatar
unknown
plain_text
2 years ago
2.9 kB
11
Indexable

import os
from time import sleep, time

try:
    from ujson import loads, dumps
    import asyncio
    import aiofiles
    from nextcord import Embed, Interaction
    from nextcord.ext import commands
    import nextcord

except ImportError:
    os.system("pip install -U nextcord aiofiles asyncio ujson")
    from ujson import loads, dumps
    import asyncio
    import aiofiles
    from nextcord import Embed, Interaction
    from nextcord.ext import commands
    import nextcord

intents = nextcord.Intents.default()
intents.message_content = True
intents.members = False

bot = commands.Bot(command_prefix = "/", intents = intents)
users: dict = None

async def getusers():
    global users

    async with aiofiles.open("./users.json", "r+", encoding="utf-8", errors="ignore") as file:
        users = loads(await file.read())

async def appendUser():
    global users

    async with aiofiles.open("./users.json", "w+", encoding='utf-8', errors='ignore') as file:
        await file.seek(0)
        await file.truncate()
        await file.write(dumps(users, indent = 4, ensure_ascii=False))
        

class verificationButton(nextcord.ui.View):

    def __init__(self) -> None:
        super().__init__()

    @nextcord.ui.button(label = "Verify", style = nextcord.ButtonStyle.green, custom_id = "verify_button")
    async def onVerifyClicked(self, button = nextcord.ui.Button, interaction = Interaction):
        global users
        await interaction.response.defer()

        role = nextcord.utils.get(interaction.guild.roles, name = "Verified")

        if interaction.user.id == users.get(interaction.user.name, None) or role in interaction.user.roles:
            await interaction.followup.send("You are already verified", ephemeral = True)
            return

        try:
            users.update({interaction.user.name: interaction.user.id})
            await appendUser()
            await interaction.user.add_roles(role)
            await interaction.followup.send("Verification Success", ephemeral = True)
        except Exception as e:
            print(e)
            del users[interaction.user.name]
            await interaction.followup.send("Verification Failed", ephemeral = True)

@bot.slash_command(name = "verify", description = "Shows an embed that has a button to verify the user")
async def verify(interaction: Interaction):

    if not interaction.permissions.administrator:
        await interaction.response.send_message("This command is only or admins")
        return

    embed = Embed(
        title = "Verification",
        description = "Click the button to verify your account.",
        color = 0x00ff00)
    
    view = verificationButton()
    await interaction.send(embed = embed, view = view)

if __name__ == "__main__":
    Token = ""
    print("Running")
    asyncio.get_event_loop().run_until_complete(getusers())
    asyncio.get_event_loop().run_until_complete(bot.start(Token))