Skip to content

Commit

Permalink
Merge pull request #17 from rssnyder/always-update
Browse files Browse the repository at this point in the history
Always update
  • Loading branch information
rssnyder authored Mar 1, 2021
2 parents ae23dbb + 65bb861 commit 6d43215
Showing 1 changed file with 39 additions and 59 deletions.
98 changes: 39 additions & 59 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ async def stock_update_name(self, ticker: str, name: str):
name = override for symbol as shown on bot
'''

old_price = ''

await self.wait_until_ready()
logging.info(f'stock name update ready: {name}')

Expand All @@ -111,21 +109,14 @@ async def stock_update_name(self, ticker: str, name: str):
price = price_data.get('regularMarketPrice', {}).get('raw', 0.00)
logging.info(f'stock name price retrived {price}')

# Only update on price change
if old_price != price:

try:
await self.user.edit(
username=f'{name} - ${price}'
)
try:
await self.user.edit(
username=f'{name} - ${price}'
)

old_price = price
logging.info('name updated')
except discord.HTTPException as e:
logging.warning(f'updating name failed: {e.status}: {e.text}')

else:
logging.info('no price change')
logging.info('name updated')
except discord.HTTPException as e:
logging.warning(f'updating name failed: {e.status}: {e.text}')

# Only update every hour
logging.info(f'stock name sleeping for {NAME_CHANGE_DELAY}s')
Expand All @@ -142,9 +133,6 @@ async def stock_update_activity(self, ticker: str, name: str, change_nick: bool
frequency = how often to update in seconds
'''

# Get config
old_price = ''

await self.wait_until_ready()
logging.info(f'stock activity update ready: {name}')

Expand Down Expand Up @@ -176,52 +164,44 @@ async def stock_update_activity(self, ticker: str, name: str, change_nick: bool
activity_content = f'${price} / {diff}'
logging.info(f'stock activity price retrived: {activity_content}')

# Only update on price change
if old_price != price:

# Change name via nickname if set
if change_nick:
# Change name via nickname if set
if change_nick:

for server in self.guilds:
for server in self.guilds:

try:
await server.me.edit(
nick=f'{name} - ${price}'
)
except discord.HTTPException as e:
logging.error(f'updating nick failed: {e.status}: {e.text}')
except discord.Forbidden as f:
logging.error(f'lacking perms for chaning nick: {f.status}: {f.text}')

logging.info(f'stock updated nick in {server.name}')

# Check what price we are displaying
if price_data.get('postMarketChange'):
activity_content_header = 'After Hours'
else:
activity_content_header = 'Day Diff'

activity_content = f'{activity_content_header}: {diff}'


# Change activity
try:
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=activity_content
try:
await server.me.edit(
nick=f'{name} - ${price}'
)
except discord.HTTPException as e:
logging.error(f'updating nick failed: {e.status}: {e.text}')
except discord.Forbidden as f:
logging.error(f'lacking perms for chaning nick: {f.status}: {f.text}')

logging.info(f'stock updated nick in {server.name}')

# Check what price we are displaying
if price_data.get('postMarketChange'):
activity_content_header = 'After Hours'
else:
activity_content_header = 'Day Diff'

activity_content = f'{activity_content_header}: {diff}'


# Change activity
try:
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=activity_content
)
)

old_price = price
logging.info('activity updated')

except discord.InvalidArgument as e:
logging.error(f'updating activity failed: {e.status}: {e.text}')

logging.info('activity updated')

else:
logging.info('no price change')
except discord.InvalidArgument as e:
logging.error(f'updating activity failed: {e.status}: {e.text}')

# Only update every min
logging.info(f'stock activity sleeping for {frequency}s')
Expand Down

0 comments on commit 6d43215

Please sign in to comment.