Skip to content

Commit

Permalink
Add permission checks and change dropdown text length
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Aug 17, 2021
1 parent e53e508 commit e1c088c
Showing 1 changed file with 76 additions and 21 deletions.
97 changes: 76 additions & 21 deletions cogs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ async def on_ready(self):
logger.info(
f"Loading {len(items)} feed{'s' if len(items) > 1 else ''} that exist in the database."
)

error_channel_ids = []

for item in items:
channel = self.client.get_channel(int(item["channel"]))
channel: discord.TextChannel = self.client.get_channel(int(item["channel"]))

if item in self.feeds:
continue
Expand All @@ -351,6 +354,42 @@ async def on_ready(self):
await database.feed_remove([item])
continue

permissions = channel.permissions_for(self.client)
if not permissions:
logger.debug(
f"Could not load <{item['username']}:{item['channel']}:{item['type']}> - Invalid permissions"
)
continue
elif not permissions.send_messages:

if channel.id not in error_channel_ids:
guild = channel.guild
owner = guild.get_member(guild.owner_id)

embed = discord.Embed(
title="`Warning`",
description=f"Missing Access\nCannot send messages to channel `#{channel.name}`",
color=color_warn,
)
embed.add_field(
name="Error",
value="```The feed has been automatically removed.\n"
"Please setup a feed again after giving Mitsu the 'Send Messages' permission.```",
)

try:
await owner.send(embed=embed)
except:
pass

error_channel_ids.append(channel.id)

logger.debug(
f"Could not load <{item['username']}:{item['channel']}:{item['type']}> - Incorrect permissions"
)
await database.feed_remove([item])
continue

user: Activity = await Activity.create(
item["username"], channel, int(item["type"])
)
Expand All @@ -370,6 +409,7 @@ async def on_ready(self):
)

logger.info(f"Loaded {len(self.feeds)}.")
del error_channel_ids

async def process(self):
"""Processes all feeds with the specified interval in the config file."""
Expand Down Expand Up @@ -412,6 +452,21 @@ async def _activity(self, ctx: SlashContext, username: str):
)
return

permissions = ctx.channel.permissions_for(self.client)
if not permissions or not permissions.send_messages:
embed = discord.Embed(
title="`Warning`",
description=f"Missing Access\nCannot send messages to this channel.",
color=color_warn,
)
embed.add_field(
name="Error",
value="```Please grant Mitsu the 'Send Messages' permission\n'"
"and try again.",
)
await ctx.send("Incorrect permissions", hidden=True)
return

select = create_select(
custom_id="_activity0",
options=[
Expand Down Expand Up @@ -595,7 +650,7 @@ async def _edit(self, ctx: SlashContext):
custom_id="_edit0",
options=[
create_select_option(
label=i if len(i) <= 25 else i[:22] + "...",
label=i if len(i) <= 100 else i[:97] + "...",
description=", ".join(
[Feed.get_type(f.type) for f in items if f.username == i]
),
Expand Down Expand Up @@ -633,7 +688,7 @@ def check_author(cctx: ComponentContext):
custom_id="_edit1",
options=[
create_select_option(
label=(i if len(i) <= 25 else (i[:22] + "...")),
label=(i if len(i) <= 100 else (i[:97] + "...")),
description=", ".join(
[Feed.get_type(i.type) for i in all_types]
),
Expand Down Expand Up @@ -691,7 +746,7 @@ def check_author(cctx: ComponentContext):
custom_id="_edit1",
options=[
create_select_option(
label=(i if len(i) <= 25 else (i[:22] + "...")),
label=(i if len(i) <= 100 else (i[:97] + "...")),
description=", ".join(
[Feed.get_type(i.type) for i in all_types]
),
Expand Down Expand Up @@ -732,7 +787,7 @@ def check_author(cctx: ComponentContext):
custom_id="_edit1",
options=[
create_select_option(
label=(i if len(i) <= 25 else (i[:22] + "...")),
label=(i if len(i) <= 100 else (i[:97] + "...")),
description=", ".join(
[Feed.get_type(i.type) for i in all_types]
),
Expand Down Expand Up @@ -978,28 +1033,28 @@ async def _search(self, ctx: SlashContext, media: str, query: str) -> CAnime:
label=(
(
i.title.romaji
if len(i.title.romaji) <= 25
else i.title.romaji[:22] + "..."
if len(i.title.romaji) <= 100
else i.title.romaji[:97] + "..."
)
if media != "character"
else (
i.name.full
if len(i.name.full) <= 25
else i.name.full[:22] + "..."
if len(i.name.full) <= 100
else i.name.full[:97] + "..."
)
),
description=(
(
(
i.title.english
if len(i.title.english) <= 50
else i.title.english[:47] + "..."
if len(i.title.english) <= 100
else i.title.english[:97] + "..."
)
if hasattr(i.title, "english")
else (
i.title.native
if len(i.title.native) <= 50
else i.title.native[:47] + "..."
if len(i.title.native) <= 100
else i.title.native[:97] + "..."
)
)
if media != "character"
Expand Down Expand Up @@ -1048,28 +1103,28 @@ def check_author(cctx: ComponentContext):
label=(
(
i.title.romaji
if len(i.title.romaji) <= 25
else i.title.romaji[:22] + "..."
if len(i.title.romaji) <= 100
else i.title.romaji[:97] + "..."
)
if media != "character"
else (
i.name.full
if len(i.name.full) <= 25
else i.name.full[:22] + "..."
if len(i.name.full) <= 100
else i.name.full[:97] + "..."
)
),
description=(
(
(
i.title.english
if len(i.title.english) <= 50
else i.title.english[:47] + "..."
if len(i.title.english) <= 100
else i.title.english[:97] + "..."
)
if hasattr(i.title, "english")
else (
i.title.native
if len(i.title.native) <= 50
else i.title.native[:47] + "..."
if len(i.title.native) <= 100
else i.title.native[:97] + "..."
)
)
if media != "character"
Expand Down

0 comments on commit e1c088c

Please sign in to comment.