diff --git a/cogs/poll.py b/cogs/poll.py index b5f6d12..eb83942 100644 --- a/cogs/poll.py +++ b/cogs/poll.py @@ -16,6 +16,7 @@ import discord from essentials.multi_server import get_pre from essentials.exceptions import * from essentials.settings import SETTINGS +from utils.misc import possible_timezones logger = logging.getLogger('bot') @@ -60,7 +61,7 @@ class Poll: self.weights_roles = [] self.weights_numbers = [] self.duration = 0 - self.duration_tz = 'UTC' + self.duration_tz = 0.0 self.time_created = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) self.open = True @@ -666,7 +667,10 @@ class Poll: return dt try: - self.duration = await get_valid(force) + dt = await get_valid(force) + self.duration = dt + if self.duration != 0: + self.duration_tz = dt.utcoffset().total_seconds() / 3600 return except InputError: pass @@ -688,7 +692,7 @@ class Poll: if self.duration == 0: await self.add_vaild(message, 'until closed manually') else: - self.duration_tz = dt.tzinfo.tzname(dt) + self.duration_tz = dt.utcoffset().total_seconds() / 3600 await self.add_vaild(message, self.duration.strftime('%d-%b-%Y %H:%M %Z')) break except InvalidInput: @@ -1006,7 +1010,16 @@ class Poll: deadline = self.duration if deadline.tzinfo is None or deadline.tzinfo.utcoffset(deadline) is None: deadline = pytz.utc.localize(deadline) - tz = pytz.timezone(self.duration_tz) + if isinstance(self.duration_tz, float): + tz = possible_timezones(self.duration_tz, common_only=True) + if not tz: + tz = pytz.timezone('UTC') + else: + # choose one valid timezone with the offset + tz = pytz.timezone(tz[0]) + else: + tz = pytz.timezone(self.duration_tz) + deadline = deadline.astimezone(tz) if string: return deadline.strftime('%d-%b-%Y %H:%M %Z') diff --git a/pollmaster.py b/pollmaster.py index 69dffb7..abc5613 100644 --- a/pollmaster.py +++ b/pollmaster.py @@ -16,7 +16,7 @@ bot_config = { 'status': discord.Status.online, 'owner_id': SETTINGS.owner_id, 'fetch_offline_members': False, - 'max_messages': 200000 + 'max_messages': 15000 } bot = commands.Bot(**bot_config) diff --git a/utils/misc.py b/utils/misc.py new file mode 100644 index 0000000..a7ef100 --- /dev/null +++ b/utils/misc.py @@ -0,0 +1,24 @@ +import pytz +import datetime as dt + +def possible_timezones(tz_offset, common_only=True): + # pick one of the timezone collections + timezones = pytz.common_timezones if common_only else pytz.all_timezones + + # convert the float hours offset to a timedelta + offset_days, offset_seconds = 0, int(tz_offset * 3600) + if offset_seconds < 0: + offset_days = -1 + offset_seconds += 24 * 3600 + desired_delta = dt.timedelta(offset_days, offset_seconds) + + # Loop through the timezones and find any with matching offsets + null_delta = dt.timedelta(0, 0) + results = [] + for tz_name in timezones: + tz = pytz.timezone(tz_name) + non_dst_offset = getattr(tz, '_transition_info', [[null_delta]])[-1] + if desired_delta == non_dst_offset[0]: + results.append(tz_name) + + return results \ No newline at end of file diff --git a/utils/paginator.py b/utils/paginator.py index 6ccb5b4..c1d3385 100644 --- a/utils/paginator.py +++ b/utils/paginator.py @@ -10,12 +10,13 @@ async def embed_list_paginated(bot, pre, items, item_fct, base_embed, footer_pre # footer text #footer_text = f'Type {pre}show