1010# Built-in/Generic Imports
1111
1212# Libs
13- from discord .ext import commands
13+ import datetime
14+
15+ import discord
16+ from discord .ext import commands , tasks
1417
1518# Own modules
19+ from discord .ext .commands import BadArgument
20+
1621import koalabot
1722from koala .db import get_all_available_guild_extensions , give_guild_extension , \
1823 get_enabled_guild_extensions , remove_guild_extension
19- from .utils import new_discord_activity , list_ext_embed
24+ from . import core
25+ from .utils import list_ext_embed
2026from .log import logger
2127
2228# Constants
2329
2430# Variables
2531
2632
33+ def convert_activity_type (argument ):
34+ try :
35+ return discord .ActivityType [argument ]
36+ except KeyError :
37+ raise BadArgument ('Unknown activity type %s' % argument )
38+
39+
40+ def convert_iso_datetime (argument ):
41+ try :
42+ return datetime .datetime .fromisoformat (argument )
43+ except ValueError :
44+ raise BadArgument ('Invalid ISO format "%s", instead use the format "2020-01-01 00:00:00"' % argument )
45+
46+
47+
2748class BaseCog (commands .Cog , name = 'KoalaBot' ):
2849 """
2950 A discord.py cog with general commands useful to managers of the bot and servers
@@ -38,30 +59,97 @@ def __init__(self, bot):
3859 self ._last_member = None
3960 self .started = False
4061 self .COGS_DIR = koalabot .COGS_DIR
62+ self .current_activity = None
4163
4264 @commands .Cog .listener ()
4365 async def on_ready (self ):
4466 """
4567 Ran after all cogs have been started and bot is ready
4668 """
47- await self .bot .change_presence (activity = new_discord_activity ("playing" , f"{ koalabot .COMMAND_PREFIX } help" ))
69+ core .activity_clear_current ()
70+ await self .update_activity ()
71+ self .update_activity .start ()
4872 self .started = True
4973 logger .info ("Bot is ready." )
5074
51- @commands .command (name = "activity" , aliases = ["change_activity" ])
75+ @commands .group (name = "activity" )
76+ @commands .check (koalabot .is_owner )
77+ async def activity_group (self , ctx : commands .Context ):
78+ """
79+ Group of commands for activity functionality.
80+ :param ctx: Context of the command
81+ :return:
82+ """
83+
84+ @activity_group .command (name = "set" )
5285 @commands .check (koalabot .is_owner )
53- async def change_activity (self , ctx , new_activity , name ):
86+ async def activity_set (self , ctx , new_activity : convert_activity_type , name : str , url : str = None ):
5487 """
5588 Change the activity of the bot
5689 :param ctx: Context of the command
5790 :param new_activity: The new activity of the bot
5891 :param name: The name of the activity
92+ :param url: url for streaming
5993 """
60- if str .lower (new_activity ) in ["playing" , "watching" , "listening" , "streaming" ]:
61- await self .bot .change_presence (activity = new_discord_activity (new_activity , name ))
62- await ctx .send (f"I am now { new_activity } { name } " )
63- else :
64- await ctx .send ("That is not a valid activity, sorry!\n Try 'playing' or 'watching'" )
94+ await core .activity_set (new_activity , name , url , bot = self .bot )
95+ await ctx .send (f"I am now { new_activity .name } { name } " )
96+
97+ @activity_group .command (name = "schedule" )
98+ @commands .check (koalabot .is_owner )
99+ async def activity_schedule (self , ctx , new_activity : convert_activity_type , message : str ,
100+ start_time : convert_iso_datetime , end_time : convert_iso_datetime , url : str = None ):
101+ """
102+ Schedule an activity
103+ :param ctx: Context of the command
104+ :param new_activity: activity type (watching, playing etc.)
105+ :param message: message
106+ :param start_time: iso format start time
107+ :param end_time: iso format end time
108+ :param url: url
109+ """
110+ core .activity_schedule (new_activity , message , url , start_time , end_time )
111+ await ctx .send ("Activity saved" )
112+
113+ @activity_group .command (name = "list" )
114+ @commands .check (koalabot .is_owner )
115+ async def activity_list (self , ctx , show_all : bool = False ):
116+ """
117+ List scheduled activities
118+ :param ctx: Context of the command
119+ :param show_all: false=future activities, true=all activities
120+ """
121+ activities = core .activity_list (show_all )
122+ result = "Activities:"
123+ for activity in activities :
124+ result += "\n %s, %s, %s, %s, %s, %s" % (activity .activity_id , activity .activity_type .name ,
125+ activity .stream_url , activity .message , activity .time_start ,
126+ activity .time_end )
127+ await ctx .send (result )
128+
129+ @activity_group .command (name = "remove" )
130+ @commands .check (koalabot .is_owner )
131+ async def activity_remove (self , ctx , activity_id : int ):
132+ """
133+ Remove an existing activity
134+ :param ctx: Context of the command
135+ :param activity_id: Activity ID
136+ """
137+ activity = core .activity_remove (activity_id )
138+ result = "Removed:"
139+ result += "\n %s, %s, %s, %s, %s, %s" % (activity .activity_id , activity .activity_type .name ,
140+ activity .stream_url , activity .message , activity .time_start ,
141+ activity .time_end )
142+ await ctx .send (result )
143+
144+ @tasks .loop (seconds = 1.0 )
145+ async def update_activity (self ):
146+ """
147+ Loop for updating the activity of the bot according to scheduled activities
148+ """
149+ try :
150+ await core .activity_set_current_scheduled (self .bot )
151+ except Exception as err :
152+ logger .error ("Error in update_activity loop %s" % err , exc_info = err )
65153
66154 @commands .command ()
67155 async def ping (self , ctx ):
0 commit comments