1+ import discord
2+ import asyncio
3+ # nur nötig wenn connect hier in der selben datei ist
4+ # import contextlib
5+
6+ from discord import InputTextStyle
7+ from discord .ext .commands import MissingPermissions
8+ from discord .ext import commands , tasks
9+ from discord .ui import Button , View , Modal , InputText
10+ from discord .commands import Option , slash_command , SlashCommandGroup
11+
12+ # from utils.funcs import get_config
13+ # from utils.funcs import connect
14+ # from utils.funcs import get_database_tables
15+
16+ ### {list(get_database_tables())[5]} das ist "counting"
17+ # connect ist:
18+ # @contextlib.asynccontextmanager
19+ # async def connect():
20+ # conn = await aiomysql.connect(
21+ # host=get_config("mysql")["host"],
22+ # user=get_config("mysql")["user"],
23+ # password=get_config("mysql")["password"],
24+ # db=get_config("mysql")["database"],
25+ # autocommit=True
26+ # )
27+ # async with conn.cursor() as cur:
28+ # yield conn, cur
29+ # conn.close()
30+ #
31+ # Datenbank structure
32+ # "guild_id": "BIGINT",
33+ # "counting_channel_id": "BIGINT",
34+ # "score": "BIGINT",
35+ # "highscore": "BIGINT",
36+ # "last_user": "BIGINT",
37+ # "fail_role": "BIGINT",
38+ # "only_numbers": "BOOLEAN"
39+
40+
41+ class counting (commands .Cog ):
42+ def __init__ (self , bot ):
43+ self .bot = bot
44+
45+
46+ counting_cmd = SlashCommandGroup (name = "counting" , description = "All counting commands" , guild_only = True )
47+
48+ @counting_cmd .command ()
49+ async def setchannel (self , ctx , channel : Option (discord .abc .GuildChannel , "Choose a Welcome Channel" )):
50+ async with connect () as (conn , cur ):
51+ await cur .execute (f"SELECT counting_channel_id FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { ctx .guild .id } " )
52+ result_channel = await cur .fetchone ()
53+ if not result_channel :
54+ await cur .execute (f"INSERT INTO `counting`(`guild_id`, `counting_channel_id`, `score`, `highscore`, `last_user`, `only_numbers`) VALUES ('{ ctx .guild .id } ','{ channel .id } ','1','0','0','0')" )
55+ else :
56+ await cur .execute (f"UPDATE `counting` SET `counting_channel_id`='{ channel .id } ' WHERE guild_id = { ctx .guild .id } " )
57+ return await ctx .respond (f"Der Counting-Channel wurde auf { channel .mention } Gesetzt" , ephemeral = True )
58+
59+ @counting_cmd .command ()
60+ async def next (self , ctx ):
61+ async with connect () as (conn , cur ):
62+ await cur .execute (f"SELECT counting_channel_id FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { ctx .guild .id } " )
63+ result_channel = await cur .fetchone ()
64+ if not result_channel :
65+ return await ctx .respond (f"Nutze vorher /counting setchannel" , ephemeral = True )
66+ await cur .execute (f"SELECT score FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { ctx .guild .id } " )
67+ next_int = await cur .fetchone ()
68+ await cur .execute (f"SELECT last_user FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { ctx .guild .id } " )
69+ next_user = await cur .fetchone ()
70+ if next_user [0 ] == 0 :
71+ return await ctx .respond (f"Die nächste Zahl ist { next_int [0 ]} !" , ephemeral = True )
72+ else :
73+ return await ctx .respond (f"Die nächste Zahl ist { next_int [0 ]} und der User <@{ next_user [0 ]} > darf nicht als nächstes Zählen!" , ephemeral = True )
74+
75+ @commands .Cog .listener ()
76+ async def on_message (self , message ):
77+ async with connect () as (conn , cur ):
78+ if message .author .bot :
79+ return
80+ await cur .execute (f"SELECT counting_channel_id FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { message .guild .id } " )
81+ result_channel = await cur .fetchone ()
82+ if not result_channel :
83+ return
84+ if message .channel .id == result_channel [0 ]:
85+ if message .author .bot :
86+ return
87+ await cur .execute (f"SELECT only_numbers FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { message .guild .id } " )
88+ num = await cur .fetchone ()
89+ if num :
90+ if not message .content .isnumeric ():
91+ await asyncio .sleep (2 )
92+ return await message .delete ()
93+ if not message .content .isnumeric ():
94+ return
95+ await cur .execute (f"SELECT last_user FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { message .guild .id } " )
96+ last_user = await cur .fetchone ()
97+ if message .author .id == int (last_user [0 ]):
98+ await cur .execute (f"UPDATE { list (get_database_tables ())[5 ]} SET score = %s, last_user = %s WHERE guild_id = %s" , ("1" , "0" , message .guild .id ))
99+ await message .add_reaction ("❌" )
100+ channel = self .bot .get_channel (result_channel [0 ])
101+ if channel :
102+ return await message .channel .send (F"{ message .author .mention } hat die Reihe unterbrochen, da er versucht hat 2 mal hinter einander zu Zählen" )
103+ else :
104+ await cur .execute (f"SELECT score FROM { list (get_database_tables ())[5 ]} WHERE guild_id = { message .guild .id } " )
105+ score_result = await cur .fetchone ()
106+ if message .content == str (score_result [0 ]):
107+ count = score_result [0 ] + 1
108+ await cur .execute (f"UPDATE { list (get_database_tables ())[5 ]} SET score = %s, last_user = %s WHERE guild_id = %s" , (count , message .author .id , message .guild .id ))
109+ if score_result [0 ] < count :
110+ await cur .execute (f"UPDATE { list (get_database_tables ())[5 ]} SET highscore = %s WHERE guild_id = %s" , (message .content , message .guild .id ))
111+ return await message .add_reaction ("✅" )
112+ else :
113+ await cur .execute (f"UPDATE { list (get_database_tables ())[5 ]} SET score = %s, last_user = %s WHERE guild_id = %s" , ("1" , "0" , message .guild .id ))
114+ return await message .add_reaction ("❌" )
115+ else :
116+ return
117+
118+ def setup (bot ):
119+ bot .add_cog (counting (bot ))
0 commit comments