-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (146 loc) · 6.82 KB
/
main.py
File metadata and controls
164 lines (146 loc) · 6.82 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
import discord
import json
import re
# import urllib.request as req
import sys
import pickle
from currency import *
from coinmarketcap import Market
from zenhan import z2h
# from bs4 import BeautifulSoup
from twitter import Twitter, OAuth
from pathlib import Path
# 本番token
token = "NDA1MzY1ODI0NDQyOTkwNTky.DUjV6A.kVeYsW0rldoLX4BtKczQCiXqI58"
# pettyaテストtoken
# token = "NDA0NjE4MDA4MjA0NTQxOTYy.DUoAtQ.DqDyvVDhSIQSMD-KNRtx86WKRgo"
# yufiテストtoken
# token = "NDA3NTYwNTkxNDM2NDE0OTg2.DVK3gg.3tFv-GiJ0le-qYGGFynn5xARa4A"
# 通貨変換対象リスト
currency_list = ['?btc', '?eth', '?xem', '?諭吉']
# チャンネルID
technology = "405377859662774281"
othercoin = "404641206996303879"
#bot = '426417253584601098'
bot = '424712532184399912' # testchannel
restriction_channel = [technology, othercoin, bot]
client = discord.Client()
client.get_all_members()
market = Market()
# Twitter Auth
t = Twitter(auth=OAuth(
'935058768563339265-7eIsN2892DRYL6tCrTjzPGnySQQUHfr',
'G3xDy4lUsRBQbR3GQ1ZEF7ankZgNY64gBpf7x0edPwBxF',
'xMzUeWmIimG83P9QMhFnENCG4',
'REDBG6CH9LkiR5ACBgB5hNBEYnwP00Als7TLi39WZCSdS3qLcG'
))
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
global element
element = "{"
han_message = z2h(message.content)
# 送り主がBotだった場合反応したくないので
if client.user != message.author:
if message.content.startswith("?エラリスト") | message.content.startswith("?エラリスト"):
count = 0
for member in client.get_all_members():
count += 1
msg = "報告します! 現在 " + str(count) + "人のエラリストが参加中です!"
# メッセージが送られてきたチャンネルへメッセージを送ります
await client.send_message(message.channel, msg)
elif han_message.lower() in currency_list:
src = han_message.lower().replace("?", "") + ".getBnk"
if "諭吉" in src:
src = src.replace("諭吉", "jpy")
msg = eval(src)()
# 価格のメッセージを出力
await client.send_message(message.channel, msg)
if message.channel.id in restriction_channel:
# 送られてきたメッセージの引数が2つあった場合
if len(han_message.split(" ")) == 2:
try:
# 仮想通貨リストファイルのPATH
path = 'name_conv_list.txt'
# ファイルの読み込み
f = open(path, 'r')
# JSON 形式で読み込む
json_data = json.load(f)
# ファイルクローズ
f.close()
# float型に変換可能(実数)かどうかの確認に正規表現を使う
num_reg = re.compile("^\d+(\.\d+)?\Z")
# メッセージから引数を取得
args = han_message.split(" ")
key = args[0].replace("?", "").upper()
# 第1引数のkeyがjson_dataの中にあり、かつ、第2引数が実数なら
# coin * 枚数を計算
if json_data.get(key) is not None and bool(num_reg.match(args[1])):
# coinmarketcapから価格を取得
conv_coin = json_data[key].replace(" ", "-")
coin = market.ticker(conv_coin, convert='JPY')[0]
if coin.get('error') is None:
price = float(coin['price_jpy']) * float(args[1])
msg = "Coinmarketcap:" + str(args[1]) + key + "は" + str("{:,.3f}".format(round(price, 3))) + "円です。"
# 価格のメッセージを出力
await client.send_message(message.channel, msg)
else:
msg = "Coinmarketcapに" + key + "は対応していません"
await client.send_message(message.channel, msg)
except IOError:
print("ファイルがありません")
except:
return
if message.channel.id == technology:
if han_message.lower() == "!conv":
# coinmarketcapから価格を取得
coin = market.ticker(limit=0)
for i in range(len(coin)):
keys = '"' + coin[i]['symbol'] + '"'
values = '"' + coin[i]['name'] + '"'
if i != len(coin) - 1:
element = element + keys + ":" + values + ","
else:
element = element + keys + ":" + values + "}"
f = open('name_conv_list.txt', 'w')
f.write(element)
f.close()
msg = "name_conv_list.txtを作成しました。"
await client.send_message(message.channel, msg)
# elif han_message.lower() == "!down_name":
# await client.send_file(message.channel, 'name_conv_list.txt')
# 再起動コマンド追加
if message.content.startswith('reboot'):
await client.send_message(message.channel, 'BOTを再起動します')
sys.exit()
if message.channel.id == bot:
if message.content.startswith("?最新情報") | message.content.startswith("?最新情報"):
# Get list from Twitter
# BankeraJP ID
user_id = '908504581797113856'
# Get Tweet
count = 1
# Get Hist File
hist_file = Path("last.tweet")
# Get TimeLine
aTimeLine = t.statuses.user_timeline(user_id=user_id, count=count)
msg_t = aTimeLine[0]['text']
if hist_file.is_file():
read_hist = open('last.tweet', 'rb')
load = pickle.load(read_hist)
read_hist.close
if msg_t != load:
await client.send_message(message.channel, msg_t)
else:
await client.send_message(message.channel, '前回と内容が一緒です')
else:
await client.send_message(message.channel, msg_t)
write_hist = open('last.tweet', 'wb')
pickle.dump(msg_t, write_hist)
write_hist.close
client.run(token)