Skip to content

Commit

Permalink
Make output of !ustats an embed
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjacksn committed Mar 19, 2021
1 parent cb3112c commit 880c7b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.local
__pycache__
docker-compose.override.yaml
104 changes: 55 additions & 49 deletions wormgas/cogs/rainwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,21 @@ def build_embed(song: Dict):
embed.set_author(name=channel.long_name, url=channel.url)
return embed

@staticmethod
def build_embed_ustats(user: Dict):
user_name = user.get('name')
user_id = user.get('user_id')
user_url = f'https://rainwave.cc/#!/listener/{user_id}'
user_colour = int(user.get('colour'), 16)
embed = discord.Embed(title=user_name, url=user_url, description='Rainwave user stats', colour=user_colour)
user_avatar = user.get('avatar')
embed.set_thumbnail(url=f'https://rainwave.cc{user_avatar}')
embed.add_field(name='Game', value=f'{user.get("rating_completion").get("1")}% rated', inline=True)
embed.add_field(name='Chiptune', value=f'{user.get("rating_completion").get("4")}% rated', inline=True)
embed.add_field(name='OC ReMix', value=f'{user.get("rating_completion").get("2")}% rated', inline=True)
embed.add_field(name='Covers', value=f'{user.get("rating_completion").get("3")}% rated', inline=True)
return embed

@cmds.command(name='next', aliases=['nx', 'nxgame', 'nxrw', 'nxoc', 'nxocr', 'nxcover', 'nxcovers', 'nxmw', 'nxvw',
'nxbw', 'nxch', 'nxchip', 'nxall', 'nxomni', 'nxow'])
async def next(self, ctx: cmds.Context, chan_abbr: str = None):
Expand Down Expand Up @@ -739,61 +754,52 @@ async def ustats(self, ctx: cmds.Context, *, username: str = None):
Use "!ustats [<username>]" to see some statistics about a Rainwave user.
Leave off <username> to see your own stats."""

log.info(f'username: {username!r}')
out = []
async with ctx.typing():
log.info(f'username: {username!r}')

if username is None:
listener_id = await self.get_id_for_user(ctx.author)
if listener_id is None:
await ctx.author.send(f'Use **!id add <id>** to connect your Rainwave and Discord accounts.')
return
elif username.startswith('<@') and username.endswith('>') and username[2:-1].isdigit():
member = discord.utils.get(ctx.guild.members, id=int(username[2:-1]))
username = member.display_name
listener_id = await self.get_id_for_user(member)
else:
listener_id = await self.get_id_for_name(username)

if username is None:
listener_id = await self.get_id_for_user(ctx.author)
if listener_id is None:
await ctx.author.send(f'Use **!id add <id>** to connect your Rainwave and Discord accounts.')
await ctx.author.send(f'{username} is not a valid Rainwave user.')
return
elif username.startswith('<@') and username.endswith('>') and username[2:-1].isdigit():
member = discord.utils.get(ctx.guild.members, id=int(username[2:-1]))
username = member.display_name
listener_id = await self.get_id_for_user(member)
else:
listener_id = await self.get_id_for_name(username)

if listener_id is None:
await ctx.author.send(f'{username} is not a valid Rainwave user.')
return
user_id = self.bot.config.get('rainwave:user_id')
key = self.bot.config.get('rainwave:key')
d = await self.rw_listener(user_id, key, listener_id)
embed = self.build_embed_ustats(d.get('listener'))

user_id = self.bot.config.get('rainwave:user_id')
key = self.bot.config.get('rainwave:key')
d = await self.rw_listener(user_id, key, listener_id)
cun = d.get('listener').get('name')
completion = d.get('listener').get('rating_completion')
game = int(completion.get('1', 0))
ocr = int(completion.get('2', 0))
cover = int(completion.get('3', 0))
chip = int(completion.get('4', 0))
m = f'{cun} has rated {game}% of Game, {ocr}% of OCR, {cover}% of Covers, {chip}% of Chiptune channel content.'
out.append(m)

current_channel = await self.get_current_channel_for_name(cun)
if current_channel:
m = f'{cun} is currently listening to the {current_channel.long_name}.'
out.append(m)

if not ctx.guild:
for line in out:
await ctx.send(line)
return
user_name = d.get('listener').get('name')
current_channel = await self.get_current_channel_for_name(user_name)
if current_channel:
embed.set_footer(text=f'Currently listening to the {current_channel.long_name}')

now = int(time.time())
last = int(self.bot.config.get('rainwave:ustats:last', 0))
wait = int(self.bot.config.get('rainwave:ustats:wait', 0))
if last < now - wait:
for line in out:
await ctx.send(line)
self.bot.config.set('rainwave:ustats:last', now)
else:
for line in out:
await ctx.author.send(line)
remaining = last + wait - now
cmd = ctx.invoked_with
m = f'I am cooling down. You cannot use **{cmd}** in {ctx.channel.mention} for another {remaining} seconds.'
await ctx.author.send(m)
if not ctx.guild:
await ctx.send(embed=embed)
return

now = int(time.time())
last = int(self.bot.config.get('rainwave:ustats:last', 0))
wait = int(self.bot.config.get('rainwave:ustats:wait', 0))
if last < now - wait:
await ctx.send(embed=embed)
self.bot.config.set('rainwave:ustats:last', now)
else:
await ctx.author.send(embed=embed)
remaining = last + wait - now
cmd = ctx.invoked_with
m = (f'I am cooling down. You cannot use **{cmd}** in {ctx.channel.mention} for another {remaining} '
f'seconds.')
await ctx.author.send(m)

@cmds.command(aliases=['vt'])
async def vote(self, ctx: cmds.Context, candidate: int):
Expand Down

0 comments on commit 880c7b9

Please sign in to comment.