Skip to content

Commit a98137e

Browse files
authored
Merge pull request #1 from EBG-PW/dev
Added Polls for Clash Tournaments
2 parents 823d0f3 + 3ab914b commit a98137e

2 files changed

Lines changed: 206 additions & 3 deletions

File tree

main.py

Lines changed: 203 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,223 @@
44
from asyncpg.pool import Pool
55
from dotenv import load_dotenv
66
from discord.ext import commands
7-
from discord import activity
7+
8+
from riotwatcher import LolWatcher, ApiError
9+
import pandas as pd
10+
import discord
11+
from discord import reaction
12+
from datetime import date
13+
import locale
14+
815

916
import asyncio
1017
import asyncpg
1118
import re
1219

1320
logging.basicConfig(level=logging.INFO)
1421

22+
# Config aus .env einlesen.
1523
load_dotenv()
1624
TOKEN = os.getenv('DISCORD_BOT_TOKEN')
1725
GUILD = os.getenv('DISCORD_GUILD')
26+
my_region = os.getenv('myregion')
27+
api_key = os.getenv('RIOTAPI')
1828

29+
# Variablen assignen
30+
watcher = LolWatcher(api_key)
1931
pool: Pool = "eule"
2032

2133
async def main():
2234
global pool
2335
pool = await asyncpg.create_pool(user=os.getenv('DB_USER'), password=os.getenv('DB_PW'),
2436
database=os.getenv('DB_NAME'), host=os.getenv('DB_HOST'),
2537
port=os.getenv('DB_PORT'))
26-
bot = commands.Bot(command_prefix='!', description="COOLER BOT", case_insensitive=True)
38+
39+
bot = commands.Bot(command_prefix='!', description="COOLER BOT", case_insensitive=True, )
40+
client = discord.Client()
41+
42+
@bot.command(name='clash')
43+
@commands.has_any_role('Admin', 'Social Media Manager')
44+
async def getclash(ctx, inputtime1, inputtime2):
45+
time1 = re.findall(r"^(?:[0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$", inputtime1)
46+
time2 = re.findall(r"^(?:[0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$", inputtime2)
47+
48+
tournaments = watcher.clash.tournaments(my_region)
49+
50+
for tournament in tournaments:
51+
async with pool.acquire() as conn:
52+
row = await conn.fetchrow('SELECT * FROM clashdata WHERE id = $1', tournament['id'])
53+
if row is None:
54+
async with pool.acquire() as conn:
55+
await conn.execute('INSERT INTO clashdata VALUES ($1, $2, $3, $4, $5, $6)', tournament['id'],
56+
tournament['nameKey'], tournament['nameKeySecondary'],
57+
tournament['schedule'][0]['registrationTime'],
58+
tournament['schedule'][0]['startTime'], tournament['schedule'][0]['cancelled'])
59+
60+
async with pool.acquire() as conn:
61+
rows = await conn.fetch(
62+
'SELECT * FROM clashdata WHERE "announced" = False ORDER BY "registrationTime" ASC')
63+
64+
await ctx.send("**Clashanmeldung**")
65+
66+
for row in rows:
67+
epochtime = row['registrationTime']
68+
d = date.fromtimestamp(epochtime / 1000)
69+
locale.setlocale(locale.LC_TIME, "de-DE")
70+
day = d.strftime('%A %d %b')
71+
message = await ctx.send(":one: " + day + " " + time1[0] + "\n" +
72+
":two: " + day + " " + time2[0])
73+
await message.add_reaction('1️⃣')
74+
await message.add_reaction('2️⃣')
75+
76+
async with pool.acquire() as conn:
77+
await conn.execute('UPDATE clashdata SET "announced" = True, "announceMessageID" = $1 '
78+
'WHERE id = $2', message.id, row['id'])
79+
80+
@bot.command(name='endclash')
81+
@commands.has_any_role('Admin', 'Social Media Manager')
82+
async def endclash(ctx):
83+
async with pool.acquire() as conn:
84+
events = await conn.fetch("SELECT * FROM clashdata WHERE announced = True AND ended = False "
85+
'ORDER BY "registrationTime" ASC')
86+
87+
if events is not None:
88+
for event in events:
89+
async with pool.acquire() as conn:
90+
await conn.execute("UPDATE clashdata SET ended = True WHERE id = $1", event['id'])
91+
await conn.execute("DELETE FROM clashplayerdata")
92+
93+
epochtime = event['registrationTime']
94+
d = date.fromtimestamp(epochtime / 1000)
95+
locale.setlocale(locale.LC_TIME, "de-DE")
96+
day = d.strftime('%A %d %b')
97+
98+
await ctx.send("Clash Event vom " + day + " gelöscht")
99+
100+
@bot.command(name='clashstream')
101+
@commands.has_any_role('Social Media Manager')
102+
async def clashstream(ctx):
103+
async with pool.acquire() as conn:
104+
events = await conn.fetch('SELECT * FROM clashdata WHERE announced = True AND ended = False '
105+
'ORDER BY "registrationTime" ASC')
106+
107+
await ctx.send("**NUR FÜR STT STREAMER RELEVANT**")
108+
109+
for event in events:
110+
epochtime = event['registrationTime']
111+
d = date.fromtimestamp(epochtime / 1000)
112+
locale.setlocale(locale.LC_TIME, "de-DE")
113+
day = d.strftime('%A %d %b')
114+
115+
message = await ctx.send("CLASH STREAM " + day)
116+
await message.add_reaction('🔴')
117+
118+
async with pool.acquire() as conn:
119+
await conn.execute('UPDATE clashdata SET "streamMessageID" = $1 '
120+
'WHERE announced = True AND ended = False', message.id)
121+
122+
async def newreaction(reaction):
123+
async with pool.acquire() as conn:
124+
event = await conn.fetchrow('SELECT * FROM clashdata WHERE "announceMessageID" = $1',
125+
reaction.message_id)
126+
player = await conn.fetchrow('SELECT "regnum" FROM playerdata WHERE "idplayer" = $1', reaction.user_id)
127+
stream = await conn.fetchrow('SELECT * FROM clashdata WHERE "streamMessageID" = $1', reaction.message_id)
128+
registerd = await conn.fetchrow('SELECT * FROM clashplayerdata WHERE idplayer = $1', reaction.user_id)
129+
130+
if event is not None and player is not None and event['ended'] is False:
131+
132+
epochtime = event['registrationTime']
133+
d = date.fromtimestamp(epochtime / 1000)
134+
locale.setlocale(locale.LC_TIME, "de-DE")
135+
day = d.strftime('%A')
136+
137+
if registerd is not None:
138+
if reaction.emoji.name == "1️⃣":
139+
if day == 'Samstag':
140+
async with pool.acquire() as conn:
141+
await conn.execute('UPDATE clashplayerdata SET day1time1 = True WHERE idplayer = $1',
142+
reaction.user_id)
143+
elif day == 'Sonntag':
144+
async with pool.acquire() as conn:
145+
await conn.execute('UPDATE clashplayerdata SET day2time1 = True WHERE idplayer = $1',
146+
reaction.user_id)
147+
elif reaction.emoji.name == "2️⃣":
148+
if day == 'Samstag':
149+
async with pool.acquire() as conn:
150+
await conn.execute('UPDATE clashplayerdata SET day1time2 = True WHERE idplayer = $1',
151+
reaction.user_id)
152+
elif day == 'Sonntag':
153+
async with pool.acquire() as conn:
154+
await conn.execute('UPDATE clashplayerdata SET day2time2 = True WHERE idplayer = $1',
155+
reaction.user_id)
156+
157+
else:
158+
if reaction.emoji.name == "1️⃣":
159+
if day == 'Samstag':
160+
async with pool.acquire() as conn:
161+
await conn.execute('INSERT INTO clashplayerdata (idplayer, day1time1, regnum) VALUES '
162+
'($1, True, $2) ', reaction.user_id, player['regnum'])
163+
elif day == 'Sonntag':
164+
async with pool.acquire() as conn:
165+
await conn.execute('INSERT INTO clashplayerdata (idplayer, day2time1, regnum) VALUES '
166+
'($1, True, $2) ', reaction.user_id, player['regnum'])
167+
elif reaction.emoji.name == "2️⃣":
168+
if day == 'Samstag':
169+
async with pool.acquire() as conn:
170+
await conn.execute('INSERT INTO clashplayerdata (idplayer, day1time2, regnum) VALUES '
171+
'($1, True, $2) ', reaction.user_id, player['regnum'])
172+
elif day == 'Sonntag':
173+
async with pool.acquire() as conn:
174+
await conn.execute('INSERT INTO clashplayerdata (idplayer, day2time2, regnum) VALUES '
175+
'($1, True, $2) ', reaction.user_id, player['regnum'])
176+
177+
if stream is not None and player is not None and stream['ended'] is False and registerd is not None:
178+
179+
for role in reaction.member.roles:
180+
if role.name == "Streamer":
181+
async with pool.acquire() as conn:
182+
await conn.execute('UPDATE clashplayerdata SET streaming = True WHERE idplayer = $1',
183+
reaction.user_id)
184+
185+
async def removereaction(reaction):
186+
async with pool.acquire() as conn:
187+
event = await conn.fetchrow('SELECT * FROM clashdata WHERE "announceMessageID" = $1',
188+
reaction.message_id)
189+
player = await conn.fetchrow('SELECT "regnum" FROM playerdata WHERE "idplayer" = $1', reaction.user_id)
190+
stream = await conn.fetchrow('SELECT * FROM clashdata WHERE "streamMessageID" = $1', reaction.message_id)
191+
registerd = await conn.fetchrow('SELECT * FROM clashplayerdata WHERE idplayer = $1', reaction.user_id)
192+
193+
if event is not None and player is not None and event['ended'] is False:
194+
195+
epochtime = event['registrationTime']
196+
d = date.fromtimestamp(epochtime / 1000)
197+
locale.setlocale(locale.LC_TIME, "de-DE")
198+
day = d.strftime('%A')
199+
200+
if registerd is not None:
201+
if reaction.emoji.name == "1️⃣":
202+
if day == 'Samstag':
203+
async with pool.acquire() as conn:
204+
await conn.execute('UPDATE clashplayerdata SET day1time1 = False WHERE idplayer = $1',
205+
reaction.user_id)
206+
elif day == 'Sonntag':
207+
async with pool.acquire() as conn:
208+
await conn.execute('UPDATE clashplayerdata SET day2time1 = False WHERE idplayer = $1',
209+
reaction.user_id)
210+
elif reaction.emoji.name == "2️⃣":
211+
if day == 'Samstag':
212+
async with pool.acquire() as conn:
213+
await conn.execute('UPDATE clashplayerdata SET day1time2 = False WHERE idplayer = $1',
214+
reaction.user_id)
215+
elif day == 'Sonntag':
216+
async with pool.acquire() as conn:
217+
await conn.execute('UPDATE clashplayerdata SET day2time2 = False WHERE idplayer = $1',
218+
reaction.user_id)
219+
220+
if stream is not None and player is not None and stream['ended'] is False and registerd is not None:
221+
async with pool.acquire() as conn:
222+
await conn.execute('UPDATE clashplayerdata SET streaming = False WHERE idplayer = $1',
223+
reaction.user_id)
27224

28225
@bot.command(name='register', help='Registriere dich im Spielerverzeichniss')
29226
@commands.dm_only()
@@ -503,7 +700,11 @@ async def on_command_error(ctx, error):
503700
'folgende: `top, jgl, mid, bot, sup`')
504701
print(error)
505702

703+
bot.add_listener(newreaction, 'on_raw_reaction_add')
704+
bot.add_listener(removereaction, "on_raw_reaction_remove")
705+
506706
await bot.start(TOKEN)
707+
await client.run(TOKEN)
507708

508709

509710
if __name__ == "__main__":

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
python-dotenv~=0.14.0
22
discord~=1.0.1
3-
asyncpg~=0.21.0
3+
asyncpg~=0.21.0
4+
pandas~=1.1.2
5+
riotwatcher~=3.1.0

0 commit comments

Comments
 (0)