Skip to content

Commit

Permalink
Merge branch 'release/3.4.0-a2'
Browse files Browse the repository at this point in the history
  • Loading branch information
aussig committed Feb 2, 2024
2 parents c71d34e + 142b858 commit 20fb542
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 89 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## v3.4.0-a2 - 2024-02-02

### Changes:

* 'Post to Discord' button always visible (but greyed out if not usable) on Fleet Carrier and CMDR Information windows.

### Bug Fixes:

* If one of the discord webhook URLs was invalid, was failing immediately and wasn't trying any additional webhooks.
* Discord button enabling / disabling wasn't working properly with the new multiple discord channel support.


## v3.4.0-a1 - 2024-01-28

### New Features:
Expand Down
29 changes: 9 additions & 20 deletions bgstally/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def post_plaintext(self, discord_text:str, webhooks_data:dict|None, channel:Disc

for webhook in webhooks.values():
webhook_url:str = webhook.get('url')
if not self._is_webhook_valid(webhook_url): return
if not self._is_webhook_valid(webhook_url): continue

# Get the previous state for this webhook's uuid from the passed in data, if it exists. Default to the state from the webhook manager
specific_webhook_data:dict = {} if webhooks_data is None else webhooks_data.get(webhook.get('uuid', ""), webhook)
Expand Down Expand Up @@ -72,7 +72,7 @@ def post_embed(self, title:str, description:str, fields:list, webhooks_data:dict

for webhook in webhooks.values():
webhook_url:str = webhook.get('url')
if not self._is_webhook_valid(webhook_url): return
if not self._is_webhook_valid(webhook_url): continue

# Get the previous state for this webhook's uuid from the passed in data, if it exists. Default to the state from the webhook manager
specific_webhook_data:dict = {} if webhooks_data is None else webhooks_data.get(webhook.get('uuid', ""), webhook)
Expand Down Expand Up @@ -152,28 +152,17 @@ def _get_embed(self, title:str, description:str, fields:list, update:bool) -> di
return embed


def is_webhook_valid(self, channel:DiscordChannel):
def valid_webhook_available(self, channel:DiscordChannel):
"""
Check a channel's webhook is valid
Check whether there is a valid webhook available for this channel
"""
return self._is_webhook_valid(self._get_webhook(channel))
webhooks:dict = self.bgstally.webhook_manager.get_webhooks_as_dict(channel) # No need to deepcopy as we're not altering the data

for webhook in webhooks.values():
webhook_url:str = webhook.get('url')
if self._is_webhook_valid(webhook_url): return True

def _get_webhook(self, channel:DiscordChannel):
"""
Get the webhook url for the given channel
"""
match channel:
case DiscordChannel.BGS:
return self.bgstally.state.DiscordBGSWebhook.get().strip()
case DiscordChannel.CMDR_INFORMATION:
return self.bgstally.state.DiscordCMDRInformationWebhook.get().strip()
case DiscordChannel.FLEETCARRIER_MATERIALS:
return self.bgstally.state.DiscordFCMaterialsWebhook.get().strip()
case DiscordChannel.FLEETCARRIER_OPERATIONS:
return self.bgstally.state.DiscordFCOperationsWebhook.get().strip()
case DiscordChannel.THARGOIDWAR:
return self.bgstally.state.DiscordTWWebhook.get().strip()
return False


def _is_webhook_valid(self, webhook:str):
Expand Down
7 changes: 2 additions & 5 deletions bgstally/state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tkinter as tk

from bgstally.constants import CheckStates, DiscordActivity, DiscordPostStyle
from bgstally.constants import CheckStates, DiscordPostStyle
from config import config


Expand All @@ -25,15 +25,14 @@ def load(self):
self.IncludeSecondaryInf:tk.StringVar = tk.StringVar(value=config.get_str('XSecondaryInf', default=CheckStates.STATE_ON))
self.DiscordUsername:tk.StringVar = tk.StringVar(value=config.get_str('XDiscordUsername', default=""))
self.DiscordPostStyle:tk.StringVar = tk.StringVar(value=config.get_str('XDiscordPostStyle', default=DiscordPostStyle.EMBED))
self.DiscordActivity:tk.StringVar = tk.StringVar(value=config.get_str('XDiscordActivity', default=DiscordActivity.BOTH))
self.EnableOverlay:tk.StringVar = tk.StringVar(value=config.get_str('XEnableOverlay', default=CheckStates.STATE_ON))
self.EnableOverlayCurrentTick:tk.StringVar = tk.StringVar(value=config.get_str('BGST_EnableOverlayCurrentTick', default=CheckStates.STATE_ON))
self.EnableOverlayActivity:tk.StringVar = tk.StringVar(value=config.get_str('BGST_EnableOverlayActivity', default=CheckStates.STATE_ON))
self.EnableOverlayTWProgress:tk.StringVar = tk.StringVar(value=config.get_str('BGST_EnableOverlayTWProgress', default=CheckStates.STATE_ON))
self.EnableOverlaySystem:tk.StringVar = tk.StringVar(value=config.get_str('BGST_EnableOverlaySystem', default=CheckStates.STATE_ON))
self.EnableSystemActivityByDefault:tk.StringVar = tk.StringVar(value=config.get_str('BGST_EnableSystemActivityByDefault', default=CheckStates.STATE_ON))

# TODO: Legacy values, remove in future version
# TODO: Legacy values, used to migrate initial state, remove in future version
self.DiscordBGSWebhook:tk.StringVar = tk.StringVar(value=config.get_str('XDiscordWebhook', default=""))
self.DiscordCMDRInformationWebhook:tk.StringVar = tk.StringVar(value=config.get_str("BGST_DiscordCMDRInformationWebhook", default=""))
self.DiscordFCMaterialsWebhook:tk.StringVar = tk.StringVar(value=config.get_str("BGST_DiscordFCMaterialsWebhook", default=""))
Expand Down Expand Up @@ -78,7 +77,6 @@ def save(self):
config.set('XSecondaryInf', self.IncludeSecondaryInf.get())
config.set('XDiscordUsername', self.DiscordUsername.get())
config.set('XDiscordPostStyle', self.DiscordPostStyle.get())
config.set('XDiscordActivity', self.DiscordActivity.get())
config.set('XEnableOverlay', self.EnableOverlay.get())
config.set('BGST_EnableOverlayCurrentTick', self.EnableOverlayCurrentTick.get())
config.set('BGST_EnableOverlayActivity', self.EnableOverlayActivity.get())
Expand All @@ -90,4 +88,3 @@ def save(self):
config.set('XCurrentSystemID', self.current_system_id if self.current_system_id != None else "")
config.set('XStationFaction', self.station_faction if self.station_faction != None else "")
config.set('XStationType', self.station_type if self.station_type != None else "")
config.set('XStationType', self.station_type if self.station_type != None else "")
15 changes: 5 additions & 10 deletions bgstally/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,18 @@ def get_prefs_frame(self, parent_frame: tk.Frame):
HyperlinkLabel(frame, text="Instructions for Use", background=nb.Label().cget('background'), url=URL_WIKI, underline=True).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1

ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=current_row, columnspan=2, padx=10, pady=1, sticky=tk.EW); current_row += 1
nb.Label(frame, text="General", font=FONT_HEADING_2).grid(row=current_row, column=0, padx=10, sticky=tk.NW)
nb.Label(frame, text="General Options", font=FONT_HEADING_2).grid(row=current_row, column=0, padx=10, sticky=tk.NW)
nb.Checkbutton(frame, text="BGS Tally Active", variable=self.bgstally.state.Status, onvalue="Active", offvalue="Paused").grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Checkbutton(frame, text="Show Systems with Zero Activity", variable=self.bgstally.state.ShowZeroActivitySystems, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1

ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=current_row, columnspan=2, padx=10, pady=1, sticky=tk.EW); current_row += 1
nb.Label(frame, text="Discord", font=FONT_HEADING_2).grid(row=current_row, column=0, padx=10, sticky=tk.NW) # Don't increment row because we want the 1st radio option to be opposite title
nb.Label(frame, text="Activity to Include").grid(row=current_row + 1, column=0, padx=10, sticky=tk.W)
nb.Radiobutton(frame, text="BGS", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.BGS).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Radiobutton(frame, text="Thargoid War", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.THARGOIDWAR).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Radiobutton(frame, text="Both", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.BOTH).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Label(frame, text="Post Format").grid(row=current_row, column=0, padx=10, sticky=tk.W)
nb.Radiobutton(frame, text="Modern", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.EMBED).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Radiobutton(frame, text="Legacy", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.TEXT).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Label(frame, text="Other Options").grid(row=current_row, column=0, padx=10, sticky=tk.W)
nb.Label(frame, text="Discord Options", font=FONT_HEADING_2).grid(row=current_row, column=0, padx=10, sticky=tk.NW) # Don't increment row because we want the 1st radio option to be opposite title
nb.Checkbutton(frame, text="Abbreviate Faction Names", variable=self.bgstally.state.AbbreviateFactionNames, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Checkbutton(frame, text="Include Secondary INF", variable=self.bgstally.state.IncludeSecondaryInf, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Checkbutton(frame, text="Report Newly Visited System Activity By Default", variable=self.bgstally.state.EnableSystemActivityByDefault, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Label(frame, text="Post Format").grid(row=current_row, column=0, padx=10, sticky=tk.W)
nb.Radiobutton(frame, text="Modern", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.EMBED).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
nb.Radiobutton(frame, text="Legacy", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.TEXT).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1

ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=current_row, columnspan=2, padx=10, pady=1, sticky=tk.EW); current_row += 1
nb.Label(frame, text="Discord Webhooks", font=FONT_HEADING_2).grid(row=current_row, column=0, padx=10, sticky=tk.NW); current_row += 1
Expand Down
56 changes: 13 additions & 43 deletions bgstally/windows/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from functools import partial
from os import path
from tkinter import PhotoImage, ttk
from typing import Dict

from ttkHyperlinkLabel import HyperlinkLabel

Expand Down Expand Up @@ -69,7 +68,7 @@ def _show(self, activity: Activity):

DiscordTextFrame = ttk.Frame(DiscordFrame)
DiscordTextFrame.grid(row=2, column=0, pady=5, sticky=tk.NSEW)
DiscordText = DiscordAnsiColorText(DiscordTextFrame, state='disabled', wrap=tk.WORD, bg="Gray13", height=1, font=FONT_TEXT)
DiscordText = DiscordAnsiColorText(DiscordTextFrame, state='disabled', wrap=tk.WORD, bg="Gray13", height=15, font=FONT_TEXT)
DiscordScroll = tk.Scrollbar(DiscordTextFrame, orient=tk.VERTICAL, command=DiscordText.yview)
DiscordText['yscrollcommand'] = DiscordScroll.set
DiscordScroll.pack(fill=tk.Y, side=tk.RIGHT)
Expand All @@ -91,10 +90,6 @@ def _show(self, activity: Activity):
ttk.Label(DiscordOptionsFrame, text="Post Format").grid(row=current_row, column=0, padx=10, sticky=tk.W)
ttk.Radiobutton(DiscordOptionsFrame, text="Modern", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.EMBED).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Radiobutton(DiscordOptionsFrame, text="Legacy", variable=self.bgstally.state.DiscordPostStyle, value=DiscordPostStyle.TEXT).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Label(DiscordOptionsFrame, text="Activity to Include").grid(row=current_row, column=0, padx=10, sticky=tk.W)
ttk.Radiobutton(DiscordOptionsFrame, text="BGS", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.BGS, command=partial(self._option_change, DiscordText, activity)).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Radiobutton(DiscordOptionsFrame, text="Thargoid War", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.THARGOIDWAR, command=partial(self._option_change, DiscordText, activity)).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Radiobutton(DiscordOptionsFrame, text="Both", variable=self.bgstally.state.DiscordActivity, value=DiscordActivity.BOTH, command=partial(self._option_change, DiscordText, activity)).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Label(DiscordOptionsFrame, text="Other Options").grid(row=current_row, column=0, padx=10, sticky=tk.W)
ttk.Checkbutton(DiscordOptionsFrame, text="Abbreviate Faction Names", variable=self.bgstally.state.AbbreviateFactionNames, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF, command=partial(self._option_change, DiscordText, activity)).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
ttk.Checkbutton(DiscordOptionsFrame, text="Include Secondary INF", variable=self.bgstally.state.IncludeSecondaryInf, onvalue=CheckStates.STATE_ON, offvalue=CheckStates.STATE_OFF, command=partial(self._option_change, DiscordText, activity)).grid(row=current_row, column=1, padx=10, sticky=tk.W); current_row += 1
Expand Down Expand Up @@ -261,16 +256,7 @@ def _discord_button_available(self) -> bool:
"""
Return true if the 'Post to Discord' button should be available
"""
match self.bgstally.state.DiscordActivity.get():
case DiscordActivity.BGS:
return self.bgstally.discord.is_webhook_valid(DiscordChannel.BGS)
case DiscordActivity.THARGOIDWAR:
return self.bgstally.discord.is_webhook_valid(DiscordChannel.THARGOIDWAR)
case DiscordActivity.BOTH:
return self.bgstally.discord.is_webhook_valid(DiscordChannel.BGS) or \
self.bgstally.discord.is_webhook_valid(DiscordChannel.THARGOIDWAR)
case _:
return False
return self.bgstally.discord.valid_webhook_available(DiscordChannel.BGS) or self.bgstally.discord.valid_webhook_available(DiscordChannel.THARGOIDWAR)


def _update_discord_field(self, DiscordText, activity: Activity):
Expand All @@ -279,7 +265,7 @@ def _update_discord_field(self, DiscordText, activity: Activity):
"""
DiscordText.configure(state='normal')
DiscordText.delete('1.0', 'end-1c')
DiscordText.write(activity.generate_text(self.bgstally.state.DiscordActivity.get(), True))
DiscordText.write(activity.generate_text(DiscordActivity.BOTH, True))
DiscordText.configure(state='disabled')


Expand All @@ -288,32 +274,16 @@ def _post_to_discord(self, activity: Activity):
Callback to post to discord in the appropriate channel(s)
"""
if self.bgstally.state.DiscordPostStyle.get() == DiscordPostStyle.TEXT:
if self.bgstally.state.DiscordActivity.get() == DiscordActivity.BGS:
# BGS Only - post to BGS channels
discord_text:str = activity.generate_text(DiscordActivity.BGS, True)
self.bgstally.discord.post_plaintext(discord_text, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
elif self.bgstally.state.DiscordActivity.get() == DiscordActivity.THARGOIDWAR:
# TW Only - post to TW channels
discord_text:str = activity.generate_text(DiscordActivity.THARGOIDWAR, True)
self.bgstally.discord.post_plaintext(discord_text, activity.discord_webhook_data, DiscordChannel.THARGOIDWAR, self.discord_post_complete)
else:
# Both, post to both channels
discord_text:str = activity.generate_text(DiscordActivity.BOTH, True)
self.bgstally.discord.post_plaintext(discord_text, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
discord_text:str = activity.generate_text(DiscordActivity.BGS, True)
self.bgstally.discord.post_plaintext(discord_text, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
discord_text = activity.generate_text(DiscordActivity.THARGOIDWAR, True)
self.bgstally.discord.post_plaintext(discord_text, activity.discord_webhook_data, DiscordChannel.THARGOIDWAR, self.discord_post_complete)
else:
description = "" if activity.discord_notes is None else activity.discord_notes
if self.bgstally.state.DiscordActivity.get() == DiscordActivity.BGS:
# BGS Only - post to BGS channels
discord_fields:Dict = activity.generate_discord_embed_fields(DiscordActivity.BGS)
self.bgstally.discord.post_embed(f"BGS Activity after tick: {activity.get_title()}", description, discord_fields, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
elif self.bgstally.state.DiscordActivity.get() == DiscordActivity.THARGOIDWAR:
# TW Only - post to TW channels
discord_fields:Dict = activity.generate_discord_embed_fields(DiscordActivity.THARGOIDWAR)
self.bgstally.discord.post_embed(f"TW Activity after tick: {activity.get_title()}", description, discord_fields, activity.discord_webhook_data, DiscordChannel.THARGOIDWAR, self.discord_post_complete)
else:
# Both, post to both channels
discord_fields:Dict = activity.generate_discord_embed_fields(DiscordActivity.BOTH)
self.bgstally.discord.post_embed(f"Activity after tick: {activity.get_title()}", description, discord_fields, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
discord_fields:dict = activity.generate_discord_embed_fields(DiscordActivity.BGS)
self.bgstally.discord.post_embed(f"BGS Activity after tick: {activity.get_title()}", description, discord_fields, activity.discord_webhook_data, DiscordChannel.BGS, self.discord_post_complete)
discord_fields = activity.generate_discord_embed_fields(DiscordActivity.THARGOIDWAR)
self.bgstally.discord.post_embed(f"TW Activity after tick: {activity.get_title()}", description, discord_fields, activity.discord_webhook_data, DiscordChannel.THARGOIDWAR, self.discord_post_complete)

activity.dirty = True # Because discord post ID has been changed

Expand Down Expand Up @@ -476,7 +446,7 @@ def _scenarios_change(self, notebook: ScrollableNotebook, tab_index: int, Scenar
activity.dirty = True


def _update_tab_image(self, notebook: ScrollableNotebook, tab_index: int, EnableAllCheckbutton, system: Dict):
def _update_tab_image(self, notebook: ScrollableNotebook, tab_index: int, EnableAllCheckbutton, system: dict):
"""
Update the image alongside the tab title
"""
Expand All @@ -497,6 +467,6 @@ def _copy_to_clipboard(self, Form:tk.Frame, activity:Activity):
Get all text from the Discord field and put it in the Copy buffer
"""
Form.clipboard_clear()
Form.clipboard_append(activity.generate_text(self.bgstally.state.DiscordActivity.get(), True))
Form.clipboard_append(activity.generate_text(DiscordActivity.BOTH, True))
Form.update()

Loading

0 comments on commit 20fb542

Please sign in to comment.