Advertisement
yasi04

Untitled

Mar 20th, 2024
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 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.     if message.author == bot.user:
  34.         await asyncio.sleep(120)
  35.         await message.delete()
  36.         return
  37.  
  38.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (message.channel.id,))
  39.     row = c.fetchone()
  40.     if row:
  41.         filters = row[0].split(';')
  42.         if message.content.startswith("http://") or message.content.startswith("https://"):
  43.             flag = True
  44.             for word in filters:
  45.                 if word == message.content.split('/')[2]:
  46.                     flag = False
  47.                     return
  48.             if flag:
  49.                 await message.delete()
  50.                 await message.channel.send(f"*В этом канале доступны только* `{filters}`, *Другое не разрешено XD*")
  51.                 return
  52.  
  53.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (message.channel.id,))
  54.     row = c.fetchone()
  55.     if row:
  56.         text_filters = row[0].split(';')
  57.         flag = True
  58.         for word in text_filters:
  59.             if message.content.startswith(word):
  60.                 flag = False
  61.                 return
  62.         if flag:
  63.             await message.delete()
  64.             await message.channel.send(f"*В этом канале доступны только* `{text_filters}`, *Другое не разрешено XD*")
  65.             return
  66.  
  67.     await bot.process_commands(message)
  68.  
  69.  
  70. @bot.slash_command(description="добавить текстовый фильтр")
  71. async def add_text_filter(ctx, channel_id: str, filter_text: str):
  72.     channel_id = int(channel_id)
  73.  
  74.     if ctx.author != ctx.guild.owner:
  75.         await ctx.send('Команда только для создателя')
  76.         return
  77.  
  78.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (channel_id,))
  79.     row = c.fetchone()
  80.     if row:
  81.         old_filters = row[0]
  82.         new_filters = old_filters + ";" + filter_text
  83.         c.execute("UPDATE text_filters SET filters = ? WHERE channel_id = ?", (new_filters, channel_id))
  84.         conn.commit()
  85.         await ctx.send('Готово')
  86.     else:
  87.         c.execute("INSERT INTO text_filters (channel_id, filters) VALUES (?, ?)", (channel_id, filter_text))
  88.         conn.commit()
  89.         await ctx.send('Готово')
  90.  
  91.  
  92. @bot.slash_command(description="добавить ссылку в фильтр")
  93. async def add_link_filter(ctx, channel_id: str, filter_link: str):
  94.     channel_id = int(channel_id)
  95.  
  96.     if ctx.author != ctx.guild.owner:
  97.         await ctx.send('Команда только для создателя')
  98.         return
  99.  
  100.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (channel_id,))
  101.     row = c.fetchone()
  102.     if row:
  103.         old_filters = row[0]
  104.         new_filters = old_filters + ";" + filter_link
  105.         c.execute("UPDATE link_filters SET filters = ? WHERE channel_id = ?", (new_filters, channel_id))
  106.         conn.commit()
  107.         await ctx.send('Готово')
  108.     else:
  109.         c.execute("INSERT INTO link_filters (channel_id, filters) VALUES (?, ?)", (channel_id, filter_link))
  110.         conn.commit()
  111.         await ctx.send('Готово')
  112.  
  113.  
  114. @bot.slash_command(description="удалить текстовый фильтр (выбор)")
  115. async def delete_filter(ctx, channel_id: str, filters: str):
  116.     channel_id = int(channel_id)
  117.  
  118.     if ctx.author != ctx.guild.owner:
  119.         await ctx.send('Команда только для создателя')
  120.         return
  121.  
  122.     c.execute("SELECT filters FROM text_filters WHERE channel_id = ?", (channel_id,))
  123.     row = c.fetchone()
  124.     if row:
  125.         text_filters = row[0].split(';')
  126.         new_text_filters = [filter_word for filter_word in text_filters if filter_word not in filters.split(';')]
  127.         new_text_filters_str = ';'.join(new_text_filters)
  128.         c.execute("UPDATE text_filters SET filters = ? WHERE channel_id = ?", (new_text_filters_str, channel_id))
  129.         conn.commit()
  130.  
  131.         await ctx.send('Готово')
  132.     else:
  133.         await ctx.send('Фильтров для этого канала и не было')
  134.  
  135.  
  136. @bot.slash_command(description="удалить фильтр ссылок (выбор)")
  137. async def delete_link_filter(ctx, channel_id: str, filters: str):
  138.     channel_id = int(channel_id)
  139.  
  140.     if ctx.author != ctx.guild.owner:
  141.         await ctx.send('Команда только для создателя')
  142.         return
  143.  
  144.     c.execute("SELECT filters FROM link_filters WHERE channel_id = ?", (channel_id,))
  145.     row = c.fetchone()
  146.     if row:
  147.         link_filters = row[0].split(';')
  148.         new_link_filters = [filter_link for filter_link in link_filters if filter_link not in filters.split(';')]
  149.         new_link_filters_str = ';'.join(new_link_filters)
  150.         c.execute("UPDATE link_filters SET filters = ? WHERE channel_id = ?", (new_link_filters_str, channel_id))
  151.         conn.commit()
  152.  
  153.         await ctx.send('Готово')
  154.     else:
  155.         await ctx.send('Фильтров для этого канала и не было')
  156.  
  157.  
  158. @bot.slash_command(description="удалить все фильтры для канала")
  159. async def delete_all_filters(ctx, channel_id: str):
  160.     channel_id = int(channel_id)
  161.  
  162.     if ctx.author != ctx.guild.owner:
  163.         await ctx.send('Команда только для создателя')
  164.         return
  165.  
  166.     c.execute("DELETE FROM text_filters WHERE channel_id = ?", (channel_id,))
  167.     c.execute("DELETE FROM link_filters WHERE channel_id = ?", (channel_id,))
  168.     conn.commit()
  169.  
  170.     await ctx.send('Готово')
  171.  
  172.  
  173. bot.run(os.getenv('TOKEN'))
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement