Skip to content

Commit bc538e7

Browse files
committed
Update bot version to 1.7.2 and enhance publishing logic
- Updated bot version from 1.7.1 to 1.7.2 in Dockerfile and main.py. - Modified `auto_publish` function to include `channel` and `permissions` parameters for better control. - Refactored permission checking and message publishing logic for improved clarity. - Enhanced error handling with additional logging for various scenarios, including `discord.NotFound`. - Adjusted reaction handling to occur based on permissions and retry attempts.
1 parent abd6f04 commit bc538e7

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ LABEL maintainer="Discord: pika.pika.no.mi (970119359840284743)" \
2929
description="This bot automatically publishes messages in announcement channels on discord." \
3030
release=$BUILD_DATE \
3131
url="https://github.com/Serpensin/DiscordBots-AutoPublisher" \
32-
version="1.7.1"
32+
version="1.7.2"
3333

3434
CMD ["python3", "main.py"]

main.py

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#Init
2525
discord.VoiceClient.warn_nacl = False
2626
load_dotenv()
27-
BOT_VERSION = '1.7.1'
27+
BOT_VERSION = '1.7.2'
2828
APP_FOLDER_NAME = 'AutoPublisher'
2929
BOT_NAME = 'AutoPublisher'
3030
if not os.path.exists(f'{APP_FOLDER_NAME}//Logs'):
@@ -226,15 +226,20 @@ async def __wrong_selection():
226226
'status - Set the status of the bot\n'
227227
'```')
228228

229+
channel = message.channel
230+
permissions = channel.permissions_for(channel.guild.me)
231+
229232
if message.author == bot.user:
230233
return
231234
if (
232235
message.channel.type == discord.ChannelType.news
233236
and message.type not in NON_PUBLISHABLE_MESSAGE_TYPES
237+
and permissions.send_messages
238+
and permissions.manage_messages
234239
and not message.flags.crossposted
235240
and not message.flags.is_crossposted
236241
):
237-
await Functions.auto_publish(message)
242+
await Functions.auto_publish(message, channel, permissions)
238243
if message.guild is None and message.author.id == int(OWNERID):
239244
args = message.content.split(' ')
240245
program_logger.debug(args)
@@ -343,50 +348,36 @@ async def __health_check(request):
343348
except OSError as e:
344349
program_logger.warning(f'Error while starting health server: {e}')
345350

346-
async def auto_publish(message: discord.Message, retries: int = 3):
347-
channel = message.channel
348-
permissions = channel.permissions_for(channel.guild.me)
349-
350-
if permissions.add_reactions:
351+
async def auto_publish(message: discord.Message, channel: discord.TextChannel, permissions: discord.Permissions, retries: int = 3):
352+
if permissions.add_reactions and retries == 3:
351353
await message.add_reaction("\U0001F4E2") # 📢
352354

353-
if permissions.send_messages and permissions.manage_messages:
354-
try:
355-
await message.publish()
356-
357-
except discord.HTTPException as e:
358-
if e.code == 50068:
359-
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} is not an announcement message. (Type {message.type.value})")
360-
elif e.code == 40033:
361-
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} is already published.")
362-
elif e.status == 503 and e.code == 0:
363-
if retries > 0:
364-
discord_logger.info(f"Discord is currently unavailable. Retrying to publish message {message.id} in channel {channel.id} on guild {message.guild.id}. Retries left: {retries}")
365-
await asyncio.sleep(5)
366-
await Functions.auto_publish(message, retries=retries - 1)
367-
return
368-
else:
369-
discord_logger.warning(f"Failed to publish message {message.id} in channel {channel.id} on guild {message.guild.id} after retries.")
355+
try:
356+
await message.publish()
357+
except discord.HTTPException as e:
358+
if e.code == 50068:
359+
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} is not an announcement message. (Type {message.type.value})")
360+
elif e.code == 40033:
361+
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} is already published.")
362+
elif e.status == 503 and e.code == 0:
363+
if retries > 0:
364+
discord_logger.info(f"Discord is currently unavailable. Retrying to publish message {message.id} in channel {channel.id} on guild {message.guild.id}. Retries left: {retries}")
365+
await asyncio.sleep(5)
366+
await Functions.auto_publish(message, channel, permissions, retries=retries - 1)
367+
return
370368
else:
371-
raise
372-
373-
except discord.NotFound:
374-
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} not found.")
375-
376-
except Exception as e:
377-
if not message.flags.crossposted:
378-
discord_logger.error(f"Error publishing message in {channel.id} on {message.guild.id}: {e}")
379-
if permissions.add_reactions:
380-
await message.add_reaction("\u26A0") # ⚠️
381-
382-
finally:
383-
await message.remove_reaction("\U0001F4E2", bot.user)
384-
385-
else:
386-
discord_logger.info(f"No permission to publish in {channel.id} on guild {message.guild.id}.")
369+
discord_logger.warning(f"Failed to publish message {message.id} in channel {channel.id} on guild {message.guild.id} after retries.")
370+
else:
371+
raise
372+
except discord.NotFound:
373+
discord_logger.info(f"Message {message.id} in channel {channel.id} on guild {message.guild.id} not found.")
374+
except Exception as e:
375+
if not message.flags.crossposted:
376+
discord_logger.error(f"Error publishing message in {channel.id} on {message.guild.id}: {e}")
377+
if permissions.add_reactions:
378+
await message.add_reaction("\u26A0") # ⚠️
379+
finally:
387380
await message.remove_reaction("\U0001F4E2", bot.user)
388-
if permissions.add_reactions:
389-
await message.add_reaction("\u26D4") # ⛔
390381

391382
async def create_support_invite(interaction):
392383
try:

0 commit comments

Comments
 (0)