├── .gitignore ├── LICENSE ├── README.md ├── Responses.py ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # Installer logs 28 | pip-log.txt 29 | pip-delete-this-directory.txt 30 | 31 | # Unit test / coverage reports 32 | htmlcov/ 33 | .tox/ 34 | .nox/ 35 | .coverage 36 | .coverage.* 37 | .cache 38 | nosetests.xml 39 | coverage.xml 40 | *.cover 41 | *.py,cover 42 | .hypothesis/ 43 | 44 | # Translations 45 | *.mo 46 | *.pot 47 | 48 | # Django stuff: 49 | *.log 50 | local_settings.py 51 | db.sqlite3 52 | 53 | # Flask stuff: 54 | instance/ 55 | .webassets-cache 56 | 57 | # Scrapy stuff: 58 | .scrapy 59 | 60 | # Sphinx documentation 61 | docs/_build/ 62 | 63 | # PyBuilder 64 | target/ 65 | 66 | # Environments 67 | .env 68 | .venv 69 | env/ 70 | venv/ 71 | ENV/ 72 | env.bak/ 73 | venv.bak/ 74 | 75 | # Spyder project settings 76 | .spyderproject 77 | .spyproject 78 | 79 | # Rope project settings 80 | .ropeproject 81 | 82 | # PyCharm 83 | .idea/ 84 | 85 | # Jupyter Notebook 86 | .ipynb_checkpoints 87 | 88 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 89 | __pypackages__/ 90 | 91 | # Celery stuff 92 | celerybeat-schedule 93 | celerybeat.pid 94 | 95 | # SageMath parsed files 96 | *.sage.py 97 | 98 | # dotenv 99 | .env 100 | 101 | # Pytest 102 | .pytest_cache/ 103 | 104 | # mypy 105 | .mypy_cache/ 106 | .dmypy.json 107 | dmypy.json 108 | 109 | # VSCode 110 | .vscode/ 111 | 112 | # Discord bot token 113 | .env 114 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Youssef JABRI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Discord Bot README 3 | 4 | 5 | ### Overview 6 | This repository contains a simple Discord bot written in Python. The bot listens for messages in a Discord server and responds based on predefined responses. It can also handle private messages. 7 | 8 | ### Features 9 | - Responds to user messages with predefined replies. 10 | - Can distinguish between public and private messages. 11 | - Simple command set including greeting, providing sample Python code, and rolling a dice. 12 | 13 | ### Prerequisites 14 | - Python 3.12.4 . 15 | - A Discord account & a bot token. 16 | 17 | ### Installation 18 | 19 | 1. **Clone the repository:** 20 | 21 | ```sh 22 | git clone https://github.com/youssefjabri/Discord-Bot-Python.git 23 | cd Discord-Bot-Python 24 | ``` 25 | 26 | 2. **Install the required packages:** 27 | 28 | ```sh 29 | pip install -r requirements.txt 30 | ``` 31 | 32 | 3. **Set up your `.env` file:** 33 | 34 | Create a `.env` file in the root directory of the project and add your Discord bot token: 35 | 36 | ```sh 37 | DISCORD_TOKEN=your_discord_bot_token_here 38 | ``` 39 | 40 | ### Usage 41 | 42 | 1. **Run the bot:** 43 | 44 | ```sh 45 | python main.py 46 | ``` 47 | 48 | 2. **Interact with the bot:** 49 | 50 | - Send a message containing "hello" and the bot will respond with "Hello there!" 51 | - Ask for a simple Python code snippet with "give me a simple code with python" and the bot will provide a basic example. 52 | - Request a dice roll with "roll dice" and the bot will simulate rolling a dice. 53 | - The bot will respond with a generic message if it doesn't understand the input. 54 | 55 | ### Files 56 | 57 | - `main.py`: Main script that sets up the Discord bot and handles message events. 58 | - `Responses.py`: Contains logic for generating responses based on user messages. 59 | - `requirements.txt`: Lists the dependencies required for the project. 60 | - `.env`: File containing environment variables, specifically the Discord bot token. 61 | 62 | ### main.py 63 | 64 | This script initializes and runs the Discord bot. It contains the following main sections: 65 | 66 | 1. **Load environment variables from `.env` file**: 67 | ```python 68 | load_dotenv() 69 | TOKEN: Final[str] = os.getenv('DISCORD_TOKEN') 70 | ``` 71 | 72 | 2. **Configure Discord intents and create the client**: 73 | ```python 74 | intents: Intents = Intents.default() 75 | intents.message_content = True 76 | client: Client = Client(intents=intents) 77 | ``` 78 | 79 | 3. **Function to send messages in response to users**: 80 | ```python 81 | async def send_message(message: Message, user_message: str) -> None: 82 | ``` 83 | 84 | 4. **Event triggered when the bot is ready**: 85 | ```python 86 | @client.event 87 | async def on_ready() -> None: 88 | ``` 89 | 90 | 5. **Event triggered with each new message**: 91 | ```python 92 | @client.event 93 | async def on_message(message: Message) -> None: 94 | ``` 95 | 96 | 6. **Run the bot**: 97 | ```python 98 | def main() -> None: 99 | client.run(TOKEN) 100 | 101 | if __name__ == '__main__': 102 | main() 103 | ``` 104 | 105 | ### Responses.py 106 | 107 | This script contains the `get_response` function which processes user input and returns appropriate responses. 108 | 109 | ```python 110 | from random import choice, randint 111 | 112 | def get_response(user_input: str) -> str: 113 | lowered: str = user_input.lower() 114 | if lowered == '': 115 | return "Well you're awfully silent" 116 | elif 'hello' in lowered: 117 | return 'Hello there!' 118 | elif 'give me a simple code with python' in lowered: 119 | return '`print("Hello World!")`' 120 | elif 'roll dice' in lowered: 121 | return f'You rolled: {randint(1, 6)}' 122 | else: 123 | return choice([ 124 | "I don't understand", 125 | "What are you talking about?", 126 | ]) 127 | ``` 128 | 129 | ### requirements.txt 130 | 131 | Lists the dependencies required for the project: 132 | 133 | ``` 134 | python-dotenv 135 | discord 136 | ``` 137 | 138 | ### .env File 139 | 140 | Stores the Discord bot token: 141 | 142 | ``` 143 | DISCORD_TOKEN=your_discord_bot_token_here 144 | ``` 145 | -------------------------------------------------------------------------------- /Responses.py: -------------------------------------------------------------------------------- 1 | from random import choice , randint 2 | 3 | def get_response(user_input:str)->str: 4 | lowered : str = user_input.lower() 5 | 6 | if lowered =='': 7 | return 'Well you\re awefully silent' 8 | elif 'hello' in lowered: 9 | return 'Hello there!' 10 | elif 'give me a simple code with python' in lowered: 11 | return f'`print(Hello World!)`' 12 | 13 | elif 'roll dice' in lowered: 14 | return f'You rolled : {randint(1,6)}' 15 | else: 16 | return choice([ 17 | "I don\'t understand", 18 | "What are you talking about?", 19 | ]) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from typing import Final 2 | import os 3 | from dotenv import load_dotenv 4 | from discord import Intents, Client, Message 5 | from Responses import get_response 6 | 7 | #1 Load environment variables from .env file 8 | load_dotenv() 9 | TOKEN: Final[str] = os.getenv('DISCORD_TOKEN') 10 | 11 | #2 Configuring Discord Intents & Creating the Client 12 | intents: Intents = Intents.default() 13 | intents.message_content = True # Enable access to message content 14 | client: Client = Client(intents=intents) 15 | 16 | #3 Function to send messages in response to users 17 | async def send_message(message: Message, user_message: str) -> None: 18 | if not user_message: 19 | print('(message was empty because intents were not enabled probably)') 20 | return 21 | 22 | # Check if the message is private 23 | if is_private := user_message[0] == '?': 24 | user_message = user_message[1:] 25 | 26 | try: 27 | # Get a response based on the user's message 28 | response: str = get_response(user_message) 29 | # Send the response as a private message or on the public channel 30 | await message.author.send(response) if is_private else await message.channel.send(response) 31 | except Exception as e: 32 | print(e) 33 | 34 | #4 Event that triggers when the bot is ready 35 | @client.event 36 | async def on_ready() -> None: 37 | print(f'{client.user} is now running') 38 | 39 | #5 Event that triggers with each new message 40 | @client.event 41 | async def on_message(message: Message) -> None: 42 | if message.author == client.user: 43 | return 44 | 45 | username: str = str(message.author) 46 | user_message: str = message.content 47 | channel: str = str(message.channel) 48 | 49 | # Show message details for debugging 50 | print(f'[{channel}] {username} : "{user_message}"') 51 | 52 | await send_message(message, user_message) 53 | 54 | #6 Run the bot 55 | def main() -> None: 56 | client.run(token=TOKEN) 57 | 58 | if __name__ == '__main__': 59 | main() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-dotenv 2 | discord --------------------------------------------------------------------------------