Initial version
This commit is contained in:
@@ -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"])
|
||||||
@@ -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))
|
||||||
Reference in New Issue
Block a user