113 lines
3.7 KiB
Python
113 lines
3.7 KiB
Python
from typing import Final
|
|
import os
|
|
import re
|
|
|
|
from discord.ext import commands
|
|
from dotenv import load_dotenv
|
|
from discord import Intents, Client, Message, User, Guild
|
|
|
|
load_dotenv()
|
|
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
|
|
REGEXP1: Final[str] = '<[a-z:0-9]*><[a-z:0-9]*>(.*)\(http.*\)(\(.*\)) `(.*)` (through|by) <[a-z:0-9]*><[a-z:0-9]*>(.*)\(http.*\)(\(.*\))'
|
|
REGEXP2: Final[str] = '<[@0-9]*>.*\|`(.*)` (climbed|dropped) from (\d+) to (\d+). payout in `(.*)`'
|
|
tracker_channel: int = 1218161058599141386
|
|
sniper_channel: int = 1218138955137155193
|
|
sentinel_channel: int = 1256995195954790410
|
|
|
|
intents: Intents = Intents.default()
|
|
intents.message_content = True # NOQA
|
|
client: Client = Client(intents=intents)
|
|
|
|
|
|
def clean_message(last_message: str, previous_message: str) -> str:
|
|
striker = False
|
|
striker_po = False
|
|
struck = False
|
|
struck_po = False
|
|
previous_rank = False
|
|
new_rank = False
|
|
n = 0
|
|
m = re.search(REGEXP2, last_message)
|
|
if m and len(m.groups()) == 5:
|
|
if m.group(2) == 'climbed':
|
|
striker = m.group(1)
|
|
previous_rank = int(m.group(3))
|
|
new_rank = int(m.group(4))
|
|
striker_po = m.group(5)
|
|
n = 1
|
|
elif m.group(2) == 'dropped':
|
|
struck = m.group(1)
|
|
previous_rank = int(m.group(4))
|
|
new_rank = int(m.group(3))
|
|
struck_po = m.group(5)
|
|
n = 2
|
|
m = re.search(REGEXP2, previous_message)
|
|
if m and len(m.groups()) == 5:
|
|
if m.group(2) == 'climbed':
|
|
if int(m.group(3)) == previous_rank and int(m.group(4)) == new_rank:
|
|
striker = m.group(1)
|
|
striker_po = m.group(5)
|
|
if n == 2:
|
|
n = 3
|
|
elif m.group(2) == 'dropped':
|
|
if int(m.group(3)) == new_rank and int(m.group(4)) == previous_rank:
|
|
struck = m.group(1)
|
|
struck_po = m.group(5)
|
|
if n == 1:
|
|
n = 3
|
|
if n == 3:
|
|
return striker + '(' + striker_po + ') ' + str(previous_rank) + '->' + str(new_rank) + ' ' + struck + '(' + struck_po + ')'
|
|
|
|
|
|
@client.event
|
|
async def on_ready() -> None:
|
|
print('Sentinel ready !', flush=True)
|
|
|
|
|
|
@client.event
|
|
async def on_message(message: Message) -> None:
|
|
|
|
if message.author == client.user:
|
|
# to avoid infinite loop, let's not answer to messages we (client.user) emit ourselves
|
|
print('no answer to our own message', flush=True)
|
|
return
|
|
if message.channel.id != tracker_channel:
|
|
print('message received on an unmonitored channel, ignoring', flush=True)
|
|
return
|
|
|
|
print(f'received {message}')
|
|
|
|
message_content: str = message.content
|
|
if not message_content and message.embeds:
|
|
print('try to get embedded information as message content')
|
|
for embed in message.embeds:
|
|
message_content = embed.description
|
|
if not message_content:
|
|
print('no message content, ignoring', flush=True)
|
|
return
|
|
# if 'Kriss' not in message_content:
|
|
# print('message not implying "Kriss"', flush=True)
|
|
# return
|
|
|
|
channel = client.get_channel(tracker_channel)
|
|
previous_message = [msg async for msg in channel.history(limit=2)][1]
|
|
|
|
print(f'previous message content: {previous_message.content}')
|
|
print(f'received message content: {message_content}')
|
|
new_message_content = clean_message(message_content, previous_message.content)
|
|
print(f'transformed message content: {new_message_content}')
|
|
|
|
try:
|
|
channel = client.get_channel(sentinel_channel)
|
|
await channel.send(new_message_content)
|
|
except Exception as e:
|
|
print(e, flush=True)
|
|
|
|
|
|
def main() -> None:
|
|
client.run(token=TOKEN)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|