-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_webhook.py
78 lines (66 loc) · 3.07 KB
/
discord_webhook.py
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
import json
import requests
from result import Result,ResultInformations,ResultDetails
serversetting_default = {
'settingname': None,
'url': '',
'mode': 'battle',
'filter': 'none',
'targetscore': None,
'state': 'active',
'mybest': None
}
def deactivate_allbattles(settings: dict):
"""バトルモードの設定をすべてノンアクティブにする
"""
for target in settings.values():
if target['mode'] != 'battle':
continue
if target['state'] == 'active':
target['state'] = 'nonactive'
def post_result(djname: str, setting: dict, result: Result, imagevalue: bytes):
informations: ResultInformations = result.informations
details: ResultDetails = result.details
if details is None:
return None, None
contexts = [f'DJ NAME: **{djname}**']
if setting['mode'] != 'battle':
if informations is None:
return None, None
if setting['targetscore']['musicname'] != informations.music:
return None, None
if setting['targetscore']['playmode'] != informations.play_mode:
return None, None
if setting['targetscore']['difficulty'] != informations.difficulty:
return None, None
if details is None:
return None, None
if setting['mode'] == 'score':
if details.score.current is None or setting['mybest'] is not None and setting['mybest'] >= details.score.current:
return None, None
if setting['mode'] == 'misscount':
if details.miss_count.current is None or setting['mybest'] is not None and setting['mybest'] <= details.miss_count.current:
return None, None
if informations is not None:
if not None in [informations.music, informations.play_mode, informations.difficulty]:
contexts.append(f'**{informations.music}[{informations.play_mode}{informations.difficulty[0]}]**')
if setting['mode'] != 'misscount':
if details.score is not None and details.score.current is not None:
contexts.append(f'SCORE: **{details.score.current}**')
if setting['mode'] != 'score':
if details.miss_count is not None and details.miss_count.current is not None:
contexts.append(f'MISS COUNT: **{details.miss_count.current}**')
if details.options is not None:
option_arrange = details.options.arrange
contexts.append(f'option: **{option_arrange if option_arrange is not None else "正規"}**')
try:
data = {"content": '\n'.join(contexts)}
response = requests.post(setting['url'], data=json.dumps(data), headers={"Content-Type": "application/json"})
if response.status_code != 204:
return False, f'Error {response.status_code}'
response = requests.post(setting['url'], files={'file': ('result.png', imagevalue)})
if response.status_code != 200:
return False, f'Error {response.status_code}'
except Exception as ex:
return False, (f'Error({len(ex.args)})', *ex.args)
return True, 'Posted'