Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discord対応 #2372

Merged
merged 15 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
DATABASE_URL=postgres://postgres:password@postgres:5432/
DISCORD_API_TOKEN=
OPENAI_API_KEY=
SLACK_API_TOKEN=
SLACK_SIGNING_SECRET=
YAHOO_API_TOKEN=
MODE=
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ gitpython = "==3.1.31"
pandas = "==1.5.3"
matplotlib = "==3.7.0"
openai = "==0.27.0"
discord = "==2.2.2"
18 changes: 17 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` の権限が必要です。
Goryudyuma marked this conversation as resolved.
Show resolved Hide resolved

6. docker composeで鳩botとPostgreSQLを起動します。

```sh
Expand Down
34 changes: 34 additions & 0 deletions library/clientclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""
clientに使うclass
"""
import asyncio
import os
from abc import ABCMeta, abstractmethod

import discord
from slack import WebClient

import slackbot_settings as conf
Expand Down Expand Up @@ -108,3 +110,35 @@ 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"
26 changes: 23 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@
"""
BotのMain関数
"""
import asyncio
Goryudyuma marked this conversation as resolved.
Show resolved Hide resolved
import logging
import logging.config
import sys
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
from library.clientclass import ApiClient, DisscordClient, SlackClient
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
)
Expand Down Expand Up @@ -151,10 +152,29 @@ def status():
return jsonify({"message": "hato-bot is running", "version": conf.VERSION}), 200


intents = discord.Intents.all()
discordClient = discord.Client(intents=intents)


@discordClient.event
async def on_message(message):
if message.author == discordClient.user:
return

if discordClient.user in message.mentions:
# `message.content.split(" ", 1)[1]` は、メンション先を除いた文字列
Goryudyuma marked this conversation as resolved.
Show resolved Hide resolved
analyze.analyze_message(message.content.split(" ", 1)[1])(
Goryudyuma marked this conversation as resolved.
Show resolved Hide resolved
DisscordClient(discordClient, message)
)


def main():
"""メイン関数"""

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__":
Expand Down
5 changes: 5 additions & 0 deletions slackbot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
# ChatGPT用の設定
OPENAI_API_KEY = str(os.environ["OPENAI_API_KEY"])

# 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"