This commit is contained in:
kita 2024-04-23 16:49:25 +05:00
parent 67488e6dbe
commit fc6f0624e6
Signed by: kita
GPG key ID: 1E3B374E5F392590
4 changed files with 133 additions and 7 deletions

8
bot.py
View file

@ -6,7 +6,7 @@ from discord.ext import commands
import json
import logging
import os
from typing import TypedDict, Optional
from typing import TypedDict
class BotConfiguration(TypedDict):
@ -15,11 +15,13 @@ class BotConfiguration(TypedDict):
forgejoApiToken: str
noreplyEmail: str
log = logging.getLogger("nijika")
cogs = ("cogs.core", "cogs.user")
cogs = ("cogs.core", "cogs.user", "cogs.org", "cogs.repo")
class Nijika(commands.Bot):
"""Nijika my beloved <3"""
def __init__(self):
@ -27,6 +29,7 @@ class Nijika(commands.Bot):
self.forgejoClient: AuthenticatedClient = AuthenticatedClient(
base_url=self.configuration["instanceApiUrl"],
raise_on_unexpected_status=True,
follow_redirects=True,
token=self.configuration["forgejoApiToken"])
self.accent_color = 0xecd97a
@ -71,6 +74,7 @@ class Nijika(commands.Bot):
embed_links=True,
attach_files=True
)))
async def start(self):
"""Start up Codeijika"""
await super().start(self.configuration["token"])

View file

@ -20,8 +20,6 @@ class Core(commands.Cog):
async def interaction_check(self, i: discord.Interaction):
print(i.user.id, self.bot.owner_id)
if not await self.bot.is_owner(i.user):
await i.response.send_message(embed=discord.Embed(
title="Permission denied",

100
cogs/repo.py Normal file
View file

@ -0,0 +1,100 @@
from __future__ import annotations
from pyforgejo.api.repository import repo_get
import discord
from discord import app_commands
from discord.ext import commands
import utils
import json
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bot import Nijika
class Repo(commands.Cog):
def __init__(self, bot: Nijika):
self.bot: Nijika = bot
grp = app_commands.Group(
name="repo",
description="Repository related commands"
)
@grp.command(name="get", description="Get a repository")
@app_commands.describe(
target=("Target repository in <user>/<repo> format. "
"Example: kita/codeijika"))
async def repoGet(self, i: discord.Interaction, target: str):
targetFmt = target.split("/")
if not len(targetFmt) == 2:
return await i.response.send_message(embed=discord.Embed(
title="Invalid format",
color=self.bot.error_color
))
res = await repo_get.asyncio_detailed(
targetFmt[0],
targetFmt[1],
client=self.bot.forgejoClient
)
if res.status_code == 404:
return await i.response.send_message(embed=discord.Embed(
title="Error",
description=("Could not find repository. "
"Please make sure the repository exists and is not "
"private.")
))
data = json.loads(res.content)
if data["empty"]:
return await i.response.send_message(
"The repository you specified does not contain any content.")
embed = discord.Embed(
title=data["full_name"],
url=data["html_url"],
color=self.bot.accent_color
)
if data["avatar_url"] != "":
embed.set_thumbnail(url=data["avatar_url"])
embed.set_author(
name=data["owner"]["username"],
url=utils.build_instance_url(
data["owner"]["username"]
),
icon_url=data["owner"]["avatar_url"]
)
if data["language"] != "":
embed.add_field(
name="Programming Language", value=data["language"],
inline=False)
embed.add_field(
name="Stars", value=data["stars_count"]) \
.add_field(
name="Forks", value=data["forks_count"]) \
.add_field(
name="Watchers", value=data["watchers_count"])
if data["has_issues"]:
embed.add_field(
name="Issues", value=data["open_issues_count"])
if data["has_pull_requests"]:
embed.add_field(
name="PRs", value=data["open_pr_counter"])
return await i.response.send_message(embed=embed)
async def setup(bot: Nijika):
await bot.add_cog(Repo(bot))

View file

@ -3,7 +3,6 @@ from __future__ import annotations
from pyforgejo.api.user import user_get
import discord
from discord import ui
from discord import app_commands
from discord.ext import commands
@ -42,7 +41,6 @@ class User(commands.Cog):
"**Full Name:** {fullName}\n"
"**Location:** {location}\n"
"**Email:** {email}\n"
"**Is Administrator:** {isAdmin}\n"
"\n"
"# Statistics\n"
"**Join Date:** {joinedAt}\n"
@ -55,7 +53,6 @@ class User(commands.Cog):
fullName=data["full_name"] if data["full_name"] != "" else "None",
location=data["location"] if data["location"] != "" else "None",
email=data["email"] if not check_for_noreply(data["email"]) else "Hidden",
isAdmin=data["is_admin"],
joinedAt=datetime.fromisoformat(data["created"]),
followers=data["followers_count"],
following=data["following_count"],
@ -68,7 +65,34 @@ class User(commands.Cog):
url=build_instance_url(data["username"])
))
@userGroup.command(
name="avatar",
description="Get a user/organization's avatar")
@app_commands.describe(
target="Target user/organization"
)
async def userAvatar(self, i: discord.Interaction, target: str):
res = await user_get.asyncio_detailed(
target,
client=self.bot.forgejoClient)
if res.status_code == 404:
return i.response.send_message(
title="I could not find the user/organization.",
color=self.bot.error_color
)
data = json.loads(res.content)
return await i.response.send_message(
embed=discord.Embed(
title="{}'s avatar".format(
data["full_name"] if data["full_name"] != ""
else data["username"]
),
color=self.bot.accent_color
).set_image(url=data["avatar_url"]))
async def setup(bot: Nijika):
await bot.add_cog(User(bot))