{"id":19261943,"url":"https://github.com/hacksu/python-discord-bot","last_synced_at":"2026-05-18T07:05:15.550Z","repository":{"id":149141573,"uuid":"251169608","full_name":"hacksu/Python-Discord-Bot","owner":"hacksu","description":"This repository will walk you though making a Discord Bot in Python!","archived":false,"fork":false,"pushed_at":"2020-03-30T01:20:46.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-01-05T10:12:22.008Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hacksu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-30T01:13:47.000Z","updated_at":"2020-03-30T01:20:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a3cc2b4-b238-47ae-bed3-c011603b8a51","html_url":"https://github.com/hacksu/Python-Discord-Bot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FPython-Discord-Bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FPython-Discord-Bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FPython-Discord-Bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2FPython-Discord-Bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hacksu","download_url":"https://codeload.github.com/hacksu/Python-Discord-Bot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240364295,"owners_count":19789756,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T19:29:08.223Z","updated_at":"2026-05-18T07:05:10.511Z","avatar_url":"https://github.com/hacksu.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Python-Discord-Bot\n*Written by Joshua Behler*\n## Intro\nDiscord, for those who are unfamilar, is a free text and voice chatting platform. You will need a Discord account in order to make and interact with a bot. You can create an account free at their [website](https://discord.com). You can either use Discord through the Desktop Application or through the browser. Additionally, you will need Python 3.8 installed. You can download Python from [here](https://python.org).\n## Creating a server\nYou will need to be in a server where you have admin privilegdes. If you already have a server, great! If not, here is how to create one:\n\n1: Select the plus button on the side bar of your Discord server\n\n2: Select the \"Create a server\" button\n\n3: Name your server whatever you want, and then click \"Create\".\n\nYou should now have a server to work with!\n## Setting up Python\nIn order to create your bot, you will need to install the Discord.py module. To do so, follow these steps:\n\n1: Open up your computer's terminal. For Windows, this is Command Prompt/Powershell. For Mac and Linux, this is Linux.\n\n2: Execute the following command:\n```\npip install discord\n```\nIf all works, the Discord.py module should install and become available for use.\n## Now to create the bot\nLast step before coding; we have to tell Discord to make the bot account for us. Go to the [Discord Developer Portal](https://discordapp.com/developers/applications) and follow these steps:\n\n1: Select \"New Application\". This tutorial is going to make a bot that tells the time, so when prompted, name your application \"Time Bot\".\n\n2: When the application is created, go to the \"Bot\" tab and select \"Add Bot\" and then \"Yes, do it!\"\n\n3: Click on the \"Copy\" button that appears. This will copy the bot's Token to your clipboard, which is essentially the bot's username and password. Do not share this token with anyone else. \n\nYour bot is now created, and we can move on to the part everyone has been waiting for...\n## Coding the bot\nNow to write the bot. Launch your favorite IDE (Python IDLE is preferred) or Text Editor, and create a new file called \"bot.py\".\nThis is where we were be placing our bot's code.\n\nFirst part is to add the include statements. By default, Python can't access the Discord.py module until we import it. Add ```import discord``` to the file. Additionally, we will also need the asyncio and datetime libraries, so add ```import asyncio``` and ```import datetime``` to the file.\n\nYour file should now look like this:\n```\nimport discord\nimport asyncio\nimport datetime\n```\nNext, what we are going to do is create a class. This class will inherit from the ```discord.Client``` class we imported earlier, which will let us have our bot do custom things. Add ```class MyClient(discord.Client):``` to the file. \n\nThis class is where we will put all of our functions. The first function we are going to add is the ```on_message``` function. This function will be called every single time our bot \"sees\" a message from any user, including itself! The function to add is:\n```\nasync def on_message(self,message):\n    if(message.content.startswith(\"/\")):\n        await self.process_commands(message)\n```\nNotice the if statement. That if statement will only ever execute if the message that the bot sees begins with a slash. That way the bot will ignore normal conversation. Next up, we will write the ```process_commands``` function that ```on_message``` uses.\n\nThe ```process_commands``` function is going to be where we determine which command the user tried to execute. It's basically going to be a long multiway if statement that checks for various keywords. However, since our bot is only going to have one command, it will just be an if/else. Write this into your file:\n```\nasync def process_commands(self,message):\n    command = message.content.split()[0].lower()\n    #Command List Here\n```\nBelow the ```#Command List Here``` line is where we are going to put this if statement. Now, add:\n```\nif(command == \"/time\"):\n    await self.give_time(message)\nelse:\n    await message.channel.send(\"Invalid command: my only command is `/time`\")\n```\nSo now, whenever a user sends a message that begins with \"/time\", the bot will call the `give_time` command. Lets go write that function.\n\nYour file should now look like:\n```\nimport discord\nimport asyncio\nimport datetime\n\nclass MyClient(discord.Client):\n\n    async def on_message(self,message):\n        if(message.content.startswith(\"/\")):\n            await self.process_commands(message)\n\n    async def process_commands(self,message):\n        command = message.content.split()[0].lower()\n        #Command List Here\n        if(command == \"/time\"):\n            await self.give_time(message)\n        else:\n            await message.channel.send(\"Invalid command: my only command is `/time`\")\n```\n\nTo add the ```give_time``` function, add ```async def give_time(self,message):``` to your file. The body of the function will be the following:\n```\ncrnt_time = datetime.datetime.now()\nawait message.channel.send(\"The current time is: \" + str(crnt_time.hour)+\":\"+str(crnt_time.minute)+\":\"+str(crnt_time.second)+\"!\")\n```\nThis function will find the channel that the message was sent from, and then send its own message back as a response. We use the datetime module to get the current hour, minute, and second of the day.\n\nYour code should now look like this:\n```\nimport discord\nimport asyncio\nimport datetime\n\nclass MyClient(discord.Client):\n\n    \n    async def on_message(self,message):\n        if(message.content.startswith(\"/\")):\n            await self.process_commands(message)\n\n\n    async def process_commands(self,message):\n        command = message.content.split()[0].lower()\n        #Command List Here\n        if(command == \"/time\"):\n            await self.give_time(message)\n        else:\n            await message.channel.send(\"Invalid command: my only command is `/time`\")\n    \n    async def give_time(self,message):\n        crnt_time = datetime.datetime.now()\n        await message.channel.send(\"The current time is: \" + str(crnt_time.hour)+\":\"+str(crnt_time.minute)+\":\"+str(crnt_time.second)+\"!\")\n```\n\nWe are almost done! Next up, add the following code to your file. These functions tell the bot to print information to the log whever it a connection event happens, and also sets the bot's status to tell people how to use it:\n```\n    async def on_ready(self):\n        await self.change_presence(activity=discord.Game(name = \"/time\"))\n        print(\"Successfully set Bot's game status\")\n        \n    async def on_connect(self):\n        print(\"Bot has connected to server at time:\",datetime.datetime.now())\n    \n    async def on_disconnect(self):\n        print(\"Bot has disconnected from server at time:\",datetime.datetime.now())\n```\n\nLastly, we need to make sure the bot can log into Discord. Add the following bit of code to your file, and make sure to replace ```TOKEN``` with the token you copied from Discord Developer:\n```\nprint(\"Starting Bot\")\nbot = MyClient()\nbot.run(\"TOKEN\")\n```\nIf you have done all well, your bot should work! The last step is to invite it into your server. The completed bot code can be found in this repo, so if your bot doesn't seem to work, check there to make sure you have coded everything correctly.\n## Adding to bot to a server\nNavigate back to your bot's Application page and follow these steps:\n\n1: Go to the OAuth2 tab\n\n2: From the checklist, select \"bot\"\n\n3: Copy the URL it gives you, and paste it into your browser\n\n4: From the list of servers, select the server you want to add the bot to, and then click \"Authorize\". If prompted, solve a CAPTCHA.\n\nYou should now see the bot in the member list, but it will say it is offline. To fix that, save and then run the bot.py file you made. If the bot now appears online, you can try out your command! Type /time and see if it responds!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fpython-discord-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhacksu%2Fpython-discord-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fpython-discord-bot/lists"}