From ec96b714e3e3499608dc959f00b089d7cfcea883 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Mon, 29 Jun 2020 22:58:08 +0200 Subject: [PATCH] Initial version --- Bot.py | 20 ++++++++++++++++++++ Timer.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Bot.py create mode 100644 Timer.py diff --git a/Bot.py b/Bot.py new file mode 100644 index 0000000..b65f7d5 --- /dev/null +++ b/Bot.py @@ -0,0 +1,20 @@ +import asyncio +import discord +import Timer +import importlib +import configparser + +from discord.ext import commands + +bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), description='Hourglass, bot that does timers', pm_help=True) + +#Read configuration file: +config = configparser.ConfigParser() +config.read('settings.ini') +bot.add_cog(Timer.Timer(bot)) + +@bot.event +async def on_ready(): + print('Logged in as:\n{0} (ID: {0.id})'.format(bot.user)) + +bot.run(config["tokens"]["bot"]) diff --git a/Timer.py b/Timer.py new file mode 100644 index 0000000..b4b0fe9 --- /dev/null +++ b/Timer.py @@ -0,0 +1,48 @@ +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))