This repository was archived by the owner on Apr 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_command.py
More file actions
288 lines (243 loc) · 10 KB
/
Copy pathmessage_command.py
File metadata and controls
288 lines (243 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
ユーザーのメッセージから実行するコマンド群
"""
import re
import codecs
import json
import datetime
import os
import random
import requests
import discord
from lib.lol_counter import LolCounter
from lib.typo import Typo
from lib.manual_judge import ManualJudge
from lib.party_ichiyo import PartyIchiyo
from lib.kaere import Kaere
from lib.role import role
from lib.kokusei_chousa import number
from lib.message_debug import *
from lib.hukueki import *
class MessageCommands:
"""
こいつをもとにメッセージコマンドを実行します
"""
REGEXES = {
"GitHub" : re.compile(r".*?\#(.+?)\/([^\s]+).*?"),
"Channel" : re.compile(r"^<#([0-9]+?)>$"),
"Util Command": re.compile(r"^!(\w+?)*(\s\w+)*"),
"Typo" : re.compile(r"^.*だカス$")
}
HARASYO = None
ISSO = None
MESSAGE_COMMANDS = {
"ハラショー": None,
"いっそう" : None,
"疲れた" : "大丈夫?司令官\n開発には休息も必要だよ。しっかり休んでね",
"おやすみ" : "おやすみ、司令官。"
}
HIBIKI_MESSAGES = [
"なんだい?司令官"
]
def __init__(self, message: str, channel: discord.TextChannel, member: discord.Member):
"""
インスタンス化の際に必要インスタンスを受け取る
message: str
メッセージの文章
channel: discord.Channel
メッセージ送信先のチャンネル
member: discord.Member
発言者のMemberインスタンス
"""
if MessageCommands.LOL_COUNTER is None:
return
self.message = message
self.channel = channel
self.member_id = member.id
self.member_name = member.display_name
with codecs.open(os.getcwd() + "/messages.json", 'r', 'utf-8') as json_file:
self.response_dict = json.loads(json_file.read())
async def execute(self):
"""
__init__で渡された情報をもとにコマンドを実行
"""
if "草" in self.message or "くさ" in self.message:
MessageCommands.LOL_COUNTER.count(self.message, self.member_id)
if "響" in self.message or "ひびき" in self.message:
chosen_msg = random.choice(MessageCommands.HIBIKI_MESSAGES)
await self.channel.send(chosen_msg)
if self.message.count("***") >= 2:
await self.channel.send(
self.response_dict["bold-italic-cop"]["message"].format(MessageCommands.MESSAGE_COMMANDS["ハラショー"])
)
for content in MessageCommands.MESSAGE_COMMANDS.keys():
if content in self.message:
await self.channel.send(MessageCommands.MESSAGE_COMMANDS[content])
if content == "おやすみ":
await self.channel.send(MessageCommands.goodnight_time())
command_type = None
for (key, regex) in MessageCommands.REGEXES.items():
if regex.match(self.message):
command_type = key
command = regex.match(self.message)
break
if not command_type:
return
commands_list = {
"GitHub" : self.github,
"Util Command": self.util_command,
"Typo" : self.typo
}
await commands_list[command_type](command)
@staticmethod
def goodnight_time():
"""
時間によってコマンドの内容を変える
"""
time = datetime.datetime.now()
if 7 > time.hour > 0:
return "こんな時間まで何してたんだい?\n風邪引いちゃうから明日は早めに寝なよ?"
return "また明日"
async def github(self, raw_command):
"""
GitHubのリポジトリ参照などのコマンド
Parameters
----------
raw_command: re.Match
正規表現に引っかかったコマンド達
"""
repo_name = raw_command[1]
command = raw_command[2]
message = self.response_dict["repo"]
channel = self.channel
converter = {
"t": "top",
"i": "issues",
"pr": "pull",
"p": "pull"
}
res = requests.get("https://github.com/brokenManager/" + repo_name)
if res.status_code == 404:
await self.try_connect_other_repo(repo_name, command, message)
return
if command in converter.keys():
command = converter[command]
if command in message.keys():
await channel.send(message[command].format(repo_name))
return
if command.isdecimal():
response_issue = requests.get("https://github.com/brokenManager/{}/issues/{}".format(repo_name, command))
if response_issue.status_code == 404:
await channel.send(message["issue-not-found"])
else:
await channel.send(message["issue-found"].format(repo_name, command))
return
branch = requests.get(
"https://github.com/brokenManager/{}/tree/{}".format(repo_name, command)
)
master = requests.get(
"https://github.com/brokenManager/{}/tree/master/{}".format(repo_name, command)
)
if branch.status_code != 404:
await channel.send(message["branch-selected"].format(repo_name, command))
return
if master.status_code != 404:
await channel.send(message["master-suggested"].format(repo_name, command))
return
await channel.send(message["file-not-found"])
async def try_connect_other_repo(self, user_name: str, repo_name: str, message: dict):
"""
他のユーザーやグループのリポジトリにアクセスを試みる
Parameters
----------
user_name: str
アクセス先のユーザー/グループ名
repo_name: str
アクセス先のリポジトリの名前
message: dict<str, str>
送信するメッセージのテンプレートの辞書
"""
res = requests.get("https://github.com/{}/{}".format(user_name, repo_name))
if res.status_code == 404:
await self.channel.send(message["not-found"])
else:
await self.channel.send(message["other_repo"].format(user_name, repo_name))
async def util_command(self, _):
"""
lolやtypoなどのコマンドを処理する
"""
user_message = self.message[1:]
commands = user_message.split()
command_name = commands[0].lower()
if command_name == "lol":
await MessageCommands.LOL_COUNTER.output(self.channel, self.member_id)
return
if command_name == "typo":
await self.channel.send(
MessageCommands.TYPO_COUNTER.call(self.member_id, self.member_name)
)
return
if command_name in ["jd" or "judge"]:
await MessageCommands.MANUAL_JUDGE.call(commands, self.channel)
return
if command_name in ["partyichiyo", "party"]:
await MessageCommands.PARTY_ICHIYO.change_command(commands, self.channel)
return
if command_name == "kaere":
await MessageCommands.KAERE.command_controller(commands, self.member_name)
return
if command_name in ["number", "zinnkou", "zinkou", "population"]:
await number(self.channel)
return
if command_name == "role":
await role(commands, self.channel, self.member_name)
return
if command_name in ["debug", "d"] and len(commands) >= 2:
await debug_on_message(commands[1], self.channel)
return
if command_name in ["hukueki", "syourai", "shourai"] and len(commands) >= 2:
joined_message = " ".join(commands[1:])
await hukueki(joined_message, self.channel)
return
if command_name in ["lolicon", "yameta"] and len(commands) >= 2:
joined_message = " ".join(commands[1:])
await lolicon(joined_message, self.member_name, self.channel)
return
async def typo(self, raw_command: list):
"""
typoを記録するコマンド
Parameters
----------
raw_command: list<str>
コマンドの入ったリスト
"""
command = raw_command.group()
MessageCommands.TYPO_COUNTER.append(self.member_id, command)
@staticmethod
def static_init(members: list, harasyo: discord.Emoji, isso: discord.Emoji, abc_emojis: dict,
base_voice_channel: discord.TextChannel, kikisen_channel: discord.VoiceChannel,
hakaba_voice_channel: discord.VoiceChannel):
"""
lol_counterをこのインスタンスに渡す処理
Parameters
----------
members: List<discord.Member>
メンバーのリスト
harasyo: discord.Emoji
ハラショーって言った際に返信するemoji
isso: discord.Emoji
いっそうって言った際に返信するemoji
base_voice_channel: discord.VoiceChannel
一般ボイスチャンネル PartyIchiyoはここに出てくる
kikisen_channel: discord.TextChannel
聞き専のテキストチャンネル PartyIchiyoのテキスト通知もここ
"""
if hasattr(MessageCommands, "LOL_COUNTER"):
return
MessageCommands.MESSAGE_COMMANDS["ハラショー"] = harasyo
MessageCommands.MESSAGE_COMMANDS["いっそう"] = isso
MessageCommands.LOL_COUNTER = LolCounter(members)
MessageCommands.TYPO_COUNTER = Typo(members)
MessageCommands.MANUAL_JUDGE = ManualJudge(abc_emojis)
MessageCommands.PARTY_ICHIYO = PartyIchiyo(base_voice_channel, kikisen_channel)
MessageCommands.KAERE = Kaere(base_voice_channel, kikisen_channel, hakaba_voice_channel)