issue #1 and small fixes
This commit is contained in:
parent
a8779696b6
commit
1cff89cf71
21
cogs/poll.py
21
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')
|
||||
|
||||
@ -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)
|
||||
|
||||
24
utils/misc.py
Normal file
24
utils/misc.py
Normal file
@ -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
|
||||
@ -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 <label> to show a poll. '
|
||||
footer_text = footer_prefix
|
||||
if start > 0:
|
||||
footer_prefix += f'React with ⏪ to show the last {per_page} entries. '
|
||||
footer_text += f'React with ⏪ to show the last {per_page} entries. '
|
||||
if items.__len__() > start+per_page:
|
||||
footer_prefix += f'React with ⏩ to show the next {per_page} entries. '
|
||||
if footer_prefix.__len__() > 0:
|
||||
embed.set_footer(text=footer_prefix)
|
||||
footer_text += f'React with ⏩ to show the next {per_page} entries. '
|
||||
if footer_text.__len__() > 0:
|
||||
embed.set_footer(text=footer_text)
|
||||
|
||||
# post / edit message
|
||||
if msg is not None:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user