Repository navigation
Expand file tree
/
Copy pathamong_us_code.py
More file actions
74 lines (59 loc) · 2.54 KB
/
Copy pathamong_us_code.py
File metadata and controls
74 lines (59 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import asyncio
import discord
from discord.ext import commands
import datetime
COOLDOWN = 5
CODE_CHANNEL_ID = 753825645032767559
class AmongUs(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.last_timeStamp = datetime.datetime.utcfromtimestamp(0)
def log(self, msg : str) -> None:
"""Logs to Heroku log console.
Args:
msg (str): log message
"""
now = datetime.datetime.utcnow()
current_time_str = f"{(now.hour - 7) % 24}:{now.minute}"
print(f"[{current_time_str}] {msg}")
def valid_code(self, code : str) -> bool:
"""Returns whether the given Among Us code string is valid
Args:
code (str): code string
Returns:
bool: True is valid code string
"""
return code.isalpha() and len(code) == 6
def get_embed(self, code : str) -> discord.Embed:
"""Returns a nicely formatted embed object that we can send in a discord message.
Args:
code (str): formatted code string
Returns:
discord.Embed: Nicely formatted embed object with Among Us code
"""
embed = discord.Embed(title = "Room Code:", color = 0x00f715)
embed.add_field(name = f"**{code}**", value = "** **", inline = False)
return embed
@commands.Cog.listener()
async def on_message(self, message : discord.Message) -> None:
"""When a message containing a valid code is sent
in the designated 'Among Us code channel' the
text channel is cleared and only the nicely
formatted valid code remains in the text channel
Args:
message (discord.Message): discord message (default argument for event listener)
"""
channel = message.channel
author = message.author
if channel.id == CODE_CHANNEL_ID and self.bot.user != author:
now = datetime.datetime.utcnow()
time_difference = (now - self.last_timeStamp).total_seconds()
code = message.content.strip()
if time_difference > COOLDOWN and self.valid_code(code):
self.last_timeStamp = now
await channel.purge(limit=None)
await channel.send(embed = self.get_embed(code.upper()))
self.log(f"'{code}' was SUCCESSFULLY set by {author.display_name} / {author.name}")
else:
await message.delete()
self.log(f"'{code}' FAILED to be set by {author.display_name} / {author.name}")