Added exception handling to Discord API calls (#510)

* added exception handling to discord api calls

* restored accidental deleted vars

* removed word "discord" from log in generic api
This commit is contained in:
pokzcodes
2022-02-05 13:24:41 -05:00
committed by GitHub
parent ab1d496fff
commit a87d72a044
2 changed files with 16 additions and 5 deletions
+10 -4
View File
@@ -1,19 +1,23 @@
from .generic_api import GenericApi
from config import Config
from logger import Logger
import cv2
import datetime
import traceback
import discord
from version import __version__
import numpy as np
from discord import Webhook, RequestsWebhookAdapter, Color
from discord import Webhook, RequestsWebhookAdapter, Color, InvalidArgument
class DiscordEmbeds(GenericApi):
def __init__(self):
self._config = Config()
self._webhook = Webhook.from_url(self._config.general['custom_message_hook'], adapter=RequestsWebhookAdapter(), )
self._file = None
self._psnURL = "https://i.psnprofiles.com/games/3bffee/trophies/"
try:
self._webhook = Webhook.from_url(self._config.general['custom_message_hook'], adapter=RequestsWebhookAdapter(), )
except InvalidArgument:
Logger.warning(f"Your custom_message_hook URL {self._config.general['custom_message_hook']} is invalid, Discord updates will not be sent")
def send_item(self, item: str, image: np.ndarray, location: str):
imgName = item.replace('_', '-')
@@ -73,8 +77,10 @@ class DiscordEmbeds(GenericApi):
def _send_embed(self, e, file = None):
e.set_footer(text=f'Botty v.{__version__} by Aeon')
e.timestamp=datetime.datetime.now(datetime.timezone.utc)
self._webhook.send(embed=e, file=file, username=self._config.general['name'])
try:
self._webhook.send(embed=e, file=file, username=self._config.general['name'])
except BaseException as err:
Logger.error("Error sending Discord embed: " + err)
def _get_Item_Color(self, item):
if "magic_" in item:
+6 -1
View File
@@ -1,4 +1,5 @@
from config import Config
from logger import Logger
import numpy as np
import json
import requests
@@ -32,6 +33,7 @@ class GenericApi:
def _send(self, msg: str):
msg = f"{self._config.general['name']}: {msg}"
url = self._config.general['custom_message_hook']
if not url:
return
@@ -42,4 +44,7 @@ class GenericApi:
data = json.loads(self._config.advanced_options['message_body_template'].format(msg=msg), strict=False)
requests.post(url, headers=headers, json=data)
try:
requests.post(url, headers=headers, json=data)
except BaseException as err:
Logger.error("Error sending generic message: " + err)