refactor to allow for fixed, random, or by time statuses

This commit is contained in:
Abigail 2019-12-23 14:06:42 -05:00
parent 9e79eececc
commit b25b5a95ff
2 changed files with 31 additions and 15 deletions

28
main.py
View File

@ -9,6 +9,7 @@ General Public License, version 2.
""" """
from datetime import time, datetime from datetime import time, datetime
import random
import discord import discord
from discord.ext import commands, tasks from discord.ext import commands, tasks
@ -115,6 +116,12 @@ async def _extctl_unload(ctx: commands.Context, extension: str):
async def on_ready(): async def on_ready():
print(f"Logged in as: {bot.user} - {bot.user.id}") print(f"Logged in as: {bot.user} - {bot.user.id}")
print("------") print("------")
if opt.status_mode == "time":
_ensure_activity_time.start()
elif opt.status_mode == "random":
_ensure_activity_random.start()
else:
_ensure_activity_fixed.start()
@bot.event @bot.event
@ -130,8 +137,8 @@ async def on_message(message):
# --- Tasks --- # --- Tasks ---
@tasks.loop(minutes=5) @tasks.loop(minutes=5)
async def _ensure_activity(): async def _ensure_activity_time():
status = opt.status_default status = opt.statuses[0]
try: try:
tz = pytz.timezone(opt.status_tz) tz = pytz.timezone(opt.status_tz)
@ -141,7 +148,7 @@ async def _ensure_activity():
now = datetime.now(tz=tz).time() now = datetime.now(tz=tz).time()
for sts in opt.statuses: for sts in opt.time_statuses:
start_time = time(hour=sts[1][0], minute=sts[1][1], tzinfo=tz) start_time = time(hour=sts[1][0], minute=sts[1][1], tzinfo=tz)
end_time = time(hour=sts[2][0], minute=sts[2][1], tzinfo=tz) end_time = time(hour=sts[2][0], minute=sts[2][1], tzinfo=tz)
if start_time < now <= end_time: if start_time < now <= end_time:
@ -149,10 +156,17 @@ async def _ensure_activity():
await bot.change_presence(activity=discord.Game(name=status)) await bot.change_presence(activity=discord.Game(name=status))
@tasks.loop(minutes=5)
async def _ensure_activity_random():
status = random.choice(opt.statuses)
@_ensure_activity.before_loop await bot.change_presence(activity=discord.Game(name=status))
async def _before_ensure_activity():
await bot.wait_until_ready() @tasks.loop(minutes=5)
async def _ensure_activity_fixed():
status = opt.statuses[0]
await bot.change_presence(activity=discord.Game(name=status))
# --- Run --- # --- Run ---
@ -160,8 +174,6 @@ async def _before_ensure_activity():
for ext in opt.exts: for ext in opt.exts:
bot.load_extension(ext_dir + '.' + ext) bot.load_extension(ext_dir + '.' + ext)
_ensure_activity.start()
try: try:
bot.run(keys.discord_token) bot.run(keys.discord_token)

View File

@ -29,16 +29,20 @@ owners_uids = (200102491231092736,)
# The extensions to load when running the bot. # The extensions to load when running the bot.
exts = ['ae7q', 'base', 'fun', 'grid', 'ham', 'image', 'lookup', 'morse', 'qrz', 'study', 'weather'] exts = ['ae7q', 'base', 'fun', 'grid', 'ham', 'image', 'lookup', 'morse', 'qrz', 'study', 'weather']
# Either "time", "random", or "fixed" (first item in statuses)
status_mode = "fixed"
# Random statuses pool
statuses = ["with lids on the air", "with fire"]
# Timezone for the status (string) # Timezone for the status (string)
status_tz = 'US/Eastern' status_tz = 'US/Eastern'
# The text to put in the "playing" status, with start and stop times # The text to put in the "playing" status, with start and stop times
statuses = [('with lids on 3.840', (00, 00), (6, 00)), time_statuses = [('with lids on 3.840', (00, 00), (6, 00)),
('with lids on 7.200', (6, 00), (10, 00)), ('with lids on 7.200', (6, 00), (10, 00)),
('with lids on 14.313', (10, 00), (18, 00)), ('with lids on 14.313', (10, 00), (18, 00)),
('with lids on 7.200', (18, 00), (20, 00)), ('with lids on 7.200', (18, 00), (20, 00)),
('with lids on 3.840', (20, 00), (23, 59))] ('with lids on 3.840', (20, 00), (23, 59))]
# The text to put in the "playing" status otherwise
status_default = 'with lids on the air'
# Emoji IDs and keywords for emoji reactions # Emoji IDs and keywords for emoji reactions
# Use the format {emoji_id (int): ('tuple', 'of', 'lowercase', 'keywords')} # Use the format {emoji_id (int): ('tuple', 'of', 'lowercase', 'keywords')}