aboutsummaryrefslogtreecommitdiff
path: root/code/cogs/newaccount.py
blob: 1fca84ceb14ad70e62bf2f5940d3479886e9c141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import discord
from discord import app_commands
from discord.ext import commands

from utils.database import Session
from utils.jellyfin_create import create_jellyfin_account
from utils.models import JellyfinAccounts
from utils.config import (
    JELLYFIN_PUBLIC_URL,
    JELLYFIN_ENABLED,
    ACCOUNT_TIME,
)


class NewAccount(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @app_commands.command()
    @app_commands.check(lambda inter: JELLYFIN_ENABLED)
    async def newaccount(self, interaction: discord.Interaction) -> None:
        """Create a new temporary Jellyfin account"""
        # Defer in case it takes too long
        await interaction.response.defer(ephemeral=True)
        # Make sure the user doesn't already have an account
        with Session() as session:
            account = (
                session.query(JellyfinAccounts)
                .filter(JellyfinAccounts.user_id == interaction.user.id)
                .first()
            )
        # Account already allocated
        if account:
            embed = discord.Embed(
                title="Account Already Exists",
                description=(
                    "Look at your previous DMs with me to find your account"
                    " information. You will be permitted to create a new"
                    " account after your current one expires."
                ),
                color=0xD01B86,
            )
            return await interaction.followup.send(embed=embed)

        # Create a new Jellyfin account for the user
        response = create_jellyfin_account(interaction.user.id)
        if response:
            embed = discord.Embed(
                title="Account Created",
                description=(
                    "Your account has been successfully created. Check your"
                    " DMs for your account information."
                ),
                color=0xD01B86,
            )
            await interaction.followup.send(embed=embed)

            # Send the user their account information
            embed = discord.Embed(
                title="Jellyfin Account Information",
                description=(
                    # fmt: off
                    "Here is your temporary account information.\n\n"
                    f"**Server URL:** `{JELLYFIN_PUBLIC_URL}`\n"
                    f"**Username:** `{response[0]}`\n"
                    f"**Password:** `{response[1]}`\n\n"
                    "Your account will be automatically deleted in"
                    f" {ACCOUNT_TIME} hours."
                    # fmt: on
                ),
                color=0xD01B86,
            )
            await interaction.user.send(embed=embed)
        # If account not created for some reason
        else:
            embed = discord.Embed(
                title="Unknown Error Occured",
                description=(
                    "Error creating Jellyfin account. Please try again. If the"
                    " error persists, contact an administrator."
                ),
                color=0xD01B86,
            )
            return await interaction.followup.send(embed=embed)


async def setup(bot):
    await bot.add_cog(NewAccount(bot))