diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..bf45bc8 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +DISCORD_BOT_TOKEN= +CHANNEL_LOG_ID= +CHANNEL_TRACEBACK_ID= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6e4761 --- /dev/null +++ b/.gitignore @@ -0,0 +1,129 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..52ce0ce --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Discord Bot Portal JP + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..629b83a --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..07f304a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# discordbot movie-maker + +動画生成bot diff --git a/cogs/__init__.py b/cogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/constants/__init__.py b/constants/__init__.py new file mode 100644 index 0000000..e6f1a41 --- /dev/null +++ b/constants/__init__.py @@ -0,0 +1,6 @@ +from dotenv import load_dotenv +from os import getenv + +load_dotenv() + +TOKEN = getenv('DISCORD_BOT_TOKEN') diff --git a/extensions/__init__.py b/extensions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extensions/convert.py b/extensions/convert.py new file mode 100644 index 0000000..664aa50 --- /dev/null +++ b/extensions/convert.py @@ -0,0 +1,51 @@ +import discord +from discord import app_commands +from discord.ext import commands +from moviepy.editor import AudioFileClip, ImageClip +from daug.utils.dpyexcept import excepter + +PATH_DEFAULT_IMAGE = 'icon.png' + + +class ConvertCog(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @app_commands.command(name='動画生成', description='音声と画像から動画を生成します') + @app_commands.rename(audio='音声', image='画像', comment='コメント') + @app_commands.describe(audio='音声ファイル', image='画像ファイル', comment='動画と一緒に送信するコメント') + @excepter + async def _convert_movie_app_command(self, interaction: discord.Interaction, audio: discord.Attachment, image: discord.Attachment | None, comment: str = ''): + if image is not None: + # 何故か逆になることがあるので + if audio.content_type.startswith('image') and image.content_type.startswith('audio'): + audio, image = image, audio + # ファイルが適切にアップロードされていない場合 + if not audio.content_type.startswith('audio') or not image.content_type.startswith('image'): + await interaction.response.send_message('正しい形式のファイルを指定してください', ephemeral=True) + return + + await interaction.response.defer() + + audio_path = f'/tmp/{audio.filename}' + image_path = f'/tmp/{image.filename}' if image else PATH_DEFAULT_IMAGE + movie_path = f'/tmp/output.mp4' + + with open(audio_path, 'wb') as audio_file: + await audio.save(audio_file) + + if image is not None: + with open(image_path, 'wb') as image_file: + await (image or interaction.user.avatar).save(image_file) + + image_clip = ImageClip(image_path, duration=AudioFileClip(audio_path).duration) + image_clip: ImageClip = image_clip.set_audio(AudioFileClip(audio_path)) + image_clip.write_videofile(movie_path, fps=1, codec='libx264', audio_codec='aac', temp_audiofile='temp_audiofile.m4a') + if comment: + await interaction.followup.send(comment, file=discord.File(movie_path, filename='output.mp4')) + else: + await interaction.followup.send(comment, file=discord.File(movie_path, filename='output.mp4')) + + +async def setup(bot: commands.Bot): + await bot.add_cog(ConvertCog(bot)) diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..afdcee4 Binary files /dev/null and b/icon.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..16a20df --- /dev/null +++ b/main.py @@ -0,0 +1,29 @@ +import discord +from discord.ext import commands +from constants import TOKEN + +extensions = ( + 'convert', +) + + +class MyBot(commands.Bot): + def __init__(self): + super().__init__( + command_prefix=commands.when_mentioned_or('$ '), + help_command=None, + intents=discord.Intents.all(), + ) + + async def setup_hook(self): + for extension in extensions: + await self.load_extension(f'extensions.{extension}') + await self.tree.sync() + + +def main(): + MyBot().run(TOKEN) + + +if __name__ == '__main__': + main() diff --git a/nixpacks.toml b/nixpacks.toml new file mode 100644 index 0000000..b82ad56 --- /dev/null +++ b/nixpacks.toml @@ -0,0 +1,2 @@ +[phases.setup] +aptPkgs = ["ffmpeg"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9bdbac9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +discord.py +Daug +python-dotenv +moviepy diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..67ebc4e --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.11 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..a16ff38 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[flake8] +ignore = E261,E302,E305,E501 diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29