|
1 | 1 | import asyncio
|
| 2 | +import os |
| 3 | + |
| 4 | +import psutil |
2 | 5 | from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
3 | 6 | import tzlocal
|
4 | 7 |
|
@@ -31,19 +34,22 @@ def __init__(self, version: str):
|
31 | 34 | self.db.build()
|
32 | 35 | self.cache: dict = dict()
|
33 | 36 | self.tasks = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))
|
| 37 | + self.ready = False |
34 | 38 | self.urban: UrbanDictionary = UrbanDictionary()
|
35 | 39 | self.VERSION = version
|
36 | 40 | super().__init__(
|
37 | 41 | owner_id="507214515641778187",
|
38 | 42 | shards=5000,
|
39 | 43 | intents=discord.Intents.all(),
|
| 44 | + debug_guilds=["1064582321728143421"], |
40 | 45 | description="Misc Bot used for advanced moderation and guild customization")
|
41 | 46 | self.tasks.add_job(self.db.commit, trigger='interval', minutes=30)
|
42 | 47 |
|
43 | 48 | async def on_connect(self):
|
44 | 49 | self.tasks.start()
|
45 | 50 | await self.sync_commands()
|
46 | 51 | print(f"Signed into {self.user.display_name}#{self.user.discriminator}\n")
|
| 52 | + self.ready = True |
47 | 53 |
|
48 | 54 | async def on_shard_ready(self, shard_id):
|
49 | 55 | await self.change_presence(activity=discord.Activity(type=discord.ActivityType.watching,
|
@@ -87,10 +93,11 @@ def get_name(self, data: Any, groups: list[str]):
|
87 | 93 | return self.get_name(data.get('options'), [*groups, data.get('name')])
|
88 | 94 |
|
89 | 95 | async def on_interaction(self, interaction: discord.Interaction):
|
| 96 | + if not self.ready: return |
90 | 97 | if interaction.is_command():
|
91 | 98 | name_list, options = self.get_name(interaction.data, [])
|
92 | 99 | name = " ".join(name_list)
|
93 |
| - # self.db.execute(f"""INSERT INTO commands VALUES (?,?,?,?,?)""", name, interaction.guild_id, interaction.user.id, json.dumps(options), datetime.now().timestamp()) |
| 100 | + self.db.execute(f"""INSERT INTO commands VALUES (?,?,?,?,?)""", name, interaction.guild_id, interaction.user.id, json.dumps(options), datetime.now().timestamp()) |
94 | 101 | return await super().on_interaction(interaction=interaction)
|
95 | 102 |
|
96 | 103 | async def on_application_command_error(self, ctx: CustomContext, exc) -> None:
|
@@ -126,3 +133,11 @@ async def on_application_command_error(self, ctx: CustomContext, exc) -> None:
|
126 | 133 |
|
127 | 134 | else:
|
128 | 135 | raise exc
|
| 136 | + |
| 137 | + async def cpu_percent(self, interval = None, *args, **kwargs): |
| 138 | + python_process = psutil.Process(os.getpid()) |
| 139 | + if interval is not None and interval > 0.0: |
| 140 | + python_process.cpu_percent(*args, **kwargs) |
| 141 | + await asyncio.sleep(interval) |
| 142 | + return psutil.cpu_percent(*args, **kwargs) |
| 143 | + |
0 commit comments