From 6be6e96768f319887e757ca0f87f5926e85668b1 Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Mon, 31 Aug 2020 14:19:43 +0200 Subject: [PATCH] Make 'some' changes and add Dockerfile --- .dockerignore | 11 +++++++++ .gitignore | 4 +++ Dockerfile | 15 +++++++++++ aquabot.py | 52 +++++++++++++++++++++++++++++++++------ cogs/admin.py | 8 +----- cogs/anime.py | 7 +----- cogs/cog_framework.py | 4 +-- cogs/general.py | 25 ------------------- cogs/help.py | 9 +++---- cogs/meme.py | 5 ---- cogs/music.py | 2 +- cogs/reddit.py | 41 ++++++++++++++++++++++++++++++ cogs/reminder.py | 2 -- cogs/utility.py | 12 +++------ cogs/welcome.py | 7 +----- config/__init__.py | 0 config/cogs.py | 16 ------------ config/config_example.yml | 6 ----- data/media.py | 48 ------------------------------------ loadconfig.py | 29 ---------------------- loaddata.py | 3 +-- data/memes.py => memes.py | 0 requirements.txt | 2 ++ 23 files changed, 129 insertions(+), 179 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 cogs/general.py create mode 100644 cogs/reddit.py delete mode 100644 config/__init__.py delete mode 100644 config/cogs.py delete mode 100644 config/config_example.yml delete mode 100644 data/media.py delete mode 100644 loadconfig.py rename data/memes.py => memes.py (100%) diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a6642a7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +README.md +.git +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +logs +*.log +env +venv diff --git a/.gitignore b/.gitignore index 6833e9d..a0241ba 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,7 @@ __pycache__/ # generated data from logging logs/ *.log + +# venv +env/ +venv/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..423de4f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.8 + +WORKDIR /data + +COPY . /data + +ENV PREFIX +ENV TOKEN +ENV REDDIT_CLIENT_ID +ENV REDDIT_CLIENT_SECRET +ENV REDDIT_CLIENT_USERAGENT + +RUN python3 -m pip install -r requirements.txt + +CMD python3 aquabot.py --prefix=$PREFIX --token=$TOKEN --reddit_client_id=$REDDIT_CLIENT_ID --reddit_client_secret=$REDDIT_CLIENT_SECRET --reddit_client_useragent=$REDDIT_CLIENT_USERAGENT diff --git a/aquabot.py b/aquabot.py index 68fb606..ac12f5c 100644 --- a/aquabot.py +++ b/aquabot.py @@ -21,9 +21,34 @@ from datetime import datetime import platform import random import asyncio +import sys +import argparse -# IMPORTS - internal -import loadconfig +# SET VARS +parser = argparse.ArgumentParser() +parser.add_argument("--prefix", help="Command Prefix") +parser.add_argument("--token", help="Discord Bot Token") +parser.add_argument("--reddit_client_id", help="Reddit Client ID") +parser.add_argument("--reddit_client_secret", help="Reddit Client Secret") +parser.add_argument("--reddit_client_useragent", help="Reddit Client User-Agent") +args = parser.parse_args() + +if args.prefix: + prefix = args.prefix + +if args.token: + token = args.token + +if args.reddit_client_id: + reddit_client_id = args.reddit_client_id + +if args.reddit_client_secret: + reddit_client_secret = args.reddit_client_secret + +if args.reddit_client_useragent: + reddit_client_useragent = args.reddit_client_useragent + +avatar = "https://i.redd.it/0uajctrps9u41.jpg" # LOGGING logger = logging.getLogger("discord") @@ -39,11 +64,22 @@ logger.addHandler(handler) # INIT bot = commands.Bot( - command_prefix=loadconfig.__prefix__, + command_prefix=prefix, description="Holy Goddess Aqua!") -# LOAD COGS SPECIFIED IN 'config/cogs.py' -for cog in loadconfig.__cogs__: +# LOAD COGS +cogs = [ + "cogs.admin", + "cogs.anime", + "cogs.help", + "cogs.meme", + "cogs.music", + "cogs.reddit", + "cogs.utility", + "cogs.welcome" + ] + +for cog in cogs: try: bot.load_extension(cog) except Exception as e: @@ -62,14 +98,14 @@ async def on_ready(): startup = f""" Bot Name: {bot.user.name} - {bot.user.id}\n Owner: {bot.AppInfo.owner}\n - Command Prefix: {loadconfig.__prefix__}\n + Command Prefix: {prefix}\n discord.py Version: {discord.__version__}\n python Version: {platform.python_version()}\n OS: {platform.system()} {platform.release()} {platform.version()}\n """ print(startup) - name = f"with water | {loadconfig.__prefix__}aquabot" + name = f"with water | {prefix}aquabot" activity = discord.Activity(name=name, type=discord.ActivityType.playing) await bot.change_presence(activity=activity) @@ -77,6 +113,6 @@ async def on_ready(): # START BOT bot.run( - loadconfig.__token__, + token, bot=True, reconnect=True) diff --git a/cogs/admin.py b/cogs/admin.py index f526a37..083b681 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -1,11 +1,5 @@ """ -Admin commands, that can be used from within the chat: - - load - - unload - - reload - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html -https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be +Admin commands, that can be used from within the discord-client-chat """ # IMPORTS diff --git a/cogs/anime.py b/cogs/anime.py index 8705d28..6a32f16 100644 --- a/cogs/anime.py +++ b/cogs/anime.py @@ -1,10 +1,5 @@ """ -Some anime-related commands: - - animepic - - waifupic - - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +Some anime-related commands """ # IMPORTS - external diff --git a/cogs/cog_framework.py b/cogs/cog_framework.py index 8374eef..ba202b7 100644 --- a/cogs/cog_framework.py +++ b/cogs/cog_framework.py @@ -1,7 +1,5 @@ """ This framework can be used to create new Cogs, remember to add them in the config - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ # IMPORTS @@ -17,4 +15,4 @@ class Foo(commands.Cog): # COG ENDING def setup(bot): - bot.add_cog(Foo(bot)) \ No newline at end of file + bot.add_cog(Foo(bot)) diff --git a/cogs/general.py b/cogs/general.py deleted file mode 100644 index 11b2a3e..0000000 --- a/cogs/general.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -Some general commands: - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html -""" - -# IMPORTS - external -import discord -from discord.ext import commands -import platform - -# IMPORTS - internal -import loadconfig - -# COG INIT -class General(commands.Cog): - def __init__(self, bot): - self.bot = bot - -# COG BODY - - -# COG ENDING -def setup(bot): - bot.add_cog(General(bot)) diff --git a/cogs/help.py b/cogs/help.py index 05a3bc1..262b168 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -1,8 +1,5 @@ """ -Some help for the users: - - aquabot - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +Some help for the users """ # IMPORTS - external @@ -10,7 +7,7 @@ import discord from discord.ext import commands # IMPORTS - internal -import loadconfig +from __main__ import prefix # COG INIT class Help(commands.Cog): @@ -27,7 +24,7 @@ class Help(commands.Cog): embed.set_thumbnail(url=ctx.me.avatar_url) embed.add_field(name="Owner", value=self.bot.AppInfo.owner, inline=True) - embed.add_field(name="Command Prefix", value=loadconfig.__prefix__, inline=True) + embed.add_field(name="Command Prefix", value=prefix, inline=True) embed.add_field(name="Source Code", value="[GitHub](https://github.com/crammk/aquabot)", inline=True) footer_text = "This Bot is a project by MarcMK." diff --git a/cogs/meme.py b/cogs/meme.py index b9151fa..7b788a9 100644 --- a/cogs/meme.py +++ b/cogs/meme.py @@ -1,7 +1,5 @@ """ Send spicy memes to chat - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ # IMPORTS - external @@ -12,9 +10,6 @@ import random # IMPORTS - internal import loaddata -class MemeError(Exception): - pass - # COG INIT class Meme(commands.Cog): def __init__(self, bot): diff --git a/cogs/music.py b/cogs/music.py index 32c9936..9e7b368 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -1,7 +1,7 @@ """ Play Music -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +A COG NOT WRITTEN BY ME! ALTERED FROM THIS ONE: https://gist.github.com/vbe0201/ade9b80f2d3b64643d854938d40a0a2d """ diff --git a/cogs/reddit.py b/cogs/reddit.py new file mode 100644 index 0000000..dcbc85d --- /dev/null +++ b/cogs/reddit.py @@ -0,0 +1,41 @@ +""" +Fetch pictures from subreddits +""" + +# IMPORTS - external +import discord +from discord.ext import commands +import random +import praw + +# IMPORTS - internal +from __main__ import reddit_client_id +from __main__ import reddit_client_secret +from __main__ import reddit_client_useragent + +# COG INIT +class Reddit(commands.Cog): + def __init__(self, bot): + self.bot = bot + +# COG BODY + @commands.command(name="reddit") + async def reddit(self, ctx, sub: str): + """ + Send a post from a subreddit to chat + """ + reddit = praw.Reddit(client_id=reddit_client_id, + client_secret=reddit_client_secret, + user_agent=reddit_client_useragent) + + posts = reddit.subreddit(sub).hot() + rand_post = random.randint(1, 100) + # Make sure you're not sending a pinned post + for i in range(0, rand_post): + selected_post = next(x for x in posts if not x.stickied) + + await ctx.send(selected_post.url) + +# COG ENDING +def setup(bot): + bot.add_cog(Reddit(bot)) diff --git a/cogs/reminder.py b/cogs/reminder.py index 5d2dea7..4a01fb2 100644 --- a/cogs/reminder.py +++ b/cogs/reminder.py @@ -1,7 +1,5 @@ """ A simple reminder option - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ # IMPORTS diff --git a/cogs/utility.py b/cogs/utility.py index 693e56d..cbe758e 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -1,9 +1,5 @@ """ -Some (more or less) handy utility: - - invitelink - - pat - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +Some (more or less) handy utility """ # IMPORTS - external @@ -12,7 +8,7 @@ from discord.ext import commands import random # IMPORTS - internal -import loadconfig +from __main__ import avatar # COG INIT class Utility(commands.Cog): @@ -43,7 +39,7 @@ class Utility(commands.Cog): inline=True) link_embed.set_footer( text=f"Age: {age}, Uses: {uses}", - icon_url=loadconfig.__avatar__ + icon_url=avatar ) await ctx.send(embed=link_embed) @@ -58,8 +54,6 @@ class Utility(commands.Cog): author = ctx.message.author if target is None: response = "No one to pat..." - elif target.capitalize() == "Noel": - response = "NNN-GYAAAA!" else: response = f"{target} got pat by {author.mention}" diff --git a/cogs/welcome.py b/cogs/welcome.py index 6d260f1..ad2fef9 100644 --- a/cogs/welcome.py +++ b/cogs/welcome.py @@ -1,12 +1,7 @@ """ -Welcoming new users etc.: - - listener: join - - hello - -https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +Welcoming new users etc. """ - # IMPORTS import discord from discord.ext import commands diff --git a/config/__init__.py b/config/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/config/cogs.py b/config/cogs.py deleted file mode 100644 index d41fecb..0000000 --- a/config/cogs.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -Initally loaded cogs -Don't remove the "admin" cog or you won't be able to load cogs -from the Discord-Client -""" - -__cogs__ = [ - "cogs.admin", - "cogs.anime", - "cogs.general", - "cogs.help", - "cogs.meme", - "cogs.music", - "cogs.utility", - "cogs.welcome", - ] diff --git a/config/config_example.yml b/config/config_example.yml deleted file mode 100644 index 9733a63..0000000 --- a/config/config_example.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -token: '' -prefix: '' - -... diff --git a/data/media.py b/data/media.py deleted file mode 100644 index fac3a8c..0000000 --- a/data/media.py +++ /dev/null @@ -1,48 +0,0 @@ -""" -Media, which can be accessed from the bot. -""" - -# Internal lists -gifs_anime = [] - -pics_anime = [ - "https://i.imgur.com/4xnJN9x.png", - ] - -aqua = [] - -noel = [ - "https://i.imgur.com/qSAjVSL.png", - ] - -megumin = [ - "https://i.imgur.com/mSBfcXA.jpg", - "https://i.imgur.com/Dqua2lX.jpg", - "https://i.imgur.com/1jzaE3y.png", - "https://i.imgur.com/KpGSYD2.png", - ] - -akeno = [] - -rem = [ - "https://i.imgur.com/8HNv6ow.jpg", - "https://i.imgur.com/GEhYHKi.png", - "https://i.imgur.com/5dtXgEc.jpg", - "https://i.imgur.com/MV3ancf.png", - "https://i.imgur.com/yJCw8MX.jpg", - "https://i.imgur.com/jEGpRn2.jpg", - "https://i.imgur.com/y0DP3wL.jpg", - "https://i.imgur.com/cVNMUgi.jpg", - ] - - -# Exports -__media_anime__ = [gifs_anime, pics_anime] - -__media_girl__ = { - "Aqua": aqua, - "Noel": noel, - "Megumin": megumin, - "Akeno": akeno, - "Rem": rem - } diff --git a/loadconfig.py b/loadconfig.py deleted file mode 100644 index 0595b1a..0000000 --- a/loadconfig.py +++ /dev/null @@ -1,29 +0,0 @@ -""" -Gather all config data into one file, -so all other files can call it from here - -""" - -# IMPORTS -import yaml - -__avatar__ = "https://i.redd.it/0uajctrps9u41.jpg" - -# Import from yaml -try: - with open("config/config.yml") as file: - config = yaml.safe_load(file) - - __token__ = config['token'] - __prefix__ = config['prefix'] - -except yaml.YAMLError as error: - print(f"Error while parsing: {error}") -except FileNotFoundError as error: - print(f"Error, please create a config file: {error}") - -# Load cogs activated from the start -try: - from config.cogs import __cogs__ -except ImportError as error: - print(f"Error while importing: {error}") diff --git a/loaddata.py b/loaddata.py index 1980f8d..4a53943 100644 --- a/loaddata.py +++ b/loaddata.py @@ -5,7 +5,6 @@ so all other files can call it from here """ try: - from data.media import __media_anime__, __media_girl__ - from data.memes import __memes_list__ + from memes import __memes_list__ except ImportError as error: print(f"Error while importing: {error}") diff --git a/data/memes.py b/memes.py similarity index 100% rename from data/memes.py rename to memes.py diff --git a/requirements.txt b/requirements.txt index 5d94fbe..ac74fc0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ discord.py[voice] youtube-dl pyyaml +praw +argparse