Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
1ntegrale9 committed Jan 6, 2024
0 parents commit 8679c40
Show file tree
Hide file tree
Showing 16 changed files with 252 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DISCORD_BOT_TOKEN=
CHANNEL_LOG_ID=
CHANNEL_TRACEBACK_ID=
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python main.py
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# discordbot movie-maker

動画生成bot
Empty file added cogs/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dotenv import load_dotenv
from os import getenv

load_dotenv()

TOKEN = getenv('DISCORD_BOT_TOKEN')
Empty file added extensions/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions extensions/convert.py
Original file line number Diff line number Diff line change
@@ -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))
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions nixpacks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[phases.setup]
aptPkgs = ["ffmpeg"]
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
discord.py
Daug
python-dotenv
moviepy
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.11
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore = E261,E302,E305,E501
Empty file added utils/__init__.py
Empty file.

0 comments on commit 8679c40

Please sign in to comment.