From d64a80a9633c96b09019dc5af05d9cc0186a713a Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Wed, 28 Sep 2022 15:42:04 +1000 Subject: [PATCH 1/7] refactor --- .gitignore | 3 ++- Bot/assistant_bot.py | 57 -------------------------------------------- cogs/README.md | 10 ++++++++ cogs/basics_cog.py | 10 ++++++++ cogs/inspire_cog.py | 41 +++++++++++++++++++++++++++++++ main.py | 20 ++++++++++++++++ 6 files changed, 83 insertions(+), 58 deletions(-) delete mode 100644 Bot/assistant_bot.py create mode 100644 cogs/README.md create mode 100644 cogs/basics_cog.py create mode 100644 cogs/inspire_cog.py create mode 100644 main.py diff --git a/.gitignore b/.gitignore index 2eea525..8661818 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +.vscode diff --git a/Bot/assistant_bot.py b/Bot/assistant_bot.py deleted file mode 100644 index fc03943..0000000 --- a/Bot/assistant_bot.py +++ /dev/null @@ -1,57 +0,0 @@ -import discord -import os -import requests -import json -import random - -# instance of the client -Intents = discord.Intents.default() -# Intents.message_content = True -client = discord.Client(intents=Intents) - -sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"] - -starter_encouragements = [ - "Cheer up!", - "You can do it!", - "Hang in there!", - "You are a star!", - "You are a great person!" -] - -def get_quote(): - """ - This function gets a random quote from the API - """ - url = "https://zenquotes.io/api/random" - response = requests.get(url) - json_data = json.loads(response.text) - quote = json_data[0]['q'] + " - " + json_data[0]['a'] - return quote - -@client.event -async def on_ready(): # when the bot is ready to use - print(f'We have logged in as {client.user}') - # "Hey there! I'm your personal assistant. I can help you with anything you need. Just ask me!" - -@client.event # this tells the library we are listening to a message event -async def on_message(message): # when the bot recieves a message - if message.author == client.user: # if the message is from the bot (ourselves) - return # we don't want to reply to ourselves - - if message.content.startswith('!hello'): # if the message starts with !hello - await message.channel.send(f'Hello humans!') # reply with Hello! - # await message.author.send('Hello there!') # send a message to the author - - if message.content.startswith('!inspire'): # if the message starts with !inspire - author_name = message.author.name.split()[0] # get the author's name - quote = get_quote() - # await message.channel.send(quote) - await message.reply(author_name + ", " + quote) - - if any(word in message.content for word in sad_words): - await message.channel.send(random.choice(starter_encouragements)) - # message.channel.send("I'm sorry to hear that. I'm always here to help!") - -Token = os.getenv('AUTH_TOKEN') -client.run(Token) # run the bot with the token \ No newline at end of file diff --git a/cogs/README.md b/cogs/README.md new file mode 100644 index 0000000..9d91d51 --- /dev/null +++ b/cogs/README.md @@ -0,0 +1,10 @@ +# Cogs + +Functionality of the Code Network bot is organised into cogs, discord.py's way of separating concerns within the codebase. Information about cogs can be found here: https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html. + +## Naming convention + +The file name and cog name should be suffixed with "cog", so for example if my cog was concerned with authentication, the file name and cog name should be + +- `auth_cog.py` +- `AuthCog` \ No newline at end of file diff --git a/cogs/basics_cog.py b/cogs/basics_cog.py new file mode 100644 index 0000000..a8b4b13 --- /dev/null +++ b/cogs/basics_cog.py @@ -0,0 +1,10 @@ +from discord.ext import commands + + +class BasicsCog(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + async def hello(self, ctx: commands.Context): + await ctx.channel.send(f"Hello humans!") # reply with Hello! + # await message.author.send('Hello there!') # send a message to the author diff --git a/cogs/inspire_cog.py b/cogs/inspire_cog.py new file mode 100644 index 0000000..5321017 --- /dev/null +++ b/cogs/inspire_cog.py @@ -0,0 +1,41 @@ +from discord.ext import commands +import requests +import random + +SAD_WORDS = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"] + +STARTER_ENCOURAGEMENTS = [ + "Cheer up!", + "You can do it!", + "Hang in there!", + "You are a star!", + "You are a great person!", +] + +def get_quote() -> str: + """ + This function gets a random quote from the API + """ + url = "https://zenquotes.io/api/random" + response = requests.get(url) + json_data = response.json() + quote = json_data[0]["q"] + " - " + json_data[0]["a"] + return quote + + +class InspireCog(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.command() + async def inspire(self, ctx: commands.Context): + author_name = ctx.author.name.split()[0] # get the author's name + quote = get_quote() + # await message.channel.send(quote) + await ctx.reply(author_name + ", " + quote) + + @commands.Cog.listener() + async def sad_reply(self, ctx: commands.Context): + if any(word in ctx.message.content.lower() for word in SAD_WORDS): + await ctx.channel.send(random.choice(STARTER_ENCOURAGEMENTS)) + # message.channel.send("I'm sorry to hear that. I'm always here to help!") diff --git a/main.py b/main.py new file mode 100644 index 0000000..a45011d --- /dev/null +++ b/main.py @@ -0,0 +1,20 @@ +import os +from discord.ext import commands +from dotenv import load_dotenv +import asyncio + +BOT = commands.Bot() + +@BOT.event +async def on_ready(): # when the bot is ready to use + print(f"We have logged in as {BOT.user}") + # "Hey there! I'm your personal assistant. I can help you with anything you need. Just ask me!" + +async def main(): + async with BOT: + load_dotenv() + token = os.getenv("AUTH_TOKEN") + await BOT.start(token) # run the bot with the token + +if __name__ == "__main__": + asyncio.run(main) From 2f04d1cf3ef1b5c96b913d228d054fbcc79b5f6f Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Wed, 28 Sep 2022 15:43:01 +1000 Subject: [PATCH 2/7] . --- cogs/README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cogs/README.md b/cogs/README.md index 9d91d51..cadd7b1 100644 --- a/cogs/README.md +++ b/cogs/README.md @@ -4,7 +4,4 @@ Functionality of the Code Network bot is organised into cogs, discord.py's way o ## Naming convention -The file name and cog name should be suffixed with "cog", so for example if my cog was concerned with authentication, the file name and cog name should be - -- `auth_cog.py` -- `AuthCog` \ No newline at end of file +The file name and cog name should be suffixed with "cog", so for example if my cog was concerned with authentication, the file would be called `auth_cog.py` and the class should be named `AuthCog`. \ No newline at end of file From ce0c838818c9e58555334a377d640f2a162b079f Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Wed, 28 Sep 2022 15:50:03 +1000 Subject: [PATCH 3/7] . --- main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.py b/main.py index a45011d..0081b6c 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,9 @@ from dotenv import load_dotenv import asyncio +from cogs.basics_cog import BasicsCog +from cogs.inspire_cog import InspireCog + BOT = commands.Bot() @BOT.event @@ -16,5 +19,9 @@ async def main(): token = os.getenv("AUTH_TOKEN") await BOT.start(token) # run the bot with the token +BOT.add_cog(BasicsCog(BOT)) +BOT.add_cog(InspireCog(BOT)) + + if __name__ == "__main__": asyncio.run(main) From f1a6b58c76b59fc9f93d1f43d8ef644818cf5943 Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Wed, 28 Sep 2022 15:51:49 +1000 Subject: [PATCH 4/7] . --- requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..046f18a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests==2.28.1 +discord.py==2.0.1 +python-dotenv==0.21.0 From e5b0b4f3c5994f0b42fa2f5216c74cada11f69db Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Wed, 28 Sep 2022 15:52:37 +1000 Subject: [PATCH 5/7] . --- main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/main.py b/main.py index 0081b6c..3daeaff 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,5 @@ async def main(): BOT.add_cog(BasicsCog(BOT)) BOT.add_cog(InspireCog(BOT)) - if __name__ == "__main__": asyncio.run(main) From 1792eff4f589fef8d004417e35ad14fe674285a1 Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Mon, 3 Oct 2022 11:20:36 +1000 Subject: [PATCH 6/7] add command prefix --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 3daeaff..c050cfc 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from cogs.basics_cog import BasicsCog from cogs.inspire_cog import InspireCog -BOT = commands.Bot() +BOT = commands.Bot(command_prefix='$') @BOT.event async def on_ready(): # when the bot is ready to use From d9d00cb21ec7d3f3e83031b4631f436359007d27 Mon Sep 17 00:00:00 2001 From: Joshua Inglis Date: Mon, 3 Oct 2022 11:21:44 +1000 Subject: [PATCH 7/7] reorder --- main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index c050cfc..e71c4c4 100644 --- a/main.py +++ b/main.py @@ -13,14 +13,15 @@ async def on_ready(): # when the bot is ready to use print(f"We have logged in as {BOT.user}") # "Hey there! I'm your personal assistant. I can help you with anything you need. Just ask me!" + +BOT.add_cog(BasicsCog(BOT)) +BOT.add_cog(InspireCog(BOT)) + async def main(): async with BOT: load_dotenv() token = os.getenv("AUTH_TOKEN") await BOT.start(token) # run the bot with the token -BOT.add_cog(BasicsCog(BOT)) -BOT.add_cog(InspireCog(BOT)) - if __name__ == "__main__": asyncio.run(main)