Skip to content

Commit dba74eb

Browse files
committed
chore: merge staging onto master to resync branches
2 parents ac6b25c + 3345624 commit dba74eb

33 files changed

+1075
-1042
lines changed

chiya/__init__.py

-54
This file was deleted.

chiya/bot.py

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import asyncio
22
import glob
3-
import logging
43
import os
4+
import sys
5+
import logging
56

67
import discord
78
from discord.ext import commands
9+
from loguru import logger as log
810

9-
import __init__ # noqa
1011
import database
11-
from config import config
12+
from chiya.config import config
1213

1314

1415
bot = commands.Bot(
@@ -25,7 +26,6 @@
2526
reactions=config["bot"]["intents"]["reactions"],
2627
),
2728
)
28-
log = logging.getLogger(__name__)
2929

3030

3131
@bot.event
@@ -38,9 +38,42 @@ async def on_ready() -> None:
3838

3939

4040
async def main():
41+
await setup_logger()
42+
await load_cogs()
43+
await bot.start(config["bot"]["token"])
44+
45+
46+
async def setup_logger():
47+
log_level = config["bot"]["log_level"]
48+
if not log_level:
49+
log_level = "NOTSET"
50+
log.remove()
51+
52+
class InterceptHandler(logging.Handler):
53+
"""
54+
Setup up an Interceptor class to redirect all logs from the standard logging library to loguru.
55+
"""
56+
def emit(self, record: logging.LogRecord) -> None:
57+
# Get corresponding Loguru level if it exists.
58+
level: str | int
59+
try:
60+
level = log.level(record.levelname).name
61+
except ValueError:
62+
level = record.levelno
63+
64+
log.opt(exception=record.exc_info).log(level, record.getMessage())
65+
66+
discord.utils.setup_logging(handler=InterceptHandler(),level=logging.getLevelName(config["bot"]["log_level"]), root=False)
67+
68+
fmt = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan> | <level>{message}</level>"
69+
log.add(sys.stdout, format=fmt, level=log_level, backtrace=False)
70+
log.add(os.path.join("logs", "bot.log"), format=fmt, rotation="1 day")
71+
72+
73+
async def load_cogs():
4174
for cog in glob.iglob(os.path.join("cogs", "**", "[!^_]*.py"), root_dir="chiya", recursive=True):
4275
await bot.load_extension(cog.replace("/", ".").replace("\\", ".").replace(".py", ""))
43-
await bot.start(config["bot"]["token"])
76+
4477

4578
if __name__ == "__main__":
4679
database.Database().setup()

chiya/cogs/apps/move_question.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from subprocess import call
2+
3+
import aiohttp
4+
import discord
5+
from discord import app_commands, Webhook
6+
from discord.ext import commands
7+
from loguru import logger as log
8+
9+
from chiya.config import config
10+
from chiya.utils import embeds
11+
12+
13+
14+
class MoveQuestionApp(commands.Cog):
15+
def __init__(self, bot) -> None:
16+
self.bot = bot
17+
self.move_question_command = app_commands.ContextMenu(name="Move Question", callback=self.move_question)
18+
self.bot.tree.add_command(self.move_question_command)
19+
20+
@app_commands.guilds(config["guild_id"])
21+
@app_commands.guild_only()
22+
async def move_question(self, ctx: discord.Interaction, message: discord.Message) -> None:
23+
"""
24+
Staff only context menu command for moving questions to the appropriate channel.
25+
"""
26+
await ctx.response.defer(thinking=True, ephemeral=True)
27+
28+
staff = [x for x in ctx.user.roles if x.id == config["roles"]["staff"] or x.id == config["roles"]["trial"]]
29+
if not staff:
30+
return await embeds.error_message(ctx=ctx, description="You do not have permissions to use this command.")
31+
32+
if ctx.channel.category_id in [
33+
config["categories"]["moderation"],
34+
config["categories"]["development"],
35+
config["categories"]["logs"],
36+
config["categories"]["tickets"],
37+
]:
38+
return await embeds.error_message(
39+
ctx=ctx,
40+
description="You do not have permissions to use this command in this category.",
41+
)
42+
43+
channel = discord.utils.get(
44+
ctx.guild.text_channels,
45+
id=config["channels"]["public"]["questions_and_help"],
46+
)
47+
48+
async with aiohttp.ClientSession() as session:
49+
webhook = Webhook.from_url(
50+
url=config["bot"]["webhook_url"],
51+
session=session,
52+
)
53+
54+
content = f"{message.content}\n\n"
55+
for attachment in message.attachments:
56+
content += f"{attachment.url}\n"
57+
58+
await webhook.send(
59+
content=content,
60+
username=message.author.name,
61+
avatar_url=message.author.display_avatar.url,
62+
)
63+
64+
success_embed = embeds.make_embed(
65+
description=f"Successfully moved message to: {channel.mention}",
66+
color=discord.Color.green(),
67+
)
68+
await ctx.followup.send(embed=success_embed)
69+
70+
await embeds.warning_message(
71+
ctx=ctx,
72+
title="Warning: Your question was moved",
73+
description=(
74+
f"{message.author.mention}, your message was moved to {channel.mention} "
75+
"which is the more appropriate channel for help, questions, and support type "
76+
"topics. Please continue your conversation in that channel."
77+
),
78+
)
79+
ping = await channel.send(message.author.mention)
80+
await ping.delete()
81+
await message.delete()
82+
83+
84+
async def setup(bot: commands.Bot) -> None:
85+
await bot.add_cog(MoveQuestionApp(bot))
86+
log.info("App loaded: move_question")

chiya/cogs/apps/report_message.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
import asyncio
2-
import logging
32

43
import discord
54
from discord import app_commands
65
from discord.ext import commands
6+
from loguru import logger as log
77

8-
from chiya import config
8+
from chiya.config import config
99
from chiya.utils import embeds
1010

1111

12-
log = logging.getLogger(__name__)
13-
14-
15-
@commands.Cog.listener()
16-
async def on_ready(self) -> None:
17-
"""
18-
Register the close report button that persists between bot
19-
restarts.
20-
"""
21-
self.bot.add_view(ReportCloseButton())
22-
23-
2412
class ReportCloseButton(discord.ui.View):
2513
def __init__(self) -> None:
2614
super().__init__(timeout=None)
@@ -77,7 +65,7 @@ async def submit(self, interaction: discord.Interaction, button: discord.ui.Butt
7765
style=discord.ButtonStyle.secondary,
7866
custom_id="cancel_report",
7967
)
80-
async def cancel(self, button: discord.ui.Button, interaction: discord.Interaction) -> None:
68+
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button) -> None:
8169
"""
8270
Create a View for the report message embed cancel button.
8371
"""
@@ -92,6 +80,14 @@ def __init__(self, bot) -> None:
9280
self.report_message_command = app_commands.ContextMenu(name="Report Message", callback=self.report_message)
9381
self.bot.tree.add_command(self.report_message_command)
9482

83+
@commands.Cog.listener()
84+
async def on_ready(self) -> None:
85+
"""
86+
Register the close report button that persists between bot
87+
restarts.
88+
"""
89+
self.bot.add_view(ReportCloseButton())
90+
9591
@app_commands.guilds(config["guild_id"])
9692
@app_commands.guild_only()
9793
async def report_message(self, ctx: discord.Interaction, message: discord.Message) -> None:

chiya/cogs/commands/ban.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import logging
21
import time
32

43
import discord
54
from discord import app_commands
65
from discord.ext import commands
6+
from loguru import logger as log
77

8-
from chiya import config, database
8+
from chiya import database
9+
from chiya.config import config
910
from chiya.utils import embeds
1011
from chiya.utils.helpers import can_action_member, log_embed_to_channel
1112

1213

13-
log = logging.getLogger(__name__)
14-
15-
1614
class BansCommands(commands.Cog):
1715
def __init__(self, bot: commands.Bot) -> None:
1816
self.bot = bot

0 commit comments

Comments
 (0)