Skip to content

Commit b57871b

Browse files
committed
ADD: Proper logging with outputs to a file
1 parent 8f86f90 commit b57871b

11 files changed

Lines changed: 260 additions & 60 deletions

File tree

cogs/discover.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Callable
22
from functools import wraps
3+
import logging
34

45
from discord.ext import commands
56

@@ -8,24 +9,27 @@ class DiscoverModule(commands.Cog):
89
def apply_filters(func: Callable) -> Callable:
910
@wraps(func)
1011
async def wrapper(self, ctx: commands.Context, *, filter_string: str):
11-
print(len(filter_string))
12-
print(" ".join(filter_string))
12+
logging.info(len(filter_string))
13+
logging.info(" ".join(filter_string))
1314

1415
parse_filters = {}
1516

1617
await func(self, ctx, filters=parse_filters)
1718

1819
return wrapper
1920

20-
@commands.group(name="discover", description="Discover media list from anilist. Supports FILTERS")
21+
@commands.group(
22+
name="discover",
23+
description="Discover media list from anilist. Supports FILTERS",
24+
)
2125
async def discover_group(self, ctx: commands.Context):
2226
if ctx.subcommand_passed is None:
2327
return ctx.send("Please provide a valid subcommand!")
2428

2529
@discover_group.command(name="anime", description="Disover anime by filters")
2630
@apply_filters
2731
async def discover_anime(self, ctx: commands.Context, *, filters):
28-
print(filters)
32+
logging.info(filters)
2933

3034

3135
def setup(bot: commands.Bot):

cogs/filler.py

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from discord.ext import commands
22
from discord import Embed
3+
import logging
34

45
from views.scroller import Scroller
56
from helpers import general_helper
@@ -31,7 +32,11 @@ async def parse_filler(self, anime_match: dict) -> list:
3132
embds = []
3233

3334
if len(anime_match.items()) <= 0:
34-
return [Embed(title="No Filler Information was found!", color=config.NORMAL_COLOR)]
35+
return [
36+
Embed(
37+
title="No Filler Information was found!", color=config.NORMAL_COLOR
38+
)
39+
]
3540

3641
# True if at least one filler data item was found.
3742
filler_found: bool = False
@@ -41,42 +46,69 @@ async def parse_filler(self, anime_match: dict) -> list:
4146
data = config.FILLER_DATA["data"][id]
4247
except KeyError:
4348
if filler_found is False:
44-
embds.append(Embed(title="No Filler Information was found!", color=config.NORMAL_COLOR))
49+
embds.append(
50+
Embed(
51+
title="No Filler Information was found!",
52+
color=config.NORMAL_COLOR,
53+
)
54+
)
4555
continue
4656
continue
4757
else:
4858
if filler_found is False:
4959
filler_found = True
5060
embds.clear()
5161

52-
embd = Embed(title="{name}'s Filler Episodes".format(name=name), color=config.NORMAL_COLOR)
62+
embd = Embed(
63+
title="{name}'s Filler Episodes".format(name=name),
64+
color=config.NORMAL_COLOR,
65+
)
5366

5467
try:
55-
embd.add_field(name="Anime Canon Episodes", value="```{}```".format(data["anime_canon"]), inline=False)
56-
57-
embd.add_field(name="Manga Canon Episodes", value="```{}```".format(data["manga_canon"]), inline=False)
58-
59-
embd.add_field(name="Mixed Episodes", value="```{}```".format(data["mixed_ep"]), inline=False)
60-
61-
embd.add_field(name="Filler Episodes", value="```{}```".format(data["filler_ep"]), inline=False)
68+
embd.add_field(
69+
name="Anime Canon Episodes",
70+
value="```{}```".format(data["anime_canon"]),
71+
inline=False,
72+
)
73+
74+
embd.add_field(
75+
name="Manga Canon Episodes",
76+
value="```{}```".format(data["manga_canon"]),
77+
inline=False,
78+
)
79+
80+
embd.add_field(
81+
name="Mixed Episodes",
82+
value="```{}```".format(data["mixed_ep"]),
83+
inline=False,
84+
)
85+
86+
embd.add_field(
87+
name="Filler Episodes",
88+
value="```{}```".format(data["filler_ep"]),
89+
inline=False,
90+
)
6291
except Exception:
63-
print(name)
92+
logging.error(name)
6493

6594
embds.append(embd)
6695

6796
return embds
6897

69-
@commands.command(name="filler", description="Returns a list of all the recorded filler episodes of the anime")
98+
@commands.command(
99+
name="filler",
100+
description="Returns a list of all the recorded filler episodes of the anime",
101+
)
70102
@general_helper.short_cooldown()
71103
@general_helper.with_typing_ctx()
72104
async def filler(self, ctx: commands.Context, *anime):
73105
anime = " ".join(anime)
74106

75-
print("Anime name is " + anime)
107+
logging.info("Anime name is " + anime)
76108

77109
anime_match = await self.search_filler(anime)
78110

79-
print(anime_match)
111+
logging.info(anime_match)
80112

81113
embds = await self.parse_filler(anime_match)
82114

cogs/logger.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
import logging
12
from discord.ext import commands
23
from discord import Guild, TextChannel
34

45
from helpers import general_helper
5-
from config import SERVER_LOG_CHANNEL, ERROR_COLOR, YUI_SHY_EMOTE, SUPPORT_SERVER_LINK, VOTE_LINK
6+
from config import (
7+
SERVER_LOG_CHANNEL,
8+
ERROR_COLOR,
9+
YUI_SHY_EMOTE,
10+
SUPPORT_SERVER_LINK,
11+
VOTE_LINK,
12+
)
613

714

815
class Logger(commands.Cog):
916
bot: commands.Bot = None
1017
server_log_channel: TextChannel = None
1118

12-
general_chat_channel_names = ["general", "chat", "lounge", "main", "talk", "chatting", "talking"]
19+
general_chat_channel_names = [
20+
"general",
21+
"chat",
22+
"lounge",
23+
"main",
24+
"talk",
25+
"chatting",
26+
"talking",
27+
]
1328

1429
welcome_embd = None
1530

@@ -29,22 +44,41 @@ async def load(self):
2944
@commands.Cog.listener("on_guild_join")
3045
async def on_guild_join(self, guild: Guild):
3146
await self.server_log_channel.send(
32-
embed=await general_helper.get_information_embed(title="Server Added!", description="Name : **{name}**\nMember Count : **{count}**\nTotal Server Count : **{total}**".format(name=guild.name, count=guild.member_count, total=len(self.bot.guilds)), thumbnail_link=self.bot.user.avatar.url)
47+
embed=await general_helper.get_information_embed(
48+
title="Server Added!",
49+
description="Name : **{name}**\nMember Count : **{count}**\nTotal Server Count : **{total}**".format(
50+
name=guild.name,
51+
count=guild.member_count,
52+
total=len(self.bot.guilds),
53+
),
54+
thumbnail_link=self.bot.user.avatar.url,
55+
)
3356
)
3457

3558
for channel in guild.text_channels:
3659
if channel.permissions_for(guild.me).send_messages:
37-
if channel == guild.system_channel or any(x in channel.name for x in self.general_chat_channel_names):
60+
if channel == guild.system_channel or any(
61+
x in channel.name for x in self.general_chat_channel_names
62+
):
3863
await channel.send(embed=self.welcome_embd)
3964
break
4065
else:
41-
print(f"Can't send messages in # {channel.name}")
66+
logging.error(f"Can't send messages in # {channel.name}")
4267
continue
4368

4469
@commands.Cog.listener("on_guild_remove")
4570
async def on_guild_remove(self, guild: Guild):
4671
await self.server_log_channel.send(
47-
embed=await general_helper.get_information_embed(title="Server Removed!", description="Name : **{name}**\nMember Count : **{count}**\nTotal Server Count : **{total}**".format(name=guild.name, count=guild.member_count, total=len(self.bot.guilds)), thumbnail_link=self.bot.user.avatar.url, color=ERROR_COLOR)
72+
embed=await general_helper.get_information_embed(
73+
title="Server Removed!",
74+
description="Name : **{name}**\nMember Count : **{count}**\nTotal Server Count : **{total}**".format(
75+
name=guild.name,
76+
count=guild.member_count,
77+
total=len(self.bot.guilds),
78+
),
79+
thumbnail_link=self.bot.user.avatar.url,
80+
color=ERROR_COLOR,
81+
)
4882
)
4983

5084

config.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import traceback
55
import sys
66
from dotenv import load_dotenv
7+
import logging
78

89
load_dotenv()
910

@@ -101,7 +102,18 @@
101102

102103

103104
def initialize_config_vars() -> str:
104-
global DISCORD_TOKEN, DISCORD_TEST_TOKEN, ANILIST_ID, ANILIST_TOKEN, INVITE, MONGO_SRV, FILLER_DATA, SECRET_KEY, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, YOUTUBE_API_KEY
105+
global \
106+
DISCORD_TOKEN, \
107+
DISCORD_TEST_TOKEN, \
108+
ANILIST_ID, \
109+
ANILIST_TOKEN, \
110+
INVITE, \
111+
MONGO_SRV, \
112+
FILLER_DATA, \
113+
SECRET_KEY, \
114+
SPOTIFY_CLIENT_ID, \
115+
SPOTIFY_CLIENT_SECRET, \
116+
YOUTUBE_API_KEY
105117

106118
load_dotenv()
107119

@@ -121,7 +133,7 @@ def initialize_config_vars() -> str:
121133
FILLER_DATA = json.load(filler_data)
122134

123135
except Exception as e:
124-
print(f"Error occurred while trying to cache Config Vars! \n{e}")
136+
logging.error(f"Error occurred while trying to cache Config Vars! \n{e}")
125137
traceback.print_exception(type(e), e, e.__traceback__, file=sys.stderr)
126138
else:
127-
print("Config LOADED!")
139+
logging.info("Config LOADED!")

0 commit comments

Comments
 (0)