Advertisement
yasi04

Untitled

Mar 18th, 2024 (edited)
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.28 KB | None | 0 0
  1. import disnake
  2. import asyncio
  3. import sqlite3
  4. from disnake.ext import commands
  5.  
  6.  
  7. bot = commands.Bot(command_prefix="!", help_command=None, intents=disnake.Intents.all())
  8.  
  9. conn = sqlite3.connect('filters.db')
  10. c = conn.cursor()
  11.  
  12. c.execute('''CREATE TABLE IF NOT EXISTS text_filters
  13.             (channel_id INTEGER PRIMARY KEY, filters TEXT)''')
  14.  
  15. c.execute('''CREATE TABLE IF NOT EXISTS link_filters
  16.             (channel_id INTEGER PRIMARY KEY, filters TEXT)''')
  17.  
  18. conn.commit()
  19.  
  20.  
  21. @bot.event
  22. async def on_ready():
  23.     print(f"Bot {bot.user} is ready to work!")
  24.  
  25.  
  26. @bot.event
  27. async def on_message(message):
  28.     if message.author == message.guild.owner:
  29.         return
  30.     if message.author == bot.user:
  31.         await asyncio.sleep(120)
  32.         await message.delete()
  33.         return
  34.  
  35.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (message.channel.id,))
  36.     row = c.fetchone()
  37.     if row:
  38.         filters = row[0].split(';')
  39.         if message.content.startswith("http://") or message.content.startswith("https://"):
  40.             flag = True
  41.             for word in filters:
  42.                 if word == message.content.split('/')[2]:
  43.                     flag = False
  44.                     return
  45.             if flag:
  46.                 await message.delete()
  47.                 await message.channel.send(f"*В этом канале доступны только* `{filters}`, *Другое не разрешено XD*")
  48.                 return
  49.  
  50.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (message.channel.id,))
  51.     row = c.fetchone()
  52.     if row:
  53.         text_filters = row[0].split(';')
  54.         flag = True
  55.         for word in text_filters:
  56.             if word == message.content:
  57.                 flag = False
  58.                 return
  59.         if flag:
  60.             await message.delete()
  61.             await message.channel.send(f"*В этом канале доступны только* `{text_filters}`, *Другое не разрешено XD*")
  62.             return
  63.  
  64.     await bot.process_commands(message)
  65.  
  66.  
  67. @bot.slash_command(description="добавить текстовый фильтр")
  68. async def add_text_filter(ctx, channel_id: str, filter_text: str):
  69.     channel_id = int(channel_id)
  70.  
  71.     if ctx.author != ctx.guild.owner:
  72.         await ctx.send('Команда только для создателя')
  73.         return
  74.  
  75.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (channel_id,))
  76.     row = c.fetchone()
  77.     if row:
  78.         old_filters = row[0]
  79.         new_filters = old_filters + ";" + filter_text
  80.         c.execute("UPDATE text_filters SET filters = ? WHERE channel_id = ?", (new_filters, channel_id))
  81.         conn.commit()
  82.         await ctx.send('Готово')
  83.     else:
  84.         c.execute("INSERT INTO text_filters (channel_id, filters) VALUES (?, ?)", (channel_id, filter_text))
  85.         conn.commit()
  86.         await ctx.send('Готово')
  87.  
  88.  
  89. @bot.slash_command(description="добавить ссылку в фильтр")
  90. async def add_link_filter(ctx, channel_id: str, filter_link: str):
  91.     channel_id = int(channel_id)
  92.  
  93.     if ctx.author != ctx.guild.owner:
  94.         await ctx.send('Команда только для создателя')
  95.         return
  96.  
  97.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (channel_id,))
  98.     row = c.fetchone()
  99.     if row:
  100.         old_filters = row[0]
  101.         new_filters = old_filters + ";" + filter_link
  102.         c.execute("UPDATE link_filters SET filters = ? WHERE channel_id = ?", (new_filters, channel_id))
  103.         conn.commit()
  104.         await ctx.send('Готово')
  105.     else:
  106.         c.execute("INSERT INTO link_filters (channel_id, filters) VALUES (?, ?)", (channel_id, filter_link))
  107.         conn.commit()
  108.         await ctx.send('Готово')
  109.  
  110.  
  111. @bot.slash_command(description="удалить текстовый фильтр (выбор)")
  112. async def delete_filter(ctx, channel_id: str, filters: str):
  113.     channel_id = int(channel_id)
  114.  
  115.     if ctx.author != ctx.guild.owner:
  116.         await ctx.send('Команда только для создателя')
  117.         return
  118.  
  119.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (channel_id,))
  120.     row = c.fetchone()
  121.     if row:
  122.         text_filters = row[0].split(';')
  123.         new_text_filters = [filter_word for filter_word in text_filters if filter_word not in filters.split(';')]
  124.         new_text_filters_str = ';'.join(new_text_filters)
  125.         c.execute("UPDATE text_filters SET filters = ? WHERE channel_id = ?", (new_text_filters_str, channel_id))
  126.         conn.commit()
  127.  
  128.         await ctx.send('Готово')
  129.     else:
  130.         await ctx.send('Фильтров для этого канала и не было')
  131.  
  132.  
  133. @bot.slash_command(description="удалить фильтр ссылок (выбор)")
  134. async def delete_link_filter(ctx, channel_id: str, filters: str):
  135.     channel_id = int(channel_id)
  136.  
  137.     if ctx.author != ctx.guild.owner:
  138.         await ctx.send('Команда только для создателя')
  139.         return
  140.  
  141.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (channel_id,))
  142.     row = c.fetchone()
  143.     if row:
  144.         link_filters = row[0].split(';')
  145.         new_link_filters = [filter_link for filter_link in link_filters if filter_link not in filters.split(';')]
  146.         new_link_filters_str = ';'.join(new_link_filters)
  147.         c.execute("UPDATE link_filters SET filters = ? WHERE channel_id = ?", (new_link_filters_str, channel_id))
  148.         conn.commit()
  149.  
  150.         await ctx.send('Готово')
  151.     else:
  152.         await ctx.send('Фильтров для этого канала и не было')
  153.  
  154.  
  155. @bot.slash_command(description="удалить все фильтры для канала")
  156. async def delete_all_filters(ctx, channel_id: str):
  157.     channel_id = int(channel_id)
  158.  
  159.     if ctx.author != ctx.guild.owner:
  160.         await ctx.send('Команда только для создателя')
  161.         return
  162.  
  163.     c.execute("DELETE FROM text_filters WHERE channel_id = ?", (channel_id,))
  164.     c.execute("DELETE FROM link_filters WHERE channel_id = ?", (channel_id,))
  165.     conn.commit()
  166.  
  167.     await ctx.send('Готово')
  168.  
  169.  
  170. bot.run('MTIxOTM0MTkyMTIzNTg5ODQwOA.GgunC8.owRezpOKB9gzlO9E_aRKHyrt9GcnY17qU5s_Xg')
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement