22import random
33import time
44
5- import aiosqlite
65import discord
76import discord .ext
87from discord .ext import commands
8+ from sqlalchemy import delete , select
99
10+ import models
1011import utils .miscfuncs as mf
1112
1213bank = "./data/database.sqlite"
@@ -22,84 +23,63 @@ def __init__(self, client):
2223 # events
2324 @commands .Cog .listener ()
2425 async def on_ready (self ):
25- async with aiosqlite .connect (bank ) as db :
26- cursor = await db .cursor ()
27- await cursor .execute (
28- "CREATE TABLE IF NOT EXISTS messages("
29- "num INTEGER NOT NULL PRIMARY KEY,"
30- ""
31- "messageID INTEGER NOT NULL,"
32- "channelID INTEGER NOT NULL,"
33- "guildID INTEGER NOT NULL"
34- ")"
35- )
36- await cursor .execute (
37- "CREATE TABLE IF NOT EXISTS ignore("
38- "num INTEGER NOT NULL PRIMARY KEY,"
39- ""
40- "channelID INTEGER NOT NULL,"
41- "guildID INTEGER NOT NULL"
42- ")"
43- )
44- await cursor .execute (
45- "CREATE TABLE IF NOT EXISTS blacklist(num INTEGER NOT NULL PRIMARY KEY, user_id INTEGER NOT NULL, timestamp INTEGER NOT NULL)"
46- )
47- await db .commit ()
48-
4926 print ("Database ready" )
5027
5128 async def check_ignored (self , channel ):
52- async with aiosqlite .connect (bank ) as db :
53- cursor = await db .cursor ()
54- await cursor .execute (
55- "SELECT * FROM ignore WHERE channelID = ?" , (channel .id ,)
56- )
57- result = await cursor .fetchone ()
58- if result is not None :
59- return True
60- else :
61- return False
29+ async with self .client .session as session :
30+ return (
31+ await session .scalars (
32+ select (models .database .Ignore ).where (
33+ models .database .Ignore .channelID == channel .id
34+ )
35+ )
36+ ).one_or_none () is not None
6237
6338 @commands .hybrid_command ()
6439 @commands .has_permissions (manage_messages = True )
6540 async def ignore (self , ctx , channel : discord .TextChannel ):
6641 """Make Nocaro ignore a channel"""
67- async with aiosqlite .connect (bank ) as db :
68- await db .execute (
69- "INSERT INTO ignore (channelID, guildID) VALUES (?, ?)" ,
70- (channel .id , ctx .guild .id ),
71- )
72- await db .commit ()
42+ async with self .client .session as session :
43+ async with session .begin ():
44+ session .add (
45+ models .database .Ignore (channelID = channel .id , guildID = ctx .guild .id )
46+ )
7347
7448 await ctx .reply (f"Ignored { channel .mention } ." )
7549
7650 @commands .hybrid_command ()
7751 @commands .has_permissions (manage_messages = True )
7852 async def unignore (self , ctx , channel : discord .TextChannel ):
7953 """Make Nocaro not ignore a channel"""
80- async with aiosqlite .connect (bank ) as db :
81- await db .execute ("DELETE FROM ignore WHERE channelID = ?" , (channel .id ,))
82- await db .commit ()
54+ async with self .client .session as session :
55+ async with session .begin ():
56+ await session .execute (
57+ delete (models .database .Ignore ).where (
58+ models .database .Ignore .channelID == channel .id
59+ )
60+ )
8361
8462 await ctx .reply (f"Unignored { channel .mention } ." )
8563
8664 @commands .Cog .listener ()
8765 async def on_message_delete (self , message ):
88- async with aiosqlite .connect (bank ) as db :
89- cursor = await db .cursor ()
90- await cursor .execute (
91- "DELETE FROM messages WHERE messageID = ?" , (message .id ,)
92- )
93- await db .commit ()
66+ async with self .client .session as session :
67+ async with session .begin ():
68+ await session .execute (
69+ delete (models .database .Messages ).where (
70+ models .database .Messages .messageID == message .id
71+ )
72+ )
9473
9574 @commands .Cog .listener ()
9675 async def on_guild_channel_delete (self , channel ):
97- async with aiosqlite .connect (bank ) as db :
98- cursor = await db .cursor ()
99- await cursor .execute (
100- "DELETE FROM messages WHERE channelID = ?" , (channel .id ,)
101- )
102- await db .commit ()
76+ async with self .client .session as session :
77+ async with session .begin ():
78+ await session .execute (
79+ delete (models .database .Messages ).where (
80+ models .database .Messages .channelID == channel .id
81+ )
82+ )
10383
10484 @commands .hybrid_command (aliases = ["privacy" , "policy" , "pp" , "dinfo" ])
10585 async def privacypolicy (self , ctx ):
@@ -158,30 +138,34 @@ async def on_message(self, message: discord.Message):
158138 and current_time - self .nocaro_cooldowns [user_id ] < cooldown_period
159139 ):
160140 return
161- async with aiosqlite .connect (bank ) as db :
162- cursor = await db .cursor ()
163- await cursor .execute (
164- "SELECT * FROM messages WHERE channelID = ?" , (message .channel .id ,)
165- )
166- rows = await cursor .fetchall ()
167- if not rows :
168- return
141+ async with self .client .session as session :
142+ rows = (
143+ await session .scalars (
144+ select (models .database .Messages ).where (
145+ models .database .Messages .channelID == message .channel .id
146+ )
147+ )
148+ ).all ()
149+ if not rows :
150+ return
169151 for _ in range (10 ):
170152 chosen = random .choice (rows )
171153 orgchannel = await self .client .fetch_channel (
172- chosen [ 2 ]
154+ chosen . channelID
173155 ) # lookup channel
174156 try :
175157 message_to_send = await orgchannel .fetch_message (
176- chosen [ 1 ]
158+ chosen . messageID
177159 ) # lookup message
178160 except discord .NotFound :
179- async with aiosqlite .connect (bank ) as db :
180- cursor = await db .cursor ()
181- await cursor .execute (
182- "DELETE FROM messages WHERE messageID = ?" , (chosen [1 ],)
183- )
184- await db .commit ()
161+ async with self .client .session as session :
162+ async with session .begin ():
163+ await session .execute (
164+ delete (models .database .Messages ).where (
165+ models .database .Messages .messageID
166+ == chosen .messageID
167+ )
168+ )
185169 continue
186170 mentions = message_to_send .role_mentions
187171 if not any (role in mentions for role in message .guild .roles ):
@@ -192,12 +176,15 @@ async def on_message(self, message: discord.Message):
192176 ) # send that bitch
193177 self .nocaro_cooldowns [user_id ] = current_time
194178 else :
195- async with aiosqlite .connect (bank ) as db :
196- cursor = await db .cursor ()
197- await cursor .execute (
198- f"INSERT INTO messages(messageID, channelID, guildID) values({ message .id } , { message .channel .id } , { message .guild .id } )"
199- )
200- await db .commit ()
179+ async with self .client .session as session :
180+ async with session .begin ():
181+ session .add (
182+ models .database .Messages (
183+ messageID = message .id ,
184+ channelID = message .channel .id ,
185+ guildID = message .guild .id ,
186+ )
187+ )
201188 if ("https://x.com" in msg or "https://twitter.com" in msg ) and (
202189 "fixupx.com" not in msg
203190 and "fixvx.com" not in msg
@@ -222,28 +209,33 @@ async def on_message(self, message: discord.Message):
222209 @commands .max_concurrency (1 , commands .BucketType .user )
223210 async def rmessage (self , ctx ):
224211 """Send a random message."""
225- async with aiosqlite .connect (bank ) as db :
226- cursor = await db .cursor ()
227- await cursor .execute (
228- "SELECT * FROM messages WHERE channelID = ?" , (ctx .channel .id ,)
229- )
230- rows = await cursor .fetchall ()
231- if not rows :
232- return
212+ async with self .client .session as session :
213+ rows = (
214+ await session .scalars (
215+ select (models .database .Messages ).where (
216+ models .database .Messages .channelID == ctx .channel .id
217+ )
218+ )
219+ ).all ()
220+ if not rows :
221+ return
233222 for _ in range (10 ):
234223 chosen = random .choice (rows )
235- orgchannel = await self .client .fetch_channel (chosen [2 ]) # lookup channel
224+ orgchannel = await self .client .fetch_channel (
225+ chosen .channelID
226+ ) # lookup channel
236227 try :
237228 message_to_send = await orgchannel .fetch_message (
238- chosen [ 1 ]
229+ chosen . messageID
239230 ) # lookup message
240231 except discord .NotFound :
241- async with aiosqlite .connect (bank ) as db :
242- cursor = await db .cursor ()
243- await cursor .execute (
244- "DELETE FROM messages WHERE messageID = ?" , (chosen [1 ],)
245- )
246- await db .commit ()
232+ async with self .client .session as session :
233+ async with session .begin ():
234+ await session .execute (
235+ delete (models .database .Messages ).where (
236+ models .database .Messages .messageID == chosen .messageID
237+ )
238+ )
247239 continue
248240 mentions = message_to_send .role_mentions
249241 if not any (role in mentions for role in ctx .guild .roles ):
@@ -261,29 +253,34 @@ async def conversation(self, ctx, number: int = 5):
261253 """Generate a whole conversation between random users."""
262254 if number > 10 :
263255 number = 10
264- async with aiosqlite .connect (bank ) as db :
265- cursor = await db .cursor ()
266- await cursor .execute (
267- "SELECT * FROM messages WHERE channelID = ?" , (ctx .channel .id ,)
268- )
269- rows = await cursor .fetchall ()
270- if not rows :
271- return
256+ async with self .client .session as session :
257+ rows = (
258+ await session .scalars (
259+ select (models .database .Messages ).where (
260+ models .database .Messages .channelID == ctx .channel .id
261+ )
262+ )
263+ ).all ()
264+ if not rows :
265+ return
272266 for _ in range (number ):
273267 message_to_send = random .choice (rows )
274268 chosen = random .choice (rows )
275- orgchannel = await self .client .fetch_channel (chosen [2 ]) # lookup channel
269+ orgchannel = await self .client .fetch_channel (
270+ chosen .channelID
271+ ) # lookup channel
276272 try :
277273 message_to_send = await orgchannel .fetch_message (
278- chosen [ 1 ]
274+ chosen . messageID
279275 ) # lookup message
280276 except discord .NotFound :
281- async with aiosqlite .connect (bank ) as db :
282- cursor = await db .cursor ()
283- await cursor .execute (
284- "DELETE FROM messages WHERE messageID = ?" , (chosen [1 ],)
285- )
286- await db .commit ()
277+ async with self .client .session as session :
278+ async with session .begin ():
279+ await session .execute (
280+ delete (models .database .Messages ).where (
281+ models .database .Messages .messageID == chosen .messageID
282+ )
283+ )
287284 continue
288285 mentions = message_to_send .role_mentions
289286 if any (role in mentions for role in ctx .guild .roles ):
0 commit comments