From a96537dd5c199903555fcae09cddc09c7f605210 Mon Sep 17 00:00:00 2001 From: Goryudyuma <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 10:28:52 +0900 Subject: [PATCH 01/12] =?UTF-8?q?discord=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + Pipfile | 1 + Pipfile.lock | 18 +++++++++++++++++- library/clientclass.py | 32 ++++++++++++++++++++++++++++++++ run.py | 33 ++++++++++++++++++++++++++++----- slackbot_settings.py | 3 +++ 6 files changed, 82 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index cbe03bbfb4..2c4d0785f0 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ DATABASE_URL=postgres://postgres:password@postgres:5432/ +DISCORD_API_TOKEN= OPENAI_API_KEY= SLACK_API_TOKEN= SLACK_SIGNING_SECRET= diff --git a/Pipfile b/Pipfile index 2d8e893af0..5b68ae34f1 100644 --- a/Pipfile +++ b/Pipfile @@ -32,3 +32,4 @@ gitpython = "==3.1.31" pandas = "==1.5.3" matplotlib = "==3.7.0" openai = "==0.27.0" +discord = "==2.2.2" diff --git a/Pipfile.lock b/Pipfile.lock index 984cc01438..4400fa1da1 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6944e0b3c785090c853f3a522441bfefd3a12753f814904eafda6ec12c103057" + "sha256": "b1103efe61f36181c08040702ab0582cd37cd6e6f22e8f75d691ea34bceca7ea" }, "pipfile-spec": 6, "requires": { @@ -312,6 +312,22 @@ "markers": "python_version >= '3.6'", "version": "==0.11.0" }, + "discord": { + "hashes": [ + "sha256:1c6af76ed703d07159149e5406d2857419970925454867e145a228cc0996045e", + "sha256:d6b9c3420150cea67fc72f0aa2f0bd885c37406cf3d13def8fdf77cc59ca510a" + ], + "index": "pypi", + "version": "==2.2.2" + }, + "discord.py": { + "hashes": [ + "sha256:38fc52a784727b8e5e5749267089400035b187a009028eddfabeb182abcc6d52", + "sha256:b9944056bcb5711b2d04088848fd004466cf117c15c84fa798bf55470f28275f" + ], + "markers": "python_full_version >= '3.8.0'", + "version": "==2.2.2" + }, "flask": { "hashes": [ "sha256:7eb373984bf1c770023fce9db164ed0c3353cd0b53f130f4693da0ca756a2e6d", diff --git a/library/clientclass.py b/library/clientclass.py index aa6bf430e0..9a53b37c8b 100644 --- a/library/clientclass.py +++ b/library/clientclass.py @@ -3,10 +3,12 @@ """ clientに使うclass """ +import asyncio import os from abc import ABCMeta, abstractmethod from slack import WebClient +import discord import slackbot_settings as conf @@ -108,3 +110,33 @@ def get_send_user_name(self): def get_type(): """api""" return "api" + +class DisscordClient(BaseClient): + """ + Discordを操作するClient + """ + + def __init__(self, discord_client, message: discord.Message): + self.client = discord_client + self.message = message + + def post(self, message): + """Discordにポストする""" + asyncio.create_task(self.message.channel.send(message)) + + + def upload(self, file, filename): + """ファイルを投稿する""" + asyncio.create_task(self.message.channel.send(file=discord.File(file, filename=filename))) + + def get_send_user(self): + """botを呼び出したユーザーを返す""" + return self.message.author.id + + def get_send_user_name(self): + return self.message.author.name + + @staticmethod + def get_type(): + """discord""" + return "discord" diff --git a/run.py b/run.py index 4573f06661..c45c31eca1 100644 --- a/run.py +++ b/run.py @@ -3,6 +3,7 @@ """ BotのMain関数 """ +import asyncio import logging import logging.config import sys @@ -13,13 +14,12 @@ from slackeventsapi import SlackEventAdapter import slackbot_settings as conf -from library.clientclass import ApiClient, SlackClient +from library.clientclass import ApiClient, SlackClient, DisscordClient from library.database import Database from plugins import analyze app = Flask(__name__) - slack_events_adapter = SlackEventAdapter( signing_secret=conf.SLACK_SIGNING_SECRET, endpoint="/slack/events", server=app ) @@ -92,9 +92,9 @@ def on_app_mention(event_data): if block_element["type"] == "rich_text_section": block_element_elements = block_element["elements"] if ( - len(block_element_elements) > 0 - and block_element_elements[0]["type"] == "user" - and block_element_elements[0]["user_id"] in authed_users + len(block_element_elements) > 0 + and block_element_elements[0]["type"] == "user" + and block_element_elements[0]["user_id"] in authed_users ): tpe.submit( analyze_slack_message(block_element_elements[1:]), @@ -151,9 +151,32 @@ def status(): return jsonify({"message": "hato-bot is running", "version": conf.VERSION}), 200 +import discord + +intents = discord.Intents.all() +discordClient = discord.Client(intents=intents) + + +@discordClient.event +async def on_ready(): + logging.error(f'We have logged in as {discordClient.user}') + + +@discordClient.event +async def on_message(message): + if message.author == discordClient.user: + return + + if discordClient.user in message.mentions: + # `message.content.split(" ", 1)[1]` は、メンション先を除いた文字列 + analyze.analyze_message(message.content.split(" ", 1)[1])(DisscordClient(discordClient, message)) + + def main(): """メイン関数""" + discordClient.run(token=conf.DISCORD_API_TOKEN) + app.run(host="0.0.0.0", port=conf.PORT) diff --git a/slackbot_settings.py b/slackbot_settings.py index 1ef953c6fa..4e8a8fe0d8 100644 --- a/slackbot_settings.py +++ b/slackbot_settings.py @@ -37,6 +37,9 @@ # ChatGPT用の設定 OPENAI_API_KEY = str(os.environ["OPENAI_API_KEY"]) +# Discord用の設定 +DISCORD_API_TOKEN = str(os.environ["DISCORD_API_TOKEN"]) + GIT_COMMIT_HASH = os.environ.get("GIT_COMMIT_HASH") VERSION = "2.4.4" From c77961fb2b90bb6423cdd93abc1300504ccacf3f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 4 Mar 2023 01:30:24 +0000 Subject: [PATCH 02/12] =?UTF-8?q?format=E3=81=8C=E9=96=93=E9=81=95?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E3=81=9F=E3=81=AE=E3=81=A7=E7=9B=B4=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/clientclass.py | 8 +++++--- run.py | 17 +++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/library/clientclass.py b/library/clientclass.py index 9a53b37c8b..c1dfd832b9 100644 --- a/library/clientclass.py +++ b/library/clientclass.py @@ -7,8 +7,8 @@ import os from abc import ABCMeta, abstractmethod -from slack import WebClient import discord +from slack import WebClient import slackbot_settings as conf @@ -111,6 +111,7 @@ def get_type(): """api""" return "api" + class DisscordClient(BaseClient): """ Discordを操作するClient @@ -124,10 +125,11 @@ def post(self, message): """Discordにポストする""" asyncio.create_task(self.message.channel.send(message)) - def upload(self, file, filename): """ファイルを投稿する""" - asyncio.create_task(self.message.channel.send(file=discord.File(file, filename=filename))) + asyncio.create_task( + self.message.channel.send(file=discord.File(file, filename=filename)) + ) def get_send_user(self): """botを呼び出したユーザーを返す""" diff --git a/run.py b/run.py index c45c31eca1..d0519fe48b 100644 --- a/run.py +++ b/run.py @@ -10,11 +10,12 @@ from concurrent.futures import ThreadPoolExecutor from typing import Callable, List +import discord from flask import Flask, escape, jsonify, request from slackeventsapi import SlackEventAdapter import slackbot_settings as conf -from library.clientclass import ApiClient, SlackClient, DisscordClient +from library.clientclass import ApiClient, DisscordClient, SlackClient from library.database import Database from plugins import analyze @@ -92,9 +93,9 @@ def on_app_mention(event_data): if block_element["type"] == "rich_text_section": block_element_elements = block_element["elements"] if ( - len(block_element_elements) > 0 - and block_element_elements[0]["type"] == "user" - and block_element_elements[0]["user_id"] in authed_users + len(block_element_elements) > 0 + and block_element_elements[0]["type"] == "user" + and block_element_elements[0]["user_id"] in authed_users ): tpe.submit( analyze_slack_message(block_element_elements[1:]), @@ -151,15 +152,13 @@ def status(): return jsonify({"message": "hato-bot is running", "version": conf.VERSION}), 200 -import discord - intents = discord.Intents.all() discordClient = discord.Client(intents=intents) @discordClient.event async def on_ready(): - logging.error(f'We have logged in as {discordClient.user}') + logging.error(f"We have logged in as {discordClient.user}") @discordClient.event @@ -169,7 +168,9 @@ async def on_message(message): if discordClient.user in message.mentions: # `message.content.split(" ", 1)[1]` は、メンション先を除いた文字列 - analyze.analyze_message(message.content.split(" ", 1)[1])(DisscordClient(discordClient, message)) + analyze.analyze_message(message.content.split(" ", 1)[1])( + DisscordClient(discordClient, message) + ) def main(): From d7843f75d0f39c879b1ee4403bbf7099f2efc94d Mon Sep 17 00:00:00 2001 From: Goryudyuma <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:20:05 +0900 Subject: [PATCH 03/12] =?UTF-8?q?MODE=E3=81=AE=E7=94=A8=E6=84=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + run.py | 12 ++++-------- slackbot_settings.py | 2 ++ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 2c4d0785f0..9ab0719fdd 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,4 @@ OPENAI_API_KEY= SLACK_API_TOKEN= SLACK_SIGNING_SECRET= YAHOO_API_TOKEN= +MODE= \ No newline at end of file diff --git a/run.py b/run.py index d0519fe48b..17658e327f 100644 --- a/run.py +++ b/run.py @@ -156,11 +156,6 @@ def status(): discordClient = discord.Client(intents=intents) -@discordClient.event -async def on_ready(): - logging.error(f"We have logged in as {discordClient.user}") - - @discordClient.event async def on_message(message): if message.author == discordClient.user: @@ -176,9 +171,10 @@ async def on_message(message): def main(): """メイン関数""" - discordClient.run(token=conf.DISCORD_API_TOKEN) - - app.run(host="0.0.0.0", port=conf.PORT) + if conf.MODE == "discord": + discordClient.run(token=conf.DISCORD_API_TOKEN) + else: + app.run(host="0.0.0.0", port=conf.PORT) if __name__ == "__main__": diff --git a/slackbot_settings.py b/slackbot_settings.py index 4e8a8fe0d8..7c543b08ae 100644 --- a/slackbot_settings.py +++ b/slackbot_settings.py @@ -40,6 +40,8 @@ # Discord用の設定 DISCORD_API_TOKEN = str(os.environ["DISCORD_API_TOKEN"]) +MODE = str(os.environ["MODE"]) + GIT_COMMIT_HASH = os.environ.get("GIT_COMMIT_HASH") VERSION = "2.4.4" From cb0a212c423528aa058dada0884f5bbf5af79184 Mon Sep 17 00:00:00 2001 From: Goryudyuma <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:24:43 +0900 Subject: [PATCH 04/12] =?UTF-8?q?discord=E3=81=AE=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.template.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.template.md b/README.template.md index 424222015a..8677999be5 100644 --- a/README.template.md +++ b/README.template.md @@ -52,6 +52,12 @@ `.env.example` をコピーして使うとよいでしょう + MODEに `discord` を指定すると、DiscordのBotとして動作します。 + + DISCORD_API_TOKENにDiscordのBot Tokenを指定します。 + + DISCORD_API_TOKENには `READ MESSAGES/VIEW CHANNELS` と、 `Send Messages` の権限が必要です。 + 6. docker composeで鳩botとPostgreSQLを起動します。 ```sh From 05d3008f5719a49c8719b73a5a6fcfc0b3ec72fb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 4 Mar 2023 02:25:06 +0000 Subject: [PATCH 05/12] =?UTF-8?q?README=E3=82=92=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 836c160db1..32f67feb32 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,12 @@ `.env.example` をコピーして使うとよいでしょう + MODEに `discord` を指定すると、DiscordのBotとして動作します。 + + DISCORD_API_TOKENにDiscordのBot Tokenを指定します。 + + DISCORD_API_TOKENには `READ MESSAGES/VIEW CHANNELS` と、 `Send Messages` の権限が必要です。 + 6. docker composeで鳩botとPostgreSQLを起動します。 ```sh From cb62c859bd92b85e7182bdcb02db6a6007e15444 Mon Sep 17 00:00:00 2001 From: Goryudyuma <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:27:03 +0900 Subject: [PATCH 06/12] =?UTF-8?q?=E9=A0=86=E7=95=AA=E5=85=A5=E3=82=8C?= =?UTF-8?q?=E6=9B=BF=E3=81=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 9ab0719fdd..4dbe534460 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ DATABASE_URL=postgres://postgres:password@postgres:5432/ DISCORD_API_TOKEN= +MODE= OPENAI_API_KEY= SLACK_API_TOKEN= SLACK_SIGNING_SECRET= -YAHOO_API_TOKEN= -MODE= \ No newline at end of file +YAHOO_API_TOKEN= \ No newline at end of file From c3c03905ca5f30874e3965311d90e1f992cf0544 Mon Sep 17 00:00:00 2001 From: "Goryudyuma(Kei.Matsumoto)" <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:27:36 +0900 Subject: [PATCH 07/12] Update run.py Co-authored-by: Masaya Suzuki <15100604+massongit@users.noreply.github.com> --- run.py | 1 - 1 file changed, 1 deletion(-) diff --git a/run.py b/run.py index 17658e327f..32f92d8d1a 100644 --- a/run.py +++ b/run.py @@ -3,7 +3,6 @@ """ BotのMain関数 """ -import asyncio import logging import logging.config import sys From 392135c164fc813b6071d34c05b669bd13cdc7f1 Mon Sep 17 00:00:00 2001 From: Goryudyuma <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:28:35 +0900 Subject: [PATCH 08/12] =?UTF-8?q?=E6=94=B9=E8=A1=8C=E3=82=92=E5=85=A5?= =?UTF-8?q?=E3=82=8C=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 4dbe534460..822bc9f52b 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,4 @@ MODE= OPENAI_API_KEY= SLACK_API_TOKEN= SLACK_SIGNING_SECRET= -YAHOO_API_TOKEN= \ No newline at end of file +YAHOO_API_TOKEN= From 8696dfdd49d1bf3a971acf52bf3424b8143f5237 Mon Sep 17 00:00:00 2001 From: "Goryudyuma(Kei.Matsumoto)" <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:36:26 +0900 Subject: [PATCH 09/12] Update run.py Co-authored-by: Masaya Suzuki <15100604+massongit@users.noreply.github.com> --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index 32f92d8d1a..e43543591f 100644 --- a/run.py +++ b/run.py @@ -162,7 +162,7 @@ async def on_message(message): if discordClient.user in message.mentions: # `message.content.split(" ", 1)[1]` は、メンション先を除いた文字列 - analyze.analyze_message(message.content.split(" ", 1)[1])( + analyze.analyze_message(message.content.replace("\xa0", " ").split(" ", 1)[1])( DisscordClient(discordClient, message) ) From a3ea5834e6780291c86ef6c15a81ac946c6b9545 Mon Sep 17 00:00:00 2001 From: "Goryudyuma(Kei.Matsumoto)" <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:44:30 +0900 Subject: [PATCH 10/12] Update run.py Co-authored-by: Masaya Suzuki <15100604+massongit@users.noreply.github.com> --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index e43543591f..a51b1eb16c 100644 --- a/run.py +++ b/run.py @@ -161,7 +161,7 @@ async def on_message(message): return if discordClient.user in message.mentions: - # `message.content.split(" ", 1)[1]` は、メンション先を除いた文字列 + # `message.content.replace("\xa0", " ").split(" ", 1)[1]` は、メンション先を除いた文字列 analyze.analyze_message(message.content.replace("\xa0", " ").split(" ", 1)[1])( DisscordClient(discordClient, message) ) From 271f86b49521083b168d16b2a45bc65cfd308999 Mon Sep 17 00:00:00 2001 From: "Goryudyuma(Kei.Matsumoto)" <6211370+Goryudyuma@users.noreply.github.com> Date: Sat, 4 Mar 2023 11:44:44 +0900 Subject: [PATCH 11/12] Update README.template.md Co-authored-by: Masaya Suzuki <15100604+massongit@users.noreply.github.com> --- README.template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.template.md b/README.template.md index 8677999be5..77230c3a82 100644 --- a/README.template.md +++ b/README.template.md @@ -56,7 +56,7 @@ DISCORD_API_TOKENにDiscordのBot Tokenを指定します。 - DISCORD_API_TOKENには `READ MESSAGES/VIEW CHANNELS` と、 `Send Messages` の権限が必要です。 + DISCORD_API_TOKENには `Read Messages/View Channels` と、 `Send Messages` の権限が必要です。 6. docker composeで鳩botとPostgreSQLを起動します。 From 66ebbc45dca417f9607c9deee5af5fd6c751b72e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 4 Mar 2023 02:45:02 +0000 Subject: [PATCH 12/12] =?UTF-8?q?README=E3=82=92=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 32f67feb32..1f1d691285 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ DISCORD_API_TOKENにDiscordのBot Tokenを指定します。 - DISCORD_API_TOKENには `READ MESSAGES/VIEW CHANNELS` と、 `Send Messages` の権限が必要です。 + DISCORD_API_TOKENには `Read Messages/View Channels` と、 `Send Messages` の権限が必要です。 6. docker composeで鳩botとPostgreSQLを起動します。