-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathWhosPlaying.py
105 lines (91 loc) · 3.44 KB
/
WhosPlaying.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import discord, random, operator, json
from discord.ext import commands
class WhosPlaying(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(no_pm=True)
async def whosplaying(self, ctx, *, game):
if len(game) <= 1:
await ctx.send(
"```The game should be at least 2 characters long...```",
delete_after=5.0,
)
return
guild = ctx.message.guild
members = guild.members
playing_game = ""
count_playing = 0
for member in members:
if not member:
continue
if not member.activity or not member.activity.name:
continue
if member.bot:
continue
if game.lower() in member.activity.name.lower():
count_playing += 1
if count_playing <= 15:
emote = random.choice(
[
":trident:",
":high_brightness:",
":low_brightness:",
":beginner:",
":diamond_shape_with_a_dot_inside:",
]
)
playing_game += f"{emote} {member.name} ({member.activity.name})\n"
if playing_game == "":
await ctx.send(
"```Search results:\nNo users are currently playing that game.```"
)
else:
msg = playing_game
if count_playing > 15:
showing = "(Showing 15/{})".format(count_playing)
else:
showing = "({})".format(count_playing)
em = discord.Embed(description=msg, colour=discord.Colour(value=0x36393E))
await ctx.send(embed=em)
@commands.command(no_pm=True)
async def currentgames(self, ctx):
"""Shows the most played games right now"""
guild = ctx.message.guild
members = guild.members
freq_list = {}
for member in members:
if not member:
continue
if not member.activity or not member.activity.name:
continue
if member.bot:
continue
if member.activity.name not in freq_list:
freq_list[member.activity.name] = 0
freq_list[member.activity.name] += 1
sorted_list = sorted(
freq_list.items(), key=operator.itemgetter(1), reverse=True
)
if not freq_list:
await ctx.send(
"```Search results:\nNo users are currently playing any games. Odd...```"
)
else:
# Create display and embed
msg = ""
max_games = min(len(sorted_list), 10)
em = discord.Embed(description=msg, colour=discord.Colour(value=0x36393E))
for i in range(max_games):
game, freq = sorted_list[i]
if int(freq_list[game]) < 2:
amount = "1 person"
else:
amount = f"{int(freq_list[game])} people"
em.add_field(name=game, value=amount)
em.set_thumbnail(url=guild.icon.url)
em.set_footer(
text=f"Do {ctx.prefix}whosplaying <game> to see whos playing a specific game"
)
await ctx.send(embed=em)
def setup(bot):
bot.add_cog(WhosPlaying(bot))