14
14
from discord .ext import commands #
15
15
import logging #
16
16
import time #
17
+ import asyncio #
18
+ import activity #
17
19
#
18
20
#####################################
19
21
@@ -64,7 +66,7 @@ async def on_connect():
64
66
async def on_ready ():
65
67
"""
66
68
Prints a list of guilds the bot is connected to when the bot is finished processing
67
- date from Discord servers.
69
+ date from Discord servers. Also calls the initizaling functions in activity.py.
68
70
"""
69
71
70
72
logging .info ('Bot loading complete. Current guilds: ' )
@@ -73,6 +75,11 @@ async def on_ready():
73
75
label = guild .name + " (" + str (guild .id ) + ")"
74
76
logging .info (label )
75
77
78
+ activity .readDatabase ()
79
+
80
+ for guild in bot .guilds :
81
+ addAllUsersFromGuildToDatabase (guild )
82
+
76
83
77
84
@bot .event
78
85
async def on_disconnect ():
@@ -82,21 +89,40 @@ async def on_disconnect():
82
89
83
90
logging .warning ('Lost connection to Discord.' )
84
91
92
+ @bot .event
93
+ async def on_guild_join (guild ):
94
+ """
95
+ Logs a message when bot joins a new guild.
96
+ """
97
+
98
+ logging .warning (f"Joined new guild: { guild .name + ' (' + str (guild .id ) + ')' } " )
85
99
86
100
87
101
@bot .event
88
102
async def on_member_join (member ):
89
103
"""
90
- Direct-messages a user whenever the join the server
104
+ Direct-messages a user whenever the join a server
91
105
"""
92
106
93
107
await member .create_dm ()
94
- await member .dm_channel .send (
95
- f"Hi { member .name } , welcome to Konnor's Discord server. Please set your "
96
- "nickname to match the naming scheme used on the server. For example, if "
97
- "my name was John, my nickname would be \" Protobot | John\" . Please also "
98
- "make sure to read any messages pinned in the #important channel."
99
- )
108
+ if (member .guild .id == 150717946333888514 ):
109
+ welcome_message = (
110
+ f"Hi { member .name } , welcome to Konnor's Discord server. Please set your "
111
+ "nickname to match the naming scheme used on the server. For example, if "
112
+ "my name was John, my nickname would be \" Protobot | John\" . Please also "
113
+ "make sure to read any messages pinned in the #important channel."
114
+ )
115
+
116
+ elif (member .guild .id == 720996920939642912 ):
117
+ welcome_message = f"Welcome to the testing server, { member .name } !"
118
+
119
+ else :
120
+ welcome_message = f"Welcome to { member .guild .name } , { member .name } !"
121
+
122
+ await member .dm_channel .send (welcome_message )
123
+
124
+
125
+
100
126
101
127
102
128
@bot .event
@@ -124,7 +150,6 @@ async def on_message(message):
124
150
125
151
return
126
152
127
-
128
153
elif 'happy birthday' in message .content .lower ():
129
154
"""
130
155
Lets the bot say happy birthday whenever a user says it
@@ -136,7 +161,6 @@ async def on_message(message):
136
161
for recipient in mentions :
137
162
await message .channel .send (f"Happy Birthday <@{ recipient .id } >! 🎈🎉🎂" )
138
163
139
-
140
164
elif 'im' in message .content .lower () or 'i\' m' in message .content .lower () or 'i am' in message .content .lower ():
141
165
"""
142
166
Lets the bot tell the famous "Hi x! I'm dad!" joke
@@ -157,7 +181,41 @@ async def on_message(message):
157
181
await message .channel .send (response )
158
182
break
159
183
160
-
161
184
await bot .process_commands (message )
162
185
186
+
187
+ async def run_once_per_day ():
188
+ """
189
+ Runs a block of code every day sometime between 00:00 and 01:00 local time.
190
+ """
191
+
192
+ await bot .wait_until_ready ()
193
+
194
+ if (int (time .strftime ('%H' , time .localtime ())) < 1 ):
195
+ # This code will run if it is the correct time
196
+ logging .info ("Running nightly operations." )
197
+ else :
198
+ logging .debug ("Attempted to run daily event out of defined hours." )
199
+
200
+ # Check every hour
201
+ await asyncio .sleep (3600 )
202
+
203
+
204
+ def addAllUsersFromGuildToDatabase (guild ):
205
+ for user in guild .members :
206
+ if (not user .bot ):
207
+ addUserToDatabase (user .id , user .name )
208
+
209
+ activity .writeDatabase ()
210
+
211
+
212
+ def addUserToDatabase (uuid , name , score = 0 , allowmoderator = True , rankexempt = False ):
213
+ if (not uuid in activity .USERS .keys ()):
214
+ activity .addUser (uuid , name , score , allowmoderator , rankexempt )
215
+ logging .info (f"Registered new user { name } ({ uuid } ) to database." )
216
+
217
+ activity .writeDatabase ()
218
+
219
+
220
+ bot .loop .create_task (run_once_per_day ())
163
221
bot .run (TOKEN )
0 commit comments