citememaybe

CiteMeMaybe is a Discord bot written in python that will gather all your citations from your citations channel and save them in a database.

https://github.com/namelessproj/citememaybe

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.2%) to scientific vocabulary

Keywords

citations discord discord-bot discord-py
Last synced: 6 months ago · JSON representation ·

Repository

CiteMeMaybe is a Discord bot written in python that will gather all your citations from your citations channel and save them in a database.

Basic Info
  • Host: GitHub
  • Owner: NamelessProj
  • License: mit
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 47.9 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
citations discord discord-bot discord-py
Created 11 months ago · Last pushed 10 months ago
Metadata Files
Readme Citation

README.md

Python version

licence repo size

CiteMeMaybe - 🎤 Save the legendary moments.

CiteMeMaybe automatically logs every message from a designated channel and stores it as a citation. Whether it’s hilarious, out of pocket, or pure gold—you’ll never lose a quote again.

🛠️ Features

Auto-log messages from a specified channel as citations.

Random citation generator (from anyone or specific users).

Stats commands – total citations, user-specific counts, and more.

Citation lookup by ID, in case someone really wants to deny it.

🤝 Perfect for

Friend groups who say way too much.

Servers that run on inside jokes.

Anyone who needs proof that “yes, you did say that.”

💬 Save it. Quote it. Laugh at it later.

📚 Powered by friendship and bad takes.

📊 .env

To be able to use this exemple bot, you'll need a .env file. ```env BOTTOKEN=YourBotToken GUILDID=YourServerId

PYTHON_ENV=dev

MONGOURI=mongodb+srv://:@cluster0.3xq4j.mongodb.net/?retryWrites=true&w=majority MONGODBNAME=yourdatabasename `` TheBOTTOKEN` will be used to connect the bot to your server.

The GUILD_ID will be used to get the server where the bot is connected and to get the channel where the citations are.

The PYTHON_ENV will be used to set the environment of the bot. It can be dev or prod. If you set it to dev, the bot will be only available for you. If you set it to prod, the bot will be available for everyone.

The MONGO_URI will be used to connect to your mongoDB database.

The MONGO_DB_NAME will be used to get the database name where the citations will be saved.

[!note]

How to get your Bot token ?

To get your bot token, you'll need to go to the Discord Developer Portal. You'll have to create a New Application and in the Bot page, you'll have to: - Turn on Presence Intent - Turn on Server Members Intent - Turn on Message Content Intent - Check Administartor in the Bot Permissions that way the bot can do anything. (Make sure to trust the code)

Then on the OAuth2 page, in the OAuth2 URL Generator part, check bot, copy the url and pasted it in your browser.

Once your bot on the server you wanted it to be, back on the Discrod Developer Portal, in the Bot page, you'll find a button Reset Token. Click on it and then your token will be displayed. Make sure to save it, it'll be showed one time only.

[!note]

How to get the ID of your Server ?

On Discord, you'll have to go to your user settings and in the Advanced tab. In there, you'll have to turn on the Developer Mode.

Then make a right click on the server you want the bot in and at the bottom of the context menu, you'll find the possibility to copy the ID of the server.

🤖 How to run the bot

  1. Clone the repository bash git clone https://github.com/NamelessProj/CiteMeMaybe.git
  2. Create a virtual environment bash python -m venv venv
  3. Activate the virtual environment bash # Windows venv\Scripts\activate # Linux source venv/bin/activate
  4. Install the dependencies bash pip install -r requirements.txt
  5. Create a .env file in the root of the project and add everything from the .env.example file. You can also check the 📊 .env section for more information.
  6. Run the bot bash python main.py

Everytime you want to run the bot, you have to activate the virtual environment and run the bot (step 3 and 6).

🚗 Automations

Every time a message is sent in the channel, the bot will check if the message is a citation and if it is, it will save it in the database.

When a message is deleted, the bot will check if the message is a citation and if it is, it will delete it from the database.

When a message is edited, the bot will check if the message is a citation and if it is, it will update it in the database.

❌ How to prevent messages from being saved

Before sending a message in the channel, add at the beginning of the message no-saving. It has to be in a code block.

🎮 Commands

/example

This command give you an example of how to write a citation.

/help

This command will give you a list of all the commands available.

/setup_server

This command is only available to the administrators.

This command will set up the server and the channel where it'll find the citations.

/update_database

This command is only available to the administrators.

This command will update the database with the citations from the channel.

/top

This command let you get the top users who said the most citations and who wrote the most citations. By default, it will get the top 5 users.

Parameters:

  • number: optional

/random_citation

This command let you get a random citation from the database or from a specific user.

Parameters:

  • user: optional

/get_a_citation

This command let you get a specific citation from the database using an id.

Parameters:

  • citation_id: int

/how_many

This command let you get the number of citations in the database or the number of citations from a specific user.

Parameters:

  • user: optional

/how_many_written_by

This command let you get the number of citations written by a specific user. By default, it will get the number of citations written by you.

Parameters:

  • user: optional

Owner

  • Name: Nameless Project
  • Login: NamelessProj
  • Kind: user
  • Location: Here

I'm just a regular man trying things.

Citation (citations.py)

import discord
from collections.abc import Mapping
from pymongo.synchronous.database import Database

from constants import CONSTANTS
from utils import replacing_mentions, remove_mentions, extract_mentions, extract_mentions_string


def insert_citation_to_db(db: Database[Mapping], message: discord.Message):
    """
    This function inserts the citation to the database.
    :param db: The database
    :param message: The message to process
    :return: None
    """
    # Checking if the message start with the string "`no-saving`"
    # This is used to prevent saving messages that are not citations
    if message.content.startswith(CONSTANTS["no_saving"]):
        return

    # Getting the citation ID
    citation_id = message.id

    # Getting the collection
    collection = db.get_collection('citation')

    # Checking if the citation already exists in the database
    if collection.find_one({"citation_id": citation_id}):
        return

    # Replacing mentions in the message with their names
    content = replacing_mentions(message)

    # Removing mentions from the message
    content_without_mentions = remove_mentions(message)

    # Extracting mentions from the message
    all_mentions = extract_mentions(message)

    all_mentions_string = extract_mentions_string(message) if len(all_mentions) == 0 else []

    # Preparing the citation data
    citation_data = {
        "guild_id": message.guild.id,
        "citation_id": citation_id,
        "author": {
            "name": message.author.name,
            "id": message.author.id,
            "avatar": message.author.display_avatar.url
        },
        "content": content,
        "content_without_mentions": content_without_mentions,
        "mentions": all_mentions,
        "mentions_string": all_mentions_string,
        "timestamp": message.created_at
    }

    # Inserting the citation data into the database
    collection.insert_one(citation_data)


def edit_citation_in_db(db: Database[Mapping], message: discord.Message):
    """
    This function edits the citation in the database.
    :param db: The database
    :param message: The message to process
    :return: None
    """
    # Checking if the message start with the string "`no-saving`"
    # This is used to prevent saving messages that are not citations
    if message.content.startswith(CONSTANTS["no_saving"]):
        # If the message starts with "`no-saving`", we delete the citation from the database
        delete_citation_from_db(db, message.id)
        return

    # Getting the citation ID
    citation_id = message.id

    # Getting the collection
    collection = db.get_collection('citation')

    # Replacing mentions in the message with their names
    content = replacing_mentions(message)

    # Removing mentions from the message
    content_without_mentions = remove_mentions(message)

    # Extracting mentions from the message
    all_mentions = extract_mentions(message)

    all_mentions_string = extract_mentions_string(message) if len(all_mentions) == 0 else []

    # Preparing the citation data
    citation_data = {
        "content": content,
        "content_without_mentions": content_without_mentions,
        "mentions": all_mentions,
        "mentions_string": all_mentions_string,
        "timestamp": message.created_at
    }

    print(citation_data)

    # Updating the citation data in the database
    collection.update_one({"citation_id": citation_id}, {"$set": citation_data})


def delete_citation_from_db(db: Database[Mapping], citation_id: int):
    """
    This function deletes a citation from the database.
    :param db: The database
    :param citation_id: The ID of the citation to delete
    :return: None
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Deleting the citation from the database
    collection.delete_one({"citation_id": citation_id})


def get_random_citation_from_db(db: Database[Mapping], guild_id: int):
    """
    This function gets a random citation from the database.
    :param db: The database
    :param guild_id: The ID of the guild
    :return: The citation data
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Getting a random citation from the database
    citation = collection.aggregate([
        {"$match": {"guild_id": guild_id}},
        {"$sample": {"size": 1}}
    ]).next()

    return citation


def get_random_citation_from_user(db: Database[Mapping], guild_id: int, user_id: int):
    """
    This function gets a random citation from the database for a specific user.
    :param db: The database
    :param guild_id: The ID of the guild
    :param user_id: The ID of the user
    :return: The citation data
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Checking if the user has any citations
    if not collection.count_documents({"guild_id": guild_id, "mentions.id": user_id}):
        return None

    # Getting a random citation from the database for the user
    citation = collection.aggregate([
        {"$match": {"guild_id": guild_id, "mentions.id": user_id}},
        {"$sample": {"size": 1}}
    ]).next()

    return citation


def get_citation_from_db(db: Database[Mapping], guild_id: int, citation_id: int):
    """
    This function gets a citation from the database.
    :param db: The database
    :param guild_id: The ID of the guild
    :param citation_id: The ID of the citation
    :return: The citation data
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Getting the citation from the database
    citation = collection.find_one({"guild_id": guild_id, "citation_id": citation_id})

    return citation


def get_all_citations_from_db(db: Database[Mapping], guild_id: int):
    """
    This function gets all citations from the database.
    :param db: The database
    :param guild_id: The ID of the guild
    :return: A list of all citations
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Getting all citations from the database
    citations = collection.find({"guild_id": guild_id})

    return list(citations)


def get_citation_count(db: Database[Mapping], guild_id: int, user_id: int = None, is_auther: bool = False, thousands_separators: str = " "):
    """
    This function gets the count of citations in the database or for a specific user (in mentions).
    :param db: The database
    :param guild_id: The ID of the guild
    :param user_id: The ID of the user (optional)
    :param is_auther: If True, count the citations of the author (optional)
    :param thousands_separators: The thousands separators to use (default is space)
    :return: A dictionary with the citation count and the formatted number
    """
    # Getting the collection
    collection = db.get_collection('citation')

    # Checking if the user ID is provided
    if user_id:
        # Getting the count of citations for the user
        count = collection.count_documents({"guild_id": guild_id, "mentions.id": user_id}) if not is_auther else collection.count_documents({"guild_id": guild_id, "author.id": user_id})
    else:
        # Getting the count of all citations
        count = collection.count_documents({"guild_id": guild_id})

    # Formatting the citation count with spaces instead of commas for thousands separators
    count_str = f"{count:,}".replace(",", thousands_separators)

    return {
        "number": count,
        "formated_number": count_str
    }

GitHub Events

Total
  • Push event: 8
  • Public event: 1
Last Year
  • Push event: 8
  • Public event: 1

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Dependencies

requirements.txt pypi
  • attrs *
  • discord *
  • discord.py *
  • dotenv *
  • pip *
  • pymongo *
  • python-dotenv *