Organizations support
This commit is contained in:
parent
8c7131defa
commit
67488e6dbe
3 changed files with 90 additions and 16 deletions
69
cogs/org.py
Normal file
69
cogs/org.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pyforgejo.api.organization import org_get, org_list_members
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import json
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from bot import Nijika
|
||||
|
||||
class Org(commands.Cog):
|
||||
|
||||
def __init__(self, bot: Nijika):
|
||||
self.bot: Nijika = bot
|
||||
|
||||
grp = app_commands.Group(name="org", description="Organization related commands.")
|
||||
|
||||
@grp.command(name="get", description="Get an organization")
|
||||
@app_commands.describe(org="Organization to get")
|
||||
async def _org_get(self, i: discord.Interaction, org: str):
|
||||
|
||||
org_get_payload = await org_get.asyncio_detailed(
|
||||
org, client=self.bot.forgejoClient)
|
||||
data = json.loads(org_get_payload.content)
|
||||
|
||||
if org_get_payload.status_code == 404:
|
||||
return await i.response.send_message(embed=discord.Embed(
|
||||
description="I could not find that organization.",
|
||||
color=self.bot.error_color
|
||||
))
|
||||
|
||||
org_members_payload = await org_list_members.asyncio_detailed(
|
||||
org, client=self.bot.forgejoClient)
|
||||
|
||||
memberCount = org_members_payload.headers["x-total-count"]
|
||||
|
||||
await i.response.send_message(embed=discord.Embed(
|
||||
title="Organization information",
|
||||
description=(
|
||||
"**ID**: {_id}\n"
|
||||
"**Name**: {name}\n"
|
||||
"**Full Name**: {fullName}\n"
|
||||
"**Email**: {email}\n"
|
||||
"**Website:** {website}\n"
|
||||
"**Location:** {location}\n"
|
||||
"**Repository admin can change access for teams**: {teamAccess}\n"
|
||||
"**Visible members count**: {memberCount}\n"
|
||||
"## Description\n"
|
||||
"{description}"
|
||||
).format(
|
||||
_id=data["id"],
|
||||
name=data["name"],
|
||||
fullName=data["full_name"] if data["full_name"] != "" else "None",
|
||||
email=data["email"] if data["email"] != "" else "None",
|
||||
website=data["website"] if data["website"] != "" else "None",
|
||||
location=data["location"] if data["location"] != "" else "None",
|
||||
teamAccess=data["repo_admin_change_team_access"],
|
||||
memberCount=memberCount,
|
||||
description=data["description"] if data["description"] != "" else "None"
|
||||
),
|
||||
color=self.bot.accent_color
|
||||
).set_thumbnail(url=data["avatar_url"]))
|
||||
|
||||
async def setup(bot: Nijika):
|
||||
await bot.add_cog(Org(bot))
|
19
cogs/user.py
19
cogs/user.py
|
@ -10,23 +10,12 @@ from discord.ext import commands
|
|||
from datetime import datetime
|
||||
import json
|
||||
from typing import TYPE_CHECKING
|
||||
from utils import check_for_noreply, build_instance_url
|
||||
from utils import (
|
||||
SingularLinkButton,
|
||||
check_for_noreply, build_instance_url)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from bot import Nijika
|
||||
|
||||
class SingularLinkButton(ui.View):
|
||||
|
||||
def __init__(self, *, label: str, url: str,
|
||||
style: discord.ButtonStyle = discord.ButtonStyle.gray):
|
||||
super().__init__()
|
||||
|
||||
self.add_item(ui.Button(
|
||||
style=style,
|
||||
label=label,
|
||||
url=url
|
||||
))
|
||||
|
||||
class User(commands.Cog):
|
||||
|
||||
def __init__(self, bot: Nijika):
|
||||
|
@ -48,6 +37,7 @@ class User(commands.Cog):
|
|||
return await i.response.send_message(embed=discord.Embed(
|
||||
description=(
|
||||
"# Basic information\n"
|
||||
"**User ID:** {userId}\n"
|
||||
"**Username:** {username}\n"
|
||||
"**Full Name:** {fullName}\n"
|
||||
"**Location:** {location}\n"
|
||||
|
@ -60,6 +50,7 @@ class User(commands.Cog):
|
|||
"**Following:** {following}\n"
|
||||
"**Starred Repositories:** {starredRepos}"
|
||||
).format(
|
||||
userId=data["id"],
|
||||
username=data["username"],
|
||||
fullName=data["full_name"] if data["full_name"] != "" else "None",
|
||||
location=data["location"] if data["location"] != "" else "None",
|
||||
|
|
18
utils.py
18
utils.py
|
@ -1,9 +1,25 @@
|
|||
import json
|
||||
from functools import cache
|
||||
|
||||
import discord
|
||||
from discord import ui
|
||||
|
||||
config = json.load(open("config.json"))
|
||||
|
||||
|
||||
class SingularLinkButton(ui.View):
|
||||
|
||||
def __init__(self, *, label: str, url: str,
|
||||
style: discord.ButtonStyle = discord.ButtonStyle.gray):
|
||||
super().__init__()
|
||||
|
||||
self.add_item(ui.Button(
|
||||
style=style,
|
||||
label=label,
|
||||
url=url
|
||||
))
|
||||
|
||||
|
||||
@cache
|
||||
def get_domain() -> str:
|
||||
"""Returns the domain"""
|
||||
|
@ -11,7 +27,6 @@ def get_domain() -> str:
|
|||
return str(config["instanceApiUrl"]).split("/")[2]
|
||||
|
||||
|
||||
@cache
|
||||
def build_instance_url(x) -> str:
|
||||
"""Simply prepends the instance url
|
||||
(example: https://codeberg.org) with x"""
|
||||
|
@ -20,7 +35,6 @@ def build_instance_url(x) -> str:
|
|||
return proto + "//" + get_domain() + "/" + x
|
||||
|
||||
|
||||
@cache
|
||||
def check_for_noreply(email: str) -> bool:
|
||||
"""
|
||||
Checks if the email is a "no reply" email.
|
||||
|
|
Loading…
Reference in a new issue