Celeskry commited on
Commit
138d9aa
·
verified ·
1 Parent(s): ab5abab

Update app/invite_logic.py

Browse files
Files changed (1) hide show
  1. app/invite_logic.py +12 -5
app/invite_logic.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import discord
2
  import os
3
  import asyncio
@@ -12,15 +13,17 @@ class DiscordInviteLogic:
12
  self._expiry_time = None
13
 
14
  async def start(self):
 
15
  intents = discord.Intents.default()
16
  self.client = discord.Client(intents=intents)
17
 
18
  async def stop(self):
19
- if self.client:
 
20
  await self.client.close()
21
 
22
  async def get_invite(self):
23
- Expires = 600
24
 
25
  if self._cached_invite and self._expiry_time > datetime.now() + timedelta(seconds=30):
26
  remaining = self._expiry_time - datetime.now()
@@ -37,17 +40,21 @@ class DiscordInviteLogic:
37
  return {"error": "Missing Discord Config in Environment Secrets"}
38
 
39
  try:
 
 
 
 
40
  await self.client.login(self.token)
41
  channel = await self.client.fetch_channel(int(self.channel_id))
42
 
43
  invite = await channel.create_invite(
44
- max_age=Expires,
45
  max_uses=0,
46
  unique=True
47
  )
48
 
49
  self._cached_invite = invite.url
50
- self._expiry_time = datetime.now() + timedelta(seconds=TEN_MINUTES_IN_SECONDS)
51
 
52
  await self.client.close()
53
 
@@ -59,4 +66,4 @@ class DiscordInviteLogic:
59
  except Exception as e:
60
  if self.client and not self.client.is_closed():
61
  await self.client.close()
62
- return {"error": str(e)}
 
1
+ # app/invite_logic.py
2
  import discord
3
  import os
4
  import asyncio
 
13
  self._expiry_time = None
14
 
15
  async def start(self):
16
+ """Initialize the client with default intents."""
17
  intents = discord.Intents.default()
18
  self.client = discord.Client(intents=intents)
19
 
20
  async def stop(self):
21
+ """Clean up the client connection."""
22
+ if self.client and not self.client.is_closed():
23
  await self.client.close()
24
 
25
  async def get_invite(self):
26
+ EXPIRY_DURATION = 600
27
 
28
  if self._cached_invite and self._expiry_time > datetime.now() + timedelta(seconds=30):
29
  remaining = self._expiry_time - datetime.now()
 
40
  return {"error": "Missing Discord Config in Environment Secrets"}
41
 
42
  try:
43
+ if self.client is None or self.client.is_closed():
44
+ intents = discord.Intents.default()
45
+ self.client = discord.Client(intents=intents)
46
+
47
  await self.client.login(self.token)
48
  channel = await self.client.fetch_channel(int(self.channel_id))
49
 
50
  invite = await channel.create_invite(
51
+ max_age=EXPIRY_DURATION,
52
  max_uses=0,
53
  unique=True
54
  )
55
 
56
  self._cached_invite = invite.url
57
+ self._expiry_time = datetime.now() + timedelta(seconds=EXPIRY_DURATION)
58
 
59
  await self.client.close()
60
 
 
66
  except Exception as e:
67
  if self.client and not self.client.is_closed():
68
  await self.client.close()
69
+ return {"error": f"Connection Error: {str(e)}"}