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..cadd7b1 --- /dev/null +++ b/cogs/README.md @@ -0,0 +1,7 @@ +# 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 would be called `auth_cog.py` and the class should be named `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..e71c4c4 --- /dev/null +++ b/main.py @@ -0,0 +1,27 @@ +import os +from discord.ext import commands +from dotenv import load_dotenv +import asyncio + +from cogs.basics_cog import BasicsCog +from cogs.inspire_cog import InspireCog + +BOT = commands.Bot(command_prefix='$') + +@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!" + + +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 + +if __name__ == "__main__": + asyncio.run(main) 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