9
9
10
10
from cogs .Scheduler import Job
11
11
12
-
13
12
logger = logging .getLogger (__name__ )
14
13
15
14
@@ -28,13 +27,11 @@ def __str__(self):
28
27
def __eq__ (self , other ):
29
28
if not isinstance (other , Idol ):
30
29
return NotImplemented
31
- return str .lower (self .group ) == str .lower (other .group ) and str .lower (self .name ) == str .lower (other .name )
30
+ return str .lower (self .group ) == str .lower (other .group ) and str .lower (
31
+ self .name ) == str .lower (other .name )
32
32
33
33
def to_dict (self ):
34
- return {
35
- 'group' : self .group ,
36
- 'name' : self .name
37
- }
34
+ return {'group' : self .group , 'name' : self .name }
38
35
39
36
@staticmethod
40
37
def from_dict (source ):
@@ -45,47 +42,65 @@ class BiasOfTheWeek(commands.Cog):
45
42
def __init__ (self , bot ):
46
43
self .bot = bot
47
44
self .nominations = {}
48
- self .nominations_collection = self .bot .config ['biasoftheweek' ]['nominations_collection' ]
45
+ self .nominations_collection = self .bot .config ['biasoftheweek' ][
46
+ 'nominations_collection' ]
47
+
48
+ if self .bot .loop .is_running ():
49
+ asyncio .create_task (self ._ainit ())
50
+ else :
51
+ self .bot .loop .run_until_complete (self ._ainit ())
52
+
53
+ async def _ainit (self ):
54
+ _nominations = await self .bot .db .get (self .nominations_collection )
49
55
50
- _nominations = self .bot .database .get (self .nominations_collection )
51
56
for nomination in _nominations :
52
- self .nominations [self .bot .get_user (int (nomination .id ))] = Idol .from_dict (nomination .to_dict ())
57
+ self .nominations [self .bot .get_user (int (
58
+ nomination .id ))] = Idol .from_dict (nomination .to_dict ())
53
59
54
- logger .info (f'Initial nominations from database : { self .nominations } ' )
60
+ logger .info (f'Initial nominations from db : { self .nominations } ' )
55
61
56
62
@staticmethod
57
63
def reaction_check (reaction , user , author , prompt_msg ):
58
64
return user == author and str (reaction .emoji ) in [CHECK_EMOJI , CROSS_EMOJI ] and \
59
65
reaction .message .id == prompt_msg .id
60
66
61
67
@commands .command ()
62
- async def nominate (self , ctx , group : commands .clean_content , name : commands .clean_content ):
68
+ async def nominate (self , ctx , group : commands .clean_content ,
69
+ name : commands .clean_content ):
63
70
idol = Idol (group , name )
64
71
65
72
if idol in self .nominations .values ():
66
- await ctx .send (f'**{ idol } ** has already been nominated. Please nominate someone else.' )
73
+ await ctx .send (
74
+ f'**{ idol } ** has already been nominated. Please nominate someone else.'
75
+ )
67
76
elif ctx .author in self .nominations .keys ():
68
77
old_idol = self .nominations [ctx .author ]
69
- prompt_msg = await ctx .send (f'Your current nomination is **{ old_idol } **. Do you want to override it?' )
78
+ prompt_msg = await ctx .send (
79
+ f'Your current nomination is **{ old_idol } **. Do you want to override it?'
80
+ )
70
81
await prompt_msg .add_reaction (CHECK_EMOJI )
71
82
await prompt_msg .add_reaction (CROSS_EMOJI )
72
83
try :
73
- reaction , user = await self .bot .wait_for ('reaction_add' , timeout = 60.0 ,
74
- check = lambda reaction , user : self . reaction_check ( reaction ,
75
- user ,
76
- ctx . author ,
77
- prompt_msg ))
84
+ reaction , user = await self .bot .wait_for (
85
+ 'reaction_add' ,
86
+ timeout = 60.0 ,
87
+ check = lambda reaction , user : self . reaction_check (
88
+ reaction , user , ctx . author , prompt_msg ))
78
89
except asyncio .TimeoutError :
79
90
pass
80
91
else :
81
92
await prompt_msg .delete ()
82
93
if reaction .emoji == CHECK_EMOJI :
83
94
self .nominations [ctx .author ] = idol
84
- self .bot .database .set (self .nominations_collection , str (ctx .author .id ), idol .to_dict ())
85
- await ctx .send (f'{ ctx .author } nominates **{ idol } ** instead of **{ old_idol } **.' )
95
+ await self .bot .db .set (self .nominations_collection ,
96
+ str (ctx .author .id ), idol .to_dict ())
97
+ await ctx .send (
98
+ f'{ ctx .author } nominates **{ idol } ** instead of **{ old_idol } **.'
99
+ )
86
100
else :
87
101
self .nominations [ctx .author ] = idol
88
- self .bot .database .set (self .nominations_collection , str (ctx .author .id ), idol .to_dict ())
102
+ await self .bot .db .set (self .nominations_collection ,
103
+ str (ctx .author .id ), idol .to_dict ())
89
104
await ctx .send (f'{ ctx .author } nominates **{ idol } **.' )
90
105
91
106
@nominate .error
@@ -96,7 +111,7 @@ async def nominate_error(self, ctx, error):
96
111
@commands .has_permissions (administrator = True )
97
112
async def clear_nominations (self , ctx ):
98
113
self .nominations = {}
99
- self .bot .database .delete (self .nominations_collection )
114
+ await self .bot .db .delete (self .nominations_collection )
100
115
await ctx .message .add_reaction (CHECK_EMOJI )
101
116
102
117
@commands .command ()
@@ -110,23 +125,43 @@ async def nominations(self, ctx):
110
125
else :
111
126
await ctx .send ('So far, no idols have been nominated.' )
112
127
128
+ @commands .command ()
129
+ async def db_noms (self , ctx ):
130
+ embed = discord .Embed (title = 'Bias of the Week nominations' )
131
+ nominations = {}
132
+ _nominations = await self .bot .db .get (self .nominations_collection )
133
+ for nomination in _nominations :
134
+ nominations [self .bot .get_user (int (
135
+ nomination .id ))] = Idol .from_dict (nomination .to_dict ())
136
+
137
+ for key , value in nominations .items ():
138
+ embed .add_field (name = key , value = value )
139
+
140
+ await ctx .send (embed = embed )
141
+
113
142
@commands .command (name = 'pickwinner' )
114
143
@commands .has_permissions (administrator = True )
115
- async def pick_winner (self , ctx , silent : bool = False , fast_assign : bool = False ):
144
+ async def pick_winner (self ,
145
+ ctx ,
146
+ silent : bool = False ,
147
+ fast_assign : bool = False ):
116
148
member , pick = random .choice (list (self .nominations .items ()))
117
149
118
150
# Assign BotW winner role on next wednesday at 00:00 UTC
119
151
now = pendulum .now ('Europe/London' )
120
- assign_date = now .add (seconds = 120 ) if fast_assign else now . next (
121
- pendulum .WEDNESDAY )
152
+ assign_date = now .add (
153
+ seconds = 120 ) if fast_assign else now . next ( pendulum .WEDNESDAY )
122
154
123
155
await ctx .send (
124
156
f"""Bias of the Week ({ now .week_of_year } -{ now .year } ): { member if silent else member .mention } \' s pick **{ pick } **.
125
- You will be assigned the role *{ self .bot .config ['biasoftheweek' ]['winner_role_name' ]} * at { assign_date .to_cookie_string ()} .""" )
157
+ You will be assigned the role *{ self .bot .config ['biasoftheweek' ]['winner_role_name' ]} * at { assign_date .to_cookie_string ()} ."""
158
+ )
126
159
127
160
scheduler = self .bot .get_cog ('Scheduler' )
128
161
if scheduler is not None :
129
- await scheduler .add_job (Job ('assign_winner_role' , [ctx .guild .id , member .id ], assign_date .float_timestamp ))
162
+ await scheduler .add_job (
163
+ Job ('assign_winner_role' , [ctx .guild .id , member .id ],
164
+ assign_date .float_timestamp ))
130
165
131
166
@pick_winner .error
132
167
async def pick_winner_error (self , ctx , error ):
0 commit comments