Skip to content

Commit

Permalink
Fixed iteration bug during role adding (closes #29 )
Browse files Browse the repository at this point in the history
  • Loading branch information
al-matty committed Dec 18, 2022
1 parent fab207f commit aa7d5eb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
58 changes: 47 additions & 11 deletions discord_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,30 @@ async def refresh_data(self) -> None:
self.discord_telegram_map["handles"][handle] = set()
self.discord_telegram_map["handles"][handle].add(TG_id)

# Add Discord roles to set of notification triggers
# Add Discord roles to set of notification triggers & reverse lookup
if "discord roles" in v:
roles = v["discord roles"]
self.listening_to["roles"].update(roles)
# Add Discord roles to reverse lookup
for role in roles:

# Possibility: Only one role set up -> Add it to dicts
if isinstance(roles, str):
role = roles

if role not in self.discord_telegram_map["roles"]:
self.discord_telegram_map["roles"][role] = set()

self.discord_telegram_map["roles"][role].add(TG_id)
self.listening_to["roles"].add(role)

# Possibility: Multiple roles set up -> Add all to dicts
else:
for role in roles:

if role not in self.discord_telegram_map["roles"]:
self.discord_telegram_map["roles"][role] = set()

self.discord_telegram_map["roles"][role].add(TG_id)

self.listening_to["roles"].update(roles)

# Add Discord channels to channel whitelist
if "discord channels" in v:
Expand Down Expand Up @@ -263,28 +278,49 @@ async def on_message(message):


# If no role mentions in message -> Skip this part
if message.role_mentions != []:
if message.role_mentions != [] or message.mention_everyone:

log(f"ROLE MENTIONS IN MESSAGE: {message.role_mentions}")
channel = message.channel.name
whitelist = self.channel_whitelist
rolenames = [x.name for x in message.role_mentions]

# Add in mention of @everyone as role mention
if message.mention_everyone:
rolenames.append('@everyone')

# Role mentions: Forward to TG as specified in lookup dict
for role in self.listening_to["roles"]:

if role in rolenames:

log(f"MATCHED A ROLE: {message.author} mentioned {role}")
guild_id = message.guild.id
author, guild_name = message.author, message.guild.name
author, guild = message.author, message.guild
contents = message.content[message.content.find(">")+1:]
header = f"Message to {role} in {guild_name}:\n\n"
header = f"Message to {role} in {guild.name}:\n\n"
out_msg = line+header+contents+"\n"+line
TG_ids = self.discord_telegram_map["roles"][role]

# Forward to user if no channels are specified or channel is in whitelist
for _id in TG_ids:
# Cycle through all TG ids connected to this Discord role
for _id in self.discord_telegram_map["roles"][role]:

target_guild_id = self.users[_id]["discord guild"]

log(
f"GUILD CHECK: {type(guild.id)} {guild.id} =="
f" {type(target_guild_id)} {target_guild_id}:"
f" {guild.id == target_guild_id}"
)

# Condition 1: msg guild matches guild set up by user
if guild.id == target_guild_id:

log(
f"CHANNEL CHECK: {type(channel)} {channel} in"
f" {whitelist[_id]}: {channel in whitelist[_id]}\n"
f"SET UP CHANNELS: {whitelist[_id]}"
)

# Condition 2: Channel matches or no channels set up
if whitelist[_id] == set():
await self.send_to_TG(_id, out_msg)
else:
Expand Down
8 changes: 6 additions & 2 deletions telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ async def received_information(self, update, context) -> int:

# TODO: If coming from roles or channels: Ask if another should be added
del context.user_data["choice"]
log(f"RECEIVED INFORMATION:\n\ntext: {text}\ncategory: {category}")

log(
f"RECEIVED INFORMATION:\n\category type: "
f"{type(context.user_data[category])}\ntext: {text}\ncategory: {category}"
)

# Relay changes to Discord bot
await self.refresh_discord_bot()
Expand All @@ -409,7 +413,7 @@ async def received_information(self, update, context) -> int:
"Success! Your data so far:"
f"\n{self.parse_str(context.user_data)}\n"
" If the changes don't show up under 'Current active notifications'"
" yet, please allow the bot about 30s, then hit /menu again."
" yet, please allow the bot about 10s, then hit /menu again."
)

await update.message.reply_text(success_msg, reply_markup=self.markup)
Expand Down

0 comments on commit aa7d5eb

Please sign in to comment.