-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
262 lines (168 loc) · 6.21 KB
/
discord_bot.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
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
import discord
from discord.embeds import Embed
from discord.flags import Intents
import env
import math
import datetime
import time
import mylist
import lang
from discord import reaction
from discord.utils import get
intents = discord.Intents.all()
# env.pyというファイルを作ってなかにtoken = "YOUR TOKEN"としてください
message_id = None
voteMax = 5
text = lang.ja
client = discord.Client(intents = intents)
# 以下投票コマンド selectはリストだ!!!
async def vote(message, select):
global finalCount
global reactionContent
global voteMax
finalCount = {}
reactionContent = {}
await message.delete(delay = 3)
if len(select) > voteMax + 3:
await message.channel.send(text["attention_choices"] % voteMax)
return False
emb = discord.Embed(title = select[1])
vote_time = int(select[2])
dt = datetime.datetime.now() # 投票時間をフッターに表示
if vote_time < 60:
dt1 = dt + datetime.timedelta(minutes=vote_time)
dt1 = dt.replace(microsecond = 0)
emb.set_footer(text = text["this_vote"] + str(dt1) + text["is_end"] + str(vote_time) + text["after_min"])
elif vote_time >= 60:
h = math.floor(vote_time / 60)
m = vote_time % 60
dt1 = dt + datetime.timedelta(hours=h, minutes=m)
dt1 = dt.replace(microsecond = 0)
emb.set_footer(text = text["this_vote"] + str(dt1) + text["is_end"] + str(h) + text["after_hr"] + str(m) + text["after_min"])
for i in range(3):
select.pop(0) # リストの先頭を3回削除
for i, item in enumerate(select):
emb.add_field(name=str(i + 1) , value=item)
vote_message = await message.channel.send(embed=emb) # embメッセージ送信
global message_id
message_id = vote_message.id
# もし選択肢が一つ以下だったらyes or no ここから
if len(select) <= 1:
for i in range(2):
await vote_message.add_reaction(mylist.yon[i])
time.sleep(vote_time) # 一時停止
await vote_message.delete()
print("投票タイム終了") # 終了
return await vote_result_yon(message)
# ここまで
for i ,item in enumerate(select):
await vote_message.add_reaction(mylist.count[i])
time.sleep(vote_time) # 一時停止
await vote_message.delete()
print("投票タイム終了") # 終了
return await vote_result(message)
async def vote_result(message):
select = message.content.split()
title = select[1]
emb = discord.Embed(title = text["result"] + str(title))
for i in finalCount:
emb.add_field(name = select[i + 3] , value = int(finalCount[i]) - 1)
await message.channel.send(embed = emb)
async def vote_result_yon(message): # 選択肢が一つ股はない時の結果表示
select = message.content.split()
title = select[1]
txt = [text["agree"], text["disagree"]]
if len(select) == 4: # もし選択肢が一つあったら
subTitle = select[3]
emb = discord.Embed(title = text["result"] + str(title), description=str(subTitle))
else:
emb = discord.Embed(title = text["result"] + str(title))
for i in finalCount_yor:
emb.add_field(name = txt[i] , value = int(finalCount_yor[i]) - 1)
await message.channel.send(embed = emb)
async def whichVote(message):
global reactionContent
emb = discord.Embed(title = text["vote_to"])
for user in reactionContent.keys():
reactionContent[user] = list(set(reactionContent[user]))
emb.add_field(name = user, value = sorted(reactionContent[user], key= lambda i: mylist.countOrder.get(i, float("inf"))))
await message.channel.send(embed = emb)
emojiCount = {}
finalCount = {}
emojiCount_yon = {}
finalCount_yor = {}
for i, countEmoji in enumerate(mylist.count):
emojiCount[i] = countEmoji
for i, countEmoji_yon in enumerate(mylist.yon):
emojiCount_yon[i] = countEmoji_yon
reactionContent = {}
@client.event
async def on_reaction_add(reaction, user):
global message_id
global emojiCount
global emojiCount_yon
global finalCount
global finalCount_yor
global reactionContent
if reaction.message.id == message_id:
if reaction.emoji == "✅" or reaction.emoji == "❎": # 選択肢が一つの時の場合
for i in emojiCount_yon:
if reaction.emoji == emojiCount_yon[i]:
finalCount_yor[i] = reaction.count
if not user.bot:
reactionContent.setdefault(user.name, []).append(reaction.emoji)
for i in emojiCount:
if reaction.emoji == emojiCount[i]:
finalCount[i] = reaction.count
if not user.bot:
reactionContent.setdefault(user.name, []).append(reaction.emoji)
@client.event
async def on_reaction_remove(reaction, user):
if reaction.message.id == message_id:
if not user.bot:
for i in emojiCount:
if reaction.emoji == emojiCount[i]:
finalCount[i] = reaction.count
print(finalCount)
reactionContent[user.name].remove(reaction.emoji)
@client.event
async def on_ready():
print("ログインしました。")
@client.event
async def on_message(message):
global reactionContent
global text
if message.author.bot:
return
command = mylist.command
if message.content.split()[0] == command[0] or message.content.split()[0] == command[1]:
l = message.content.split()
vote_error = await vote(message, l)
if vote_error == False:
return
await whichVote(message)
if message.content.startswith(command[2]):
vmax = message.content.split()[1]
if not str.isdecimal(vmax):
await message.channel.send(text["attention_int"] % vmax)
return
if int(vmax) > 11:
await message.channel.send(text["attention_max"])
return
global voteMax
l = message.content.split()
voteMax = int(l[1])
await message.channel.send(text["change_max"] % voteMax)
if message.content.startswith(command[3]):
await whichVote(message)
if message.content.startswith(command[4]):
language = message.content.split()[1]
if language == "ja":
text = lang.ja
await message.channel.send(text["change_lang"] % language)
elif language == "en":
text = lang.en
await message.channel.send(text["change_lang"] % language)
else:
await message.channel.send(text["error_lang"])
client.run(env.token)