-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwhoplays.py
90 lines (77 loc) · 3.05 KB
/
whoplays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import discord
from discord.ext import commands
import operator
class WhoPlays:
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True, no_pm=True)
async def whoplays(self, ctx, *, game):
"""Shows a list of all the people playing a game."""
if len(game) <= 2:
await self.bot.say("You need at least 3 characters.")
return
user = ctx.message.author
server = ctx.message.server
members = server.members
playing_game = ""
count_playing = 0
for member in members:
if not member:
continue
if not member.game or not member.game.name:
continue
if member.bot:
continue
if game.lower() in member.game.name.lower():
count_playing += 1
if count_playing <= 15:
playing_game += "▸ {} ({})\n".format(member.name,
member.game.name)
if playing_game == "":
await self.bot.say("No one is playing that game.")
else:
msg = playing_game
em = discord.Embed(description=msg, colour=user.colour)
if count_playing > 15:
showing = "(Showing 15/{})".format(count_playing)
else:
showing = "({})".format(count_playing)
text = "These are the people who are playing"
text += "{}:\n{}".format(game, showing)
em.set_author(name=text)
await self.bot.say(embed=em)
@commands.command(pass_context=True, no_pm=True)
async def cgames(self, ctx):
"""Shows the currently most played games"""
user = ctx.message.author
server = ctx.message.server
members = server.members
freq_list = {}
for member in members:
if not member:
continue
if not member.game or not member.game.name:
continue
if member.bot:
continue
if member.game.name not in freq_list:
freq_list[member.game.name] = 0
freq_list[member.game.name] += 1
sorted_list = sorted(freq_list.items(),
key=operator.itemgetter(1),
reverse=True)
if not freq_list:
await self.bot.say("Surprisingly, no one is playing anything.")
else:
# create display
msg = ""
max_games = min(len(sorted_list), 10)
for i in range(max_games):
game, freq = sorted_list[i]
msg += "▸ {}: __{}__\n".format(game, freq_list[game])
em = discord.Embed(description=msg, colour=user.colour)
em.set_author(name="These are the server's most played games at the moment:")
await self.bot.say(embed=em)
def setup(bot):
n = WhoPlays(bot)
bot.add_cog(n)