Browse Source

Various fixes, remove lazy_datetime, etc

feature/docs
Andrei 8 years ago
parent
commit
713c1be01b
  1. 17
      disco/bot/plugin.py
  2. 6
      disco/gateway/events.py
  3. 15
      disco/types/base.py
  4. 5
      disco/types/guild.py
  5. 4
      disco/types/invite.py
  6. 8
      disco/types/message.py

17
disco/bot/plugin.py

@ -5,6 +5,7 @@ import inspect
import weakref
import functools
from gevent.event import AsyncResult
from holster.emitter import Priority
from disco.util.logging import LoggingClass
@ -208,6 +209,22 @@ class Plugin(LoggingClass, PluginDeco):
def handle_exception(self, greenlet, event):
pass
def wait_for_event(self, event_name, **kwargs):
result = AsyncResult()
listener = None
def _event_callback(event):
for k, v in kwargs.items():
if getattr(event, k) != v:
break
else:
listener.remove()
return result.set(event)
listener = self.bot.client.events.on(event_name, _event_callback)
return result
def spawn_wrap(self, spawner, method, *args, **kwargs):
def wrapped(*args, **kwargs):
self.ctx['plugin'] = self

6
disco/gateway/events.py

@ -9,7 +9,7 @@ from disco.types.message import Message, MessageReactionEmoji
from disco.types.voice import VoiceState
from disco.types.guild import Guild, GuildMember, Role, GuildEmoji
from disco.types.base import Model, ModelMeta, Field, ListField, AutoDictField, snowflake, lazy_datetime
from disco.types.base import Model, ModelMeta, Field, ListField, AutoDictField, snowflake, datetime
# Mapping of discords event name to our event classes
EVENTS_MAP = {}
@ -244,7 +244,7 @@ class ChannelPinsUpdate(GatewayEvent):
The time the last message was pinned.
"""
channel_id = Field(snowflake)
last_pin_timestamp = Field(lazy_datetime)
last_pin_timestamp = Field(datetime)
@proxy(User)
@ -539,7 +539,7 @@ class TypingStart(GatewayEvent):
"""
channel_id = Field(snowflake)
user_id = Field(snowflake)
timestamp = Field(lazy_datetime)
timestamp = Field(datetime)
@wraps_model(VoiceState, alias='state')

15
disco/types/base.py

@ -177,26 +177,13 @@ def enum(typ):
return _f
# TODO: make lazy
def lazy_datetime(data):
def datetime(data):
if not data:
return None
if isinstance(data, int):
return real_datetime.utcfromtimestamp(data)
for fmt in DATETIME_FORMATS:
try:
return real_datetime.strptime(data.rsplit('+', 1)[0], fmt)
except (ValueError, TypeError):
continue
raise ValueError('Failed to conver `{}` to datetime'.format(data))
def datetime(data):
if not data:
return None
for fmt in DATETIME_FORMATS:
try:
return real_datetime.strptime(data.rsplit('+', 1)[0], fmt)

5
disco/types/guild.py

@ -7,7 +7,7 @@ from disco.api.http import APIException
from disco.util.snowflake import to_snowflake
from disco.util.functional import cached_property
from disco.types.base import (
SlottedModel, Field, ListField, AutoDictField, snowflake, text, binary, enum
SlottedModel, Field, ListField, AutoDictField, snowflake, text, binary, enum, datetime
)
from disco.types.user import User, Presence
from disco.types.voice import VoiceState
@ -21,7 +21,6 @@ VerificationLevel = Enum(
LOW=1,
MEDIUM=2,
HIGH=3,
EXTREME=4,
)
@ -141,7 +140,7 @@ class GuildMember(SlottedModel):
nick = Field(text)
mute = Field(bool)
deaf = Field(bool)
joined_at = Field(str)
joined_at = Field(datetime)
roles = ListField(snowflake)
def __str__(self):

4
disco/types/invite.py

@ -1,4 +1,4 @@
from disco.types.base import SlottedModel, Field, lazy_datetime
from disco.types.base import SlottedModel, Field, datetime
from disco.types.user import User
from disco.types.guild import Guild
from disco.types.channel import Channel
@ -37,7 +37,7 @@ class Invite(SlottedModel):
max_uses = Field(int)
uses = Field(int)
temporary = Field(bool)
created_at = Field(lazy_datetime)
created_at = Field(datetime)
@classmethod
def create(cls, channel, max_age=86400, max_uses=0, temporary=False, unique=False):

8
disco/types/message.py

@ -7,7 +7,7 @@ from holster.enum import Enum
from disco.types.base import (
SlottedModel, Field, ListField, AutoDictField, snowflake, text,
lazy_datetime, enum
datetime, enum
)
from disco.util.snowflake import to_snowflake
from disco.util.functional import cached_property
@ -108,7 +108,7 @@ class MessageEmbed(SlottedModel):
type = Field(str, default='rich')
description = Field(text)
url = Field(text)
timestamp = Field(lazy_datetime)
timestamp = Field(datetime)
color = Field(int)
footer = Field(MessageEmbedFooter)
image = Field(MessageEmbedImage)
@ -210,8 +210,8 @@ class Message(SlottedModel):
author = Field(User)
content = Field(text)
nonce = Field(snowflake)
timestamp = Field(lazy_datetime)
edited_timestamp = Field(lazy_datetime)
timestamp = Field(datetime)
edited_timestamp = Field(datetime)
tts = Field(bool)
mention_everyone = Field(bool)
pinned = Field(bool)

Loading…
Cancel
Save