Advertisement
yasi04

Untitled

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