From 357547cc59a105016119dc909962e2bc2d88ee1a Mon Sep 17 00:00:00 2001
From: tetsuya-k <64536338+tetsuya-ki@users.noreply.github.com>
Date: Mon, 3 Oct 2022 00:17:24 +0900
Subject: [PATCH] =?UTF-8?q?dockerfile=E3=81=AE=E3=83=9F=E3=82=B9=E4=BF=AE?=
 =?UTF-8?q?=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- poetryがうまく起動せず、エラーとなっていた(諦めてpipからインストール)
- fly.ioの場合、コメントアウトした部分trueにするとエラーになるので仮想環境で実行するよう指定
- ENABLE_SLASH_COMMAND_GUILD_IDがいまいちだった点も修正
---
 Dockerfile               | 20 +++++++++++---------
 assistantbot.py          |  4 +++-
 cogs/modules/settings.py |  8 +++++++-
 entrypoint.sh            |  2 +-
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index aea45b5..3a2e443 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,24 +2,25 @@
 FROM python:3.8.6-slim
 
 # ソースを置くディレクトリを変数として格納
-ARG dir=/opt/app
-ARG POETRY_VERSION=1.1.13
-ARG POETRY_HOME=/opt/poetry
+ARG dir=/opt/app \
+    home=/home
+ENV POETRY_HOME=/opt/poetry \
+    # POETRY_VIRTUALENVS_CREATE=false \
+    # POETRY_VIRTUALENVS_IN_PROJECT=false \
+    POETRY_NO_INTERACTION=1 \
+    POETRY_VERSION=1.1.14
+ENV PATH=$PATH:$POETRY_HOME/bin
 # poetry導入
-RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=${POETRY_HOME} python3 - --version=${POETRY_VERSION}
-ENV PATH ${PATH}:${POETRY_HOME}/bin
+RUN pip install poetry
 
 
 WORKDIR $dir
 RUN groupadd -r bot && useradd -r -g bot bot
 ADD entrypoint.sh $dir
-ADD requirements.txt $dir
-
-# requirements.txtに記載されたパッケージをインストール
-RUN pip install -r requirements.txt
 
 ADD . $dir
 RUN chmod -R 755 $dir && chown -R bot:bot $dir
+RUN chmod -R 755 $home && chown -R bot:bot $home
 RUN rm -f $dir/cogs/modules/first_time
 
 # user botで実行
@@ -45,6 +46,7 @@ APPLICATION_ID=\
 USE_IF_AVAILABLE_FILE=True
 
 WORKDIR $dir
+RUN poetry update && poetry install
 
 # dockerコンテナが起動する際に実行されるコードファイル (`entrypoint.sh`)
 ENTRYPOINT ["./entrypoint.sh"]
\ No newline at end of file
diff --git a/assistantbot.py b/assistantbot.py
index 61201cd..94fc166 100644
--- a/assistantbot.py
+++ b/assistantbot.py
@@ -51,8 +51,10 @@ async def setup_hook(self):
             await self.load_extension(cog) # awaitが必要
 
         # テスト中以外は環境変数で設定しないことを推奨(環境変数があれば、ギルドコマンドとして即時発行される)
-        if settings.ENABLE_SLASH_COMMAND_GUILD_ID:
+        if settings.ENABLE_SLASH_COMMAND_GUILD_ID is not None and len(settings.ENABLE_SLASH_COMMAND_GUILD_ID) > 0:
+            LOG.info(settings.ENABLE_SLASH_COMMAND_GUILD_ID)
             for guild in settings.ENABLE_SLASH_COMMAND_GUILD_ID:
+                LOG.info(guild)
                 self.tree.copy_global_to(guild=guild)
                 await self.tree.sync(guild=guild)
         else:
diff --git a/cogs/modules/settings.py b/cogs/modules/settings.py
index 16990c8..387e006 100644
--- a/cogs/modules/settings.py
+++ b/cogs/modules/settings.py
@@ -35,7 +35,13 @@ def num_env(param):
         return int(param)
 
 def split_guild_env(str):
-    guilds = [] if str is None else list(map(discord.Object, str.split(';')))
+    guilds = []
+    if str is None or str == '':
+        pass
+    elif  not ';' in str:
+        guilds.append(discord.Object(str))
+    else:
+        guilds = list(map(discord.Object, str.split(';')))
     return guilds
 
 load_dotenv(verbose=True)
diff --git a/entrypoint.sh b/entrypoint.sh
index f8cd3ec..e49c2d8 100644
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -1,3 +1,3 @@
 #!/bin/bash
-poetry init
+poetry install
 poetry run python assistantbot.py
\ No newline at end of file