From a653476e9c8bcb019029e75e6776a9713c6bbd9a Mon Sep 17 00:00:00 2001 From: CramMK Date: Thu, 23 Jan 2020 15:57:30 +0000 Subject: [PATCH] General development process - new cog commands - new voice cog - new requirements.txt - adjusted readme - added Dockerfile for faster deployment (still WIP) - other general things --- .dockerignore | 3 ++ .gitignore | 3 +- Dockerfile | 11 +++++++ README.md | 34 +++++++++++++++++----- aquabot.py | 64 ++++++++++++++++++++++++++++++++-------- cogs/admin.py | 17 ++++------- cogs/anime.py | 7 +++-- cogs/general.py | 22 ++++++++++---- cogs/help.py | 10 +++---- cogs/utility.py | 5 ++-- cogs/voice.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ cogs/welcome.py | 11 +++++-- loadconfig.py | 5 +++- requirements.txt | 1 + 14 files changed, 217 insertions(+), 52 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 cogs/voice.py create mode 100644 requirements.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f38de7b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +.gitignore +README.md diff --git a/.gitignore b/.gitignore index a512256..8c6796e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ config/config.py -.vscode/ \ No newline at end of file +.vscode/ +*.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..efa7560 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.6 + +MAINTAINER Marco Thomas + +RUN mkdir /aquabot-docker +WORKDIR /aquabot-docker +COPY . /aquabot-docker + +RUN pip install --user -r requirements.txt + +CMD ["python", "aquabot.py"] diff --git a/README.md b/README.md index 4b09d24..fa8ffe6 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,34 @@ here and there. Also note that this bot is still in its very early stages, so there is no guaranty that all features work! -Support and Report requests are handled via Discord (Link above). +Support and report requests are handled via Discord (Link above). -Installation ------------- +Installation - Docker +--------------------- -Use `pip install discord.py[voice]` to install the latest version of discord.py. +`WIP!` -Then, after cloning this repository with -`git clone https://github.com/crammk/aquabot`, you can run the `aquabot.py`. ++ Clone this repository with `git clne https://github.com/CramMK/aquabot` + ++ Create a `config/config.py`, using the `config/config_example.py` as a +guideline + ++ Launch the Container + +Installation - pip +------------------ + ++ Clone this repository with `git clone https://github.com/CramMK/aquabot` + ++ **OPTIONAL:** A virtual environment can be created at this point + ++ Use `pip install --user -r requirements.txt` to install all dependencies +needed for the bot + ++ Create a `config/config.py`, using the `config/config_example.py` as a +guideline + ++ Finally, run `python aquabot` Commands ------ @@ -41,5 +60,4 @@ To use the bot you need to add a `config/config.py` file. For reference, see Requirements ------------ -python>=3.6.0 -discord.py>=1.0.0a \ No newline at end of file ++ python>=3.6.0 diff --git a/aquabot.py b/aquabot.py index 267d96d..03e9616 100644 --- a/aquabot.py +++ b/aquabot.py @@ -8,33 +8,69 @@ https://discordpy.readthedocs.io/en/latest/intro.html # IMPORTS - external import discord from discord.ext import commands +import logging # IMPORTS - internal import loadconfig -# CONSTANTS +# LOGGING +logger = logging.getLogger("discord") +# https://docs.python.org/3/library/logging.html#levels +logger.setlevel(logging.INFO) +handler = logging.FileHandler( + filename="discord_%(asctime)s.log", + encoding="utf-8", + mode="w" + ) +handler.setFormatter( + logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s") + ) +logger.addHandler(handler) +# SOME DATA THAT CAN BE USED LATER +metadata = { + "name": f"{bot.user.name} - {bot.user.id}", + "admin": f"{self.bot.AppInfo.owner}", + "prefix": f"{loadconfig.__token__}", + "discordpy_version": f"{discord.__version__}", + "python_version": f"{platform.python_version}", + "os": f"{platform.system()} {platform.release()} {platform.version()}" +} -# INIT +# INIT THE BOT bot = commands.Bot( command_prefix=loadconfig.__prefix__, description="Holy Goddess Aqua!" ) +# LOAD COGS SPECIFIED IN 'config/cogs.py' for cog in loadconfig.__cogs__: try: bot.load_extension(cog) - except Exception: - print(f"Error while trying to load cog {cog}") + except Exception as e: + print(f"ERROR: {type(e).__name__} - {e}") + else: + print(f"SUCCESS: Loaded {cog}") -# BOT -# https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be +# BOT STARTING EVENT @bot.event async def on_ready(): - print(f""" - Logged in as: {bot.user.name} - {bot.user.id}\n - Version: {discord.__version__}\n - """) + """ + This prints some information about the bot, while starting up + + Inspired by: + https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be + """ + + startup = f""" + Bot Name: {metadata["name"]}\n + Admin: {metadata["admin"]}\n + Command Prefix: {metadata["prefix"]}\n + discord.py Version: {metadata["discordpy_version"]}\n + python Version: {metadata["python_version"]}\n + OS: {metadata["os"]}\n + """ + print(startup) status = f"Hentai | {loadconfig.__prefix__}aquabot" activity = discord.Activity(name=status, type=discord.ActivityType.watching) @@ -42,5 +78,9 @@ async def on_ready(): print(f"AquaBot is ready!\n") - -bot.run(loadconfig.__token__, bot=True, reconnect=True) \ No newline at end of file +# START BOT +bot.run( + loadconfig.__token__, + bot=True, + reconnect=True + ) diff --git a/cogs/admin.py b/cogs/admin.py index 006e840..0027546 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -1,17 +1,10 @@ -# -# A Cog that add admin command, usable within the Discord Client -# -# https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html -# -# "load", "unload", "reload" -# https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be -# """ -Admin commands, that can be used from within the chat +Admin commands, that can be used from within the chat: + - load + - unload + - reload https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html - -"load", "unload", "reload" https://gist.github.com/EvieePy/d78c061a4798ae81be9825468fe146be """ @@ -70,4 +63,4 @@ class Admin(commands.Cog): # COG ENDING def setup(bot): - bot.add_cog(Admin(bot)) \ No newline at end of file + bot.add_cog(Admin(bot)) diff --git a/cogs/anime.py b/cogs/anime.py index 056259f..15a268f 100644 --- a/cogs/anime.py +++ b/cogs/anime.py @@ -1,5 +1,6 @@ """ -Some anime-related commands +Some anime-related commands: + https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ @@ -9,7 +10,7 @@ import discord from discord.ext import commands # COG INIT -class Adnime(commands.Cog): +class Anime(commands.Cog): def __init__(self, bot): self.bot = bot @@ -17,4 +18,4 @@ class Adnime(commands.Cog): # COG ENDING def setup(bot): - bot.add_cog(Adnime(bot)) \ No newline at end of file + bot.add_cog(Anime(bot)) diff --git a/cogs/general.py b/cogs/general.py index fea1f70..fc0991a 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -1,5 +1,6 @@ """ -Some general commands +Some general commands: + - about https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ @@ -8,6 +9,9 @@ https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html import discord from discord.ext import commands +# IMPORTS - internal +from aquabot import metadata + # COG INIT class General(commands.Cog): def __init__(self, bot): @@ -21,14 +25,22 @@ class General(commands.Cog): """ embed = discord.Embed(colour=discord.Colour.blue()) - embed.add_field(name="test", value="test2") + embed.add_field(name="Bot Name", value=metadata["name"], inline=True) + embed.add_field(name="Admin", value=metadata["admin"], inline=True) + embed.add_field(name="Prefix", value=metadata["prefix"], inline=True) + embed.add_field(name="discord.py Version", value=metadata["discordpy_version"], inline=True) + embed.add_field(name="python Version", value=metadata["python_version"], inline=True) + embed.add_field(name="OS", value=metadata["os"], inline=True) - embed.set_footer(text="footer", icon_url="img/avatar.png") - + footer_text = """ + This Bot is an OpenSource project by Marc and can be found on + github.com/CramMK/aquabot + """ + embed.set_footer(text=footer_text, icon_url="img/avatar.png") await ctx.send(embed=embed) # COG ENDING def setup(bot): - bot.add_cog(General(bot)) \ No newline at end of file + bot.add_cog(General(bot)) diff --git a/cogs/help.py b/cogs/help.py index ff94415..1d33a1d 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -1,5 +1,6 @@ """ -Some help for the users +Some help for the users: + - aquabot https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ @@ -14,19 +15,18 @@ class Help(commands.Cog): self.bot = bot # COG BODY - @commands.command( name="aquabot") + @commands.command(name="aquabot") async def aquabot(self, ctx): """ Sends a short help for new users """ response = """ - I'm the usele... divine AquaBot! + I'm the usele... divine AquaBot!\n If you need help, try using the `help` command! """ - await ctx.send(response) # COG ENDING def setup(bot): - bot.add_cog(Help(bot)) \ No newline at end of file + bot.add_cog(Help(bot)) diff --git a/cogs/utility.py b/cogs/utility.py index 5c417e9..6f050c2 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -1,5 +1,7 @@ """ -Some nice utility +Some (more or less) handy utility: + - invitelink + - pat https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ @@ -19,7 +21,6 @@ class Utility(commands.Cog): """ Sends the server's invitelink to chat """ - # TODO fetch this from config so more servers are supported link = "Here is our invite link: https://discordapp.com/invite/HbYfyJT" await ctx.send(link) diff --git a/cogs/voice.py b/cogs/voice.py new file mode 100644 index 0000000..014a84c --- /dev/null +++ b/cogs/voice.py @@ -0,0 +1,76 @@ +""" +Make Aqua be able to join voice channel and play audio: + - join + - leave + - play + +https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +""" + +# IMPORTS +import discord +from discord.ext import commands +from discord.utils import get + +# COG INIT +class Voice(commands.Cog): + def __init__(self, bot): + self.bot = bot + +# COG BODY + @commands.command(name="join", aliases=["j"]) + @commands.guild_only() + async def join(self, ctx): + """ + Tries to join the author's current voice channel + """ + channel = ctx.message.author.voice.channel + if not channel: + ctx.send:("You're not connected to a voice channel!") + return + + voice = get(bot.voice_clients, guild=ctx.guild) + + if voice and voice.is_connected(): + await voice.move_to(channel) + else: + voice = await channel.connect() + await ctx.send(f"`Joined {channel}!`") +""" useless? + await voice.disconnect() + + if voice and voice.is_connected(): + await voice.move_to(channel) + else: + voice = await channel.connect() + await ctx.send(f"`Joined {channel}!`") +""" + + @commands.command(name="leave", aliases=["quit, l"]) + @commands.guild_only() + async def leave(self, ctx): + """ + Leaves the voice channel, if connected + """ + channel = ctx.message.author.voice.channel + voice = get(bot.voice_clients, guild=ctx.guild) + + if voice and voice.is_connected(): + await voice.disconnect() + await ctx.send(f"`Left {channel}!`") + else: + ctx.send("I'm not connected to a channel!") + + @commands.command(name="play", aliases=["p"]) + @commands.guild_only() + async def play(self, ctx, url: str): + """ + Plays music from YT link specifies + """ + # TODO + ctx.send("This module is not working yet!") + + +# COG ENDING +def setup(bot): + bot.add_cog(Voice(bot)) diff --git a/cogs/welcome.py b/cogs/welcome.py index 6d553e7..64d519a 100644 --- a/cogs/welcome.py +++ b/cogs/welcome.py @@ -1,5 +1,7 @@ """ -Welcoming new users etc. +Welcoming new users etc.: + - listener: join + - hello https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html """ @@ -28,9 +30,12 @@ class Welcome(commands.Cog): @commands.command(name="hello") async def hello(self, ctx): - + """ + Sends a simple reply to the user + """ await ctx.send(f"Hello {ctx.author.mention}!") + # COG ENDING def setup(bot): - bot.add_cog(Welcome(bot)) \ No newline at end of file + bot.add_cog(Welcome(bot)) diff --git a/loadconfig.py b/loadconfig.py index dff2c40..fb787d8 100644 --- a/loadconfig.py +++ b/loadconfig.py @@ -6,4 +6,7 @@ loadconfig.py then gets called in aquabot.py from config.cogs import __cogs__ -from config.config import __token__, __prefix__ +try: + from config.config import __token__, __prefix__ +except ImportError as e: + print(f"Error while importing: {e}") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0bf3ae3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +discord.py[voice]=1.2.5