import discord from discord.ext import commands, tasks import time class TrackTimer(): def __init__(self, duration, users, channel, author): self.end = time.time() + duration self.users = users self.channel = channel self.author = author def __str__(self): return str(self.end - time.time()) + "s remaining." def valid(self): return time.time() < (self.end) class Timer(commands.Cog): """Timer commands.""" def __init__(self, bot): self.bot = bot self.check_timers.start() self.timers = [] @tasks.loop(seconds=2.0) async def check_timers(self): for timer in self.timers: if not timer.valid(): msg = "Pencils down! " msg += timer.author.mention + " " for user in timer.users: msg += user.mention + " " await timer.channel.send(msg) self.timers.remove(timer) @commands.command(pass_context=True, hidden=False) async def timer(self, ctx, minutes: int, mentions: str): """Starts a timer for a given number of minutes. Tags mentioned users when timer expires. Always tags the user that called this function""" self.timers.append(TrackTimer(minutes*60, ctx.message.mentions, ctx.channel, ctx.author)) msg = "Okay " for user in ctx.message.mentions: msg += user.display_name + ", " msg += ctx.author.display_name + "; " msg += "your " + str(minutes) + " minutes start now." await ctx.channel.send(msg) def setup(bot): bot.add_cog(Timer(bot))