Skip to content

Commit

Permalink
Add "after" command to Discord bot
Browse files Browse the repository at this point in the history
  • Loading branch information
contested-space committed May 23, 2024
1 parent e82285e commit c5443e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions discord/bot2024.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ async def on_message(message):
elif text.startswith('in'):
await rc.command_in(message)
return
elif text.startswith('after'):
await rc.command_after(message)
return
if author.id == ADMIN:
mat = patmoji.search(message.content)
if mat is None:
Expand Down
51 changes: 50 additions & 1 deletion discord/role_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from discord.utils import get
from asyncio import sleep

import discord
import time

import sys
assert sys.version_info >= (3, 10)

def get_delay(s):
if len(s) == 2:
return 4.0
Expand All @@ -22,7 +28,7 @@ async def command_in(message):
# They said something that wasn't a number, so ignore it
return
if hours <= 0 or hours > 24:
await channel.send('Time parameter must be between 0 and 24 (hours)')
await message.channel.send('Time parameter must be between 0 and 24 (hours)')
return
author = message.author
channel = message.channel
Expand All @@ -34,6 +40,10 @@ async def command_in(message):
seeker_note = 'All opponent seekers:\n{}'.format(
'\n'.join([m.mention for m in seekers])
)

ts = int(time.time())
await channel.send(f"<t:{ts}>")

await channel.send(
'For the next {} hours, {} is {}\n\n{}'.format(
hours,
Expand All @@ -55,6 +65,45 @@ async def command_in(message):
)
)

async def command_after(message):
current_ts = int(time.time())
author = message.author
channel = message.channel

match message.content.split():
case ["after", wait_duration_string, "for", available_duration_string]:
wait_duration_seconds = int(float(wait_duration_string) * 3600)
available_duration_seconds = int(float(available_duration_string) * 3600)

if wait_duration_seconds > 24 * 7 * 3600 or wait_duration_seconds < 0:
await channel.send('The availability period must be scheduled between 0 and 168 hours (one week) from now.')
return

if available_duration_seconds > 24 * 3600 or available_duration_seconds < 0:
await channel.send('The availability period must be between 0 and 24 hours.')
return

start_time_ts = current_ts + wait_duration_seconds
end_time_ts = start_time_ts + available_duration_seconds

await channel.send(f'At <t:{start_time_ts}> {author.mention} will be seeking an opponent until <t:{end_time_ts}>')
await sleep(wait_duration_seconds)

role = await member_role_set(author,'Seeking opponent',True)
await channel.send(f'{author.mention} is now {role.mention} until <t:{end_time_ts}>')

await sleep(available_duration_seconds)

r = get(author.roles,name='Seeking opponent')
if r is None:
# They no longer have the role and must have canceled early
return
role = await member_role_set(author,'Seeking opponent',False)
await channel.send(f'Time expired. {author.mention} is no longer seeking an opponent.')

case _:
await channel.send("syntax: `after <nb_hours> for <nb_hours>`")

async def member_role_set(member,role_name,value):
guild = member.guild
role = get(guild.roles,name=role_name)
Expand Down

0 comments on commit c5443e2

Please sign in to comment.