Browse Source

Improve HashMap.select, rebuild BasicPlugin

pull/6/head
Andrei 9 years ago
parent
commit
56df1b6d7f
  1. 4
      disco/types/guild.py
  2. 14
      disco/util/hashmap.py
  3. 95
      examples/basic_plugin.py

4
disco/types/guild.py

@ -6,7 +6,7 @@ from disco.gateway.packets import OPCode
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, snowflake, listof, dictof, lazy_datetime, text, binary, enum
from disco.types.base import SlottedModel, Field, snowflake, listof, dictof, text, binary, enum
from disco.types.user import User
from disco.types.voice import VoiceState
from disco.types.channel import Channel
@ -118,7 +118,7 @@ class GuildMember(GuildSubType):
nick = Field(text)
mute = Field(bool)
deaf = Field(bool)
joined_at = Field(lazy_datetime)
joined_at = Field(str)
roles = Field(listof(snowflake))
def get_voice_state(self):

14
disco/util/hashmap.py

@ -26,12 +26,16 @@ class HashMap(IterableUserDict):
def find_one(self, predicate):
return next(self.find(predicate), None)
def select(self, **kwargs):
def select(self, *args, **kwargs):
if kwargs:
args += [kwargs]
for obj in self.values():
for k, v in six.iteritems(kwargs):
if getattr(obj, k) != v:
continue
yield obj
for check in args:
for k, v in six.iteritems(check):
if getattr(obj, k) != v:
break
yield obj
def select_one(self, **kwargs):
return next(self.select(**kwargs), None)

95
examples/basic_plugin.py

@ -1,8 +1,3 @@
import gevent
import sys
import json
from disco import VERSION
from disco.bot import Plugin
@ -16,24 +11,6 @@ class BasicPlugin(Plugin):
def on_message_create(self, msg):
self.log.info('Message created: {}: {}'.format(msg.author, msg.content))
@Plugin.command('status', '[component]')
def on_status_command(self, event, component=None):
if component == 'state':
parts = []
parts.append('Guilds: {}'.format(len(self.state.guilds)))
parts.append('Channels: {}'.format(len(self.state.channels)))
parts.append('Users: {}'.format(len(self.state.users)))
event.msg.reply('State Information: ```\n{}\n```'.format('\n'.join(parts)))
return
event.msg.reply('Disco v{} running on Python {}.{}.{}'.format(
VERSION,
sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro,
))
@Plugin.command('echo', '<content:str...>')
def on_test_command(self, event, content):
event.msg.reply(content)
@ -43,59 +20,17 @@ class BasicPlugin(Plugin):
for i in range(count):
event.msg.reply(content)
@Plugin.command('invites')
def on_invites(self, event):
invites = event.channel.get_invites()
event.msg.reply('Channel has a total of {} invites'.format(len(invites)))
@Plugin.command('pins')
def on_pins(self, event):
pins = event.channel.get_pins()
event.msg.reply('Channel has a total of {} pins'.format(len(pins)))
@Plugin.command('channel stats')
@Plugin.command('count', group='messages')
def on_stats(self, event):
msg = event.msg.reply('Ok, one moment...')
invite_count = len(event.channel.get_invites())
pin_count = len(event.channel.get_pins())
msg_count = 0
for msgs in event.channel.messages_iter(bulk=True):
msg_count += len(msgs)
msg.edit('{} invites, {} pins, {} messages'.format(invite_count, pin_count, msg_count))
@Plugin.command('messages stack')
def on_messages_stack(self, event):
event.msg.reply('Channels: {}, messages here: ```\n{}\n```'.format(
len(self.state.messages),
'\n'.join([str(i.id) for i in self.state.messages[event.channel.id]])
))
msg.edit('{} messages'.format(msg_count))
@Plugin.command('airhorn')
def on_airhorn(self, event):
vs = event.member.get_voice_state()
if not vs:
event.msg.reply('You are not connected to voice')
return
vc = vs.channel.connect()
gevent.sleep(1)
vc.disconnect()
@Plugin.command('lol')
def on_lol(self, event):
event.msg.reply(':^)')
# event.msg.reply("{}".format(event.channel.can(event.msg.author, Permissions.MANAGE_EMOJIS)))
@Plugin.command('perms')
def on_perms(self, event):
perms = event.channel.get_permissions(event.msg.author)
event.msg.reply('```json\n{}\n```'.format(
json.dumps(perms.to_dict(), sort_keys=True, indent=2, separators=(',', ': '))
))
@Plugin.command('tag', '<name:str> [value:str]')
@Plugin.command('tag', '<name:str> [value:str...]')
def on_tag(self, event, name, value=None):
tags = self.storage.guild.ensure('tags')
@ -107,3 +42,27 @@ class BasicPlugin(Plugin):
return event.msg.reply(tags[name])
else:
return event.msg.reply('Unknown tag: `{}`'.format(name))
@Plugin.command('info', '<query:str...>')
def on_info(self, event, query):
users = list(self.state.users.select({'username': query}, {'id': query}))
if not users:
event.msg.reply("Couldn't find user for your query: `{}`".format(query))
elif len(users) > 1:
event.msg.reply('I found too many userse ({}) for your query: `{}`'.format(len(users), query))
else:
user = users[0]
parts = []
parts.append('ID: {}'.format(user.id))
parts.append('Username: {}'.format(user.username))
parts.append('Discriminator: {}'.format(user.discriminator))
if event.channel.guild:
member = event.channel.guild.get_member(user)
parts.append('Nickname: {}'.format(member.nick))
parts.append('Joined At: {}'.format(member.joined_at))
event.msg.reply('```\n{}\n```'.format(
'\n'.join(parts)
))

Loading…
Cancel
Save