-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
110 lines (88 loc) · 2.83 KB
/
utils.py
File metadata and controls
110 lines (88 loc) · 2.83 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Utility functions for the bot.
Author: Kieran Gordon <kieran.gordon28@gmail.com>
"""
from __future__ import annotations
import asyncio
from pathlib import Path
import discord
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from config import ApplicationConfig
from models import Guilds
engine = create_async_engine(ApplicationConfig().sqlalchemy_database_uri)
Session = sessionmaker(bind=engine, class_=AsyncSession)
async def get_prefix(message: discord.Message) -> str:
"""Get the prefix for the guild.
Args:
----
message (discord.Message): The message object.
Returns:
-------
str: The prefix for the guild.
"""
if not message.guild:
return "./"
guild_id = message.guild.id
async with Session() as session:
result = await session.execute(
select(Guilds.prefix).filter(Guilds.guild_id == guild_id),
)
prefix = result.scalars().first()
if prefix:
return prefix
return "./"
async def get_git_commit_hash() -> str:
"""Get the current git commit hash.
Returns
-------
str: The current git commit hash.
"""
process = await asyncio.create_subprocess_exec(
"/usr/bin/git",
"rev-parse",
"--short",
"HEAD",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, _ = await process.communicate()
return stdout.decode().strip()
async def generate_commands_embed(
ctx: discord.Message,
title: str,
command_name: str,
description: str,
footer: str | None = None,
) -> discord.Embed:
"""Generate an embed with a list of commands for a cog.
Args:
----
ctx (commands.Context): The context of the command.
title (str): The title of the embed.
command_name (str): The name of the command.
description (str): The description of the embed.
footer (str | None): The footer text for the embed. Defaults to None.
Returns:
-------
discord.Embed: The generated embed.
"""
embed = discord.Embed(
title=title,
description=description,
color=discord.Color.blue(),
)
for command in sorted(ctx.command.commands, key=lambda x: x.name):
embed.add_field(
name=command.name,
value=f"`{await get_prefix(ctx.message)}{command_name} {command.name}` - {command.brief}",
inline=False,
)
avatar = ctx.author.avatar.url if ctx.author.avatar else None
embed.set_author(name=ctx.author.name, icon_url=avatar)
if footer:
embed.set_footer(text=footer)
return embed
async def is_running_in_docker() -> bool:
"""Check if the bot is running in Docker."""
return Path("/.dockerenv").exists()