Skip to content

Commit aa48b2c

Browse files
committed
fix issue #26
1 parent 18e5cff commit aa48b2c

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

protobot.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import asyncio #
1818
import activity #
1919
import string #
20+
import schedule #
2021
#
2122
#####################################
2223

@@ -51,15 +52,15 @@ async def on_connect():
5152
Sets the bot activity to ACTIVITY.
5253
"""
5354

54-
logging.warning(f'{bot.user.name} {VERSION} has sucessfully connected to Discord.')
55+
logging.warning(f'{bot.user.name} {VERSION} has successfully connected to Discord.')
5556
await bot.change_presence(activity = ACTIVITY)
5657

5758

5859
@bot.event
5960
async def on_ready():
6061
"""
6162
Prints a list of guilds the bot is connected to when the bot is finished processing
62-
date from Discord servers. Also calls the initizaling functions in activity.py.
63+
date from Discord servers. Also calls the initializing functions in activity.py.
6364
"""
6465

6566
logging.info('Bot loading complete. Current guilds: ')
@@ -73,6 +74,15 @@ async def on_ready():
7374
for guild in bot.guilds:
7475
add_all_users_from_guild_to_database(guild)
7576

77+
logging.info("Scheduling events...")
78+
logging.info("Daily events")
79+
schedule.every().day.at("00:00").do(run_once_every_day)
80+
logging.info("Hourly events")
81+
schedule.every().hour.do(run_once_every_hour)
82+
logging.info("Minutely events")
83+
schedule.every().minute.do(run_once_every_minute)
84+
logging.info("Scheduling complete!")
85+
7686

7787
@bot.event
7888
async def on_disconnect():
@@ -105,7 +115,7 @@ async def on_member_join(member):
105115
welcome_message = (
106116
f"Hi {member.name}, welcome to Lounge server. Please set your "
107117
"nickname to match the naming scheme used on the server. For example, if "
108-
"my name was John, my nickname would be \"Protobot | John\". Please also "
118+
"my name was John, my nickname would be \"ProtoBot | John\". Please also "
109119
"make sure to read any messages pinned in the #important channel."
110120
)
111121

@@ -283,42 +293,33 @@ async def about(ctx):
283293
await ctx.message.channel.send(f"ProtoBot {VERSION}. Source code and bug tracker: https://github.com/klercke/ProtoBot")
284294

285295

286-
async def run_once_every_day():
296+
def run_once_every_day():
287297
"""
288-
Runs a block of code every day sometime between 00:00 and 01:00 local time.
298+
Runs a block of code every day.
289299
"""
290300

291-
if (int(time.strftime('%H', time.localtime())) < 1):
292-
# This code will run if it is the correct time
293-
logging.info("Running nightly operations.")
294-
else:
295-
logging.debug("Attempted to run daily event out of defined hours.")
301+
logging.info("Running nightly operations.")
302+
pass
296303

297304

298-
async def run_once_every_minute():
305+
306+
def run_once_every_minute():
299307
"""
300-
Runs a block of code every minute
308+
Runs a block of code every minute.
301309
"""
302310

303-
await asyncio.sleep(60)
304-
305311
# Give every user in a voice channel points
306312
for guild in bot.guilds:
307313
for channel in guild.voice_channels:
308314
for user in channel.members:
309315
change_user_score(user.id, POINTS_PER_MINUTE_TALKING)
310316

311317

312-
async def run_once_every_hour():
318+
def run_once_every_hour():
313319
"""
314-
Runs a block of code every hour
320+
Runs a block of code every hour.
315321
"""
316322

317-
await asyncio.sleep(3600)
318-
319-
# Call the once-each-day function so it can do its check
320-
await run_once_every_day()
321-
322323
activity.change_all_scores(-POINT_DECAY_PER_HOUR)
323324

324325

@@ -371,9 +372,6 @@ def main():
371372
]
372373
)
373374

374-
bot.loop.create_task(run_once_every_minute())
375-
bot.loop.create_task(run_once_every_hour())
376-
377375
bot.run(TOKEN)
378376

379377
if __name__ == "__main__":

0 commit comments

Comments
 (0)