From 4581f24da9160fbcf2f02169c74ffa087228c6a2 Mon Sep 17 00:00:00 2001 From: CramMK Date: Fri, 24 Jan 2020 15:41:14 +0000 Subject: [PATCH] General Development --- .dockerignore | 6 ++++++ .gitignore | 8 +++++++- aquabot.py | 17 +++++++++++++---- cogs/general.py | 3 ++- cogs/help.py | 10 +++++----- cogs/voice.py | 34 +++++++++++++++++++++++++++++++--- config/status.py | 15 +++++++++++++++ loadconfig.py | 4 +++- requirements.txt | 1 + 9 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 config/status.py diff --git a/.dockerignore b/.dockerignore index f38de7b..070b6db 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,9 @@ +# git related things .git .gitignore + +# build environment not needed for deployment +.vscode/ + +# other README.md diff --git a/.gitignore b/.gitignore index 8c6796e..3fc115b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,10 @@ -__pycache__/ +# own config file config/config.py + +# caches and build environment +__pycache__/ .vscode/ + +# generated data from logging +logs/ *.log diff --git a/aquabot.py b/aquabot.py index c17fd47..6e2b70c 100644 --- a/aquabot.py +++ b/aquabot.py @@ -9,7 +9,9 @@ https://discordpy.readthedocs.io/en/latest/intro.html import discord from discord.ext import commands import logging +from datetime import datetime import platform +import random # IMPORTS - internal import loadconfig @@ -19,7 +21,7 @@ logger = logging.getLogger("discord") # https://docs.python.org/3/library/logging.html#levels logger.setLevel(logging.INFO) handler = logging.FileHandler( - filename="discord.log", + filename="logs/discord-{%Y-%m-%d_%H-%M}.log".format(datetime.now()), encoding="utf-8", mode="w" ) @@ -43,6 +45,15 @@ for cog in loadconfig.__cogs__: else: print(f"SUCCESS: Loaded {cog}") +# ACTIVITY +async def activity(): + while True: # Infinite Loop + new_activity = random.choice(loadconfig.__activity__) + status = f"{new_activity[1]} | {loadconfig.__prefix__}aquabot" + activity = discord.Activity(name=status, type=new_activity[0]) + await bot.change_presence(activity=activity) + await asyncio.sleep(15) # Time in minutes + # BOT STARTING EVENT @bot.event async def on_ready(): @@ -64,9 +75,7 @@ async def on_ready(): """ print(startup) - status = f"Hentai | {loadconfig.__prefix__}aquabot" - activity = discord.Activity(name=status, type=discord.ActivityType.watching) - await bot.change_presence(activity=activity) + activity_loop = asyncio.ensure_future(activity()) print(f"AquaBot is ready!\n") diff --git a/cogs/general.py b/cogs/general.py index 2bd0af1..b1b3a66 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -26,6 +26,7 @@ class General(commands.Cog): """ embed = discord.Embed(colour=discord.Colour.blue()) embed.set_thumbnail(url=ctx.me.avatar_url) + embed.set_image(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) @@ -44,4 +45,4 @@ class General(commands.Cog): # 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 1d33a1d..231673b 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -20,11 +20,11 @@ class Help(commands.Cog): """ Sends a short help for new users """ - response = """ - I'm the usele... divine AquaBot!\n - If you need help, try using the `help` command! - """ - await ctx.send(response) + response = ( + "I'm the usele... divine AquaBot!\n" + "If you need help. try using the `help` command!" + ) + await ctx.send(response) # COG ENDING diff --git a/cogs/voice.py b/cogs/voice.py index 4cb6c3f..d7dcabb 100644 --- a/cogs/voice.py +++ b/cogs/voice.py @@ -5,12 +5,16 @@ Make Aqua be able to join voice channel and play audio: - play https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html +https://stackoverflow.com/questions/56031159/discord-py-rewrite-what-is-the-source-for-youtubedl-to-play-music """ # IMPORTS import discord from discord.ext import commands from discord.utils import get +from discord import FFmpegPCMAudio +import os +import youtube_dl # COG INIT class Voice(commands.Cog): @@ -26,14 +30,14 @@ class Voice(commands.Cog): """ channel = ctx.message.author.voice.channel if not channel: - ctx.send:("You're not connected to a voice channel!") + ctx.send("You're not connected to a voice channel!") return voice = get(self.bot.voice_clients, guild=ctx.guild) if voice and voice.is_connected(): await voice.move_to(channel) - await ctx.send(f"`Move to {channel}!`") + await ctx.send(f"`Moved to {channel}!`") else: voice = await channel.connect() await ctx.send(f"`Joined {channel}!`") @@ -62,7 +66,31 @@ class Voice(commands.Cog): Plays music from YT link specifies """ # TODO - ctx.send("This module is not working yet!") + try: + if os.path.isfile("song.mp3"): + os.remove("song.mp3") + except PermissionError: + await ctx.send("Wait for the current song to end or use the `stop`command") + return + + voice = get(bot.voice_clients, guild=ctx.guild) + youtube_dl_opts = { + 'format': 'bestaudio/best', + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }], + } + with youtube_dl.YouTubeDL(youtube_dl_opts) as ydl: + ydl.download([url]) + for file in os.listdir("./"): + if file.endswith(".mp3"): + os.rename(file, "song.mp3") + + voice.play(discord.FFmpegPCMAudio("song.mp3")) + voice.volume=25 + voice.is_playing() # COG ENDING diff --git a/config/status.py b/config/status.py new file mode 100644 index 0000000..fb709a7 --- /dev/null +++ b/config/status.py @@ -0,0 +1,15 @@ +""" +The bot's online status rotates through this list +""" + +__activity__ = [ + (discord.ActivityType.watching, "Hentai"), + (discord.ActivityType.watching, "Konosuba"), + (discord.ActivityType.watching, "beach episodes") + (discord.ActivityType.playing, "with water") + (discord.ActivityType.playing, "with Megumin"), + (discord.ActivityType.playing, "with Sake"), + (discord.ActivityType.streaming, "Hentai") + (discord.ActivityType.custom, "mizu") + ] +} diff --git a/loadconfig.py b/loadconfig.py index 854fba6..73794d8 100644 --- a/loadconfig.py +++ b/loadconfig.py @@ -8,7 +8,9 @@ __avatar__ = "https://i.imgur.com/mskM9dH.png" from config.cogs import __cogs__ +from config.status import __activity__ + try: from config.config import __token__, __prefix__ except ImportError as e: - print(f"Error while importing: {e}") \ No newline at end of file + print(f"Error while importing: {e}") diff --git a/requirements.txt b/requirements.txt index 0bf3ae3..44b454e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ discord.py[voice]=1.2.5 +youtube-dl