MrShandy

discord-bot-1

Jun 5th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. # -- coding: utf-8 --
  2. import asyncio
  3. from discord.ext import commands
  4. import sqlite3
  5. import time
  6.  
  7.  
  8. class DataBase:
  9. def __init__(self):
  10. self.conn = sqlite3.connect("data.db")
  11. self.c = self.conn.cursor()
  12. try:
  13. self.c.execute("CREATE TABLE blocked (word TEXT)")
  14. self.c.execute("CREATE TABLE logs (time TEXT, user TEXT, word TEXT, id INTEGER)")
  15. except sqlite3.OperationalError:
  16. pass
  17.  
  18. def add_log(self, user, word):
  19. self.c.execute(f"INSERT INTO logs VALUES(datetime('now', 'localtime'), ?,?,?)", (user.name, word, user.id))
  20. self.conn.commit()
  21.  
  22. def get_blocked(self):
  23. self.c.execute("SELECT * FROM blocked")
  24. tuple = self.c.fetchall()
  25. list = []
  26. for name in tuple:
  27. list.append(name[0])
  28. return list
  29.  
  30. def add_blocked(self, word):
  31. self.c.execute("INSERT INTO blocked VALUES(?)", (word,))
  32. self.conn.commit()
  33.  
  34.  
  35. prefix = "block "
  36. bot = commands.Bot(command_prefix=prefix)
  37. database = DataBase()
  38. block_list = ["хихи", "чпек"] #Все блоки в маленьких буквах
  39.  
  40.  
  41. @bot.event
  42. async def on_ready():
  43. global block_list
  44. global database
  45. list = database.get_blocked()
  46. block_list += list
  47.  
  48.  
  49. @bot.event
  50. async def on_message(message):
  51. if message.author == bot.user:
  52. return
  53. global database
  54. global block_list
  55. for word in block_list:
  56. if word in message.content.lower():
  57. word = word.lower()
  58. message1 = await message.channel.send(f"<@{message.author.id}>, вы использовали в сообщениии запрещенное слово '{word}'. ")
  59. print(message.author, word)
  60. database.add_log(message.author, word)
  61. await message.delete()
  62. await asyncio.sleep(3) # Тут время сна
  63. await message1.delete()
  64. return
  65. await bot.process_commands(message)
  66.  
  67.  
  68. @bot.command()
  69. async def add(ctx, word):
  70. if ctx.author.id != 335464992079872000:
  71. return
  72. global block_list
  73. global database
  74. block_list.append(word)
  75. database.add_blocked(word)
  76.  
  77. bot.run('тут крч токен, но вам я его не дам')
Add Comment
Please, Sign In to add comment