Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from CONSTANTS import * # Seperate File
- from re import compile
- import psycopg2 # used for the database
- import discord
- words = "|".join(SWEAR_WORDS)
- bad_words = compile(fr"(?P<words>{words})")
- mention = compile(r"<@(?P<id>\d+)>|(?P<name>.+)#(?P<discrim>\d{4})")
- TABLE = "full_user_to_word_count"
- conn = psycopg2.connect(DATABASE_URL)
- intents = discord.Intents.default()
- intents.members = True # Needed to get the id of a user with only the username and discriminator
- client = discord.Client(intents=intents)
- def setup():
- # This is a heavy command that updates the database to use added words in SWEAR_WORDS
- with conn.cursor() as cur:
- for word in SWEAR_WORDS:
- if not word.strip():
- continue
- cur.execute(f'ALTER TABLE {TABLE} ADD COLUMN IF NOT EXISTS {word} INTEGER DEFAULT 0;')
- conn.commit()
- def update_or_write(id, word):
- id = str(id)
- with conn.cursor() as cur:
- cur.execute(f"SELECT {word} FROM {TABLE} WHERE id=%(id)s;", {"id": id})
- try:
- res = cur.fetchall()[0][0] or 0
- except IndexError:
- res = 0
- cur.execute(f"INSERT INTO {TABLE} (id) SELECT %(id)s WHERE NOT EXISTS (SELECT 1 FROM {TABLE} WHERE id=%(id)s);", {"id": id})
- cur.execute(f"UPDATE {TABLE} SET {word}={res+1} WHERE id=%(id)s;", {"id": id})
- conn.commit()
- async def get_from_id(id, username=None):
- if username is None: # username is used to address the user in the return msg (aesthetic)
- user = await client.fetch_user(id)
- username = user.display_name
- with conn.cursor() as cur:
- cur.execute(f"SELECT * FROM {TABLE} WHERE id=%(id)s", {"id": str(id)})
- try:
- res = cur.fetchall()[0]
- except IndexError:
- res = cur.fetchall()
- msg = f"Profanity Count of **{username}**:\n"
- x = "\n".join(f"**{SWEAR_WORDS[i].title()}**: \t{count}" for i, count in enumerate(res[1:]) if count)
- if not x:
- return f"Wow, I have never detected **{username}** swearing!"
- msg += x + "\n"
- rows = len([i for i in res if i]) - 2
- if rows < 3:
- msg += f"*Wow, **{username}** must be a great person!*"
- elif rows < 7:
- msg += "*About average I would say*"
- elif rows < 10:
- msg += "*Ok getting out of hand now*"
- elif rows < 15:
- msg += "*DUDE STOP THIS ISNT A COD LOBBY*"
- else:
- msg += "*I have no words*"
- return msg
- async def get_from_username(username, discriminator):
- response = discord.utils.get(client.get_all_members(), name=username, discriminator=discriminator)
- if response is None:
- return f"I don't know **{username}** :("
- return await get_from_id(response.id)
- @client.event
- async def on_ready():
- print("Started!")
- if input("Set Up? "):
- setup()
- print("Set Up!")
- @client.event
- async def on_message(message):
- txt: str = message.content
- send = message.channel.send
- auth: discord.member.Member = message.author
- for word in bad_words.findall(f"{txt}"):
- update_or_write(auth.id, word)
- if not txt.startswith("s!") or auth == client.user:
- return
- txt = txt.lstrip("s!").lstrip()
- command = ""
- for i in txt:
- if i in " \t\r":
- break
- command = f"{command}{i}"
- txt = txt.removeprefix(command).strip()
- match command:
- case "get_from_user":
- m = mention.fullmatch(txt)
- if m is not None:
- id = m['id']
- name = m['name']
- discrim = m['discrim']
- print(id, name, discrim)
- if id is not None:
- await send(await get_from_id(id))
- elif None in [name, discrim]:
- await send("Invalid Name!")
- else:
- await send(await get_from_username(name, discrim))
- elif txt in SELF_REFERENCE:
- await send(await get_from_id(auth.id, username="you"))
- else:
- await send("Invalid User!")
- case "help":
- await send("1. **s! get_from_user [username#0000/userid]**: Get Profanity Count of a user")
- case _:
- await send(f"Command not recognized: {command}")
- client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement