-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
216 lines (179 loc) · 8.51 KB
/
Copy pathmain.py
File metadata and controls
216 lines (179 loc) · 8.51 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
from tokens import TOKEN
import discord
from discord.ext import commands
import random, logging
import asyncio
import requests
from commands.get_weather import get_weather, get_weather_days
from commands.truth_dare import truth, dare, append_truth, append_dare, get_quote
from commands.translater import translate
from commands.db_defs import db_getfilename, db_loadpacks, update_results
from packs.pack_reader import load_pack
from random import shuffle
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
intents = discord.Intents.default()
intents.members = True
class Commands(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.receiving_answers = False
self.getting_packname = False
self.city = 'saratov'
self.packname = None
@commands.Cog.listener()
async def on_message(self, message):
if self.receiving_answers:
self.answers.append([message.content, message.author])
elif self.getting_packname:
self.packname = message.content
@commands.command(name='start_quiz')
async def start_quiz(self, ctx):
await ctx.send('Вы вошли в режим квиза, '
'стандартные команды бота теперь не работают.'
'Выберите пак:')
await asyncio.sleep(3)
await ctx.send(db_loadpacks())
self.getting_packname = True
while True:
await asyncio.sleep(5)
if self.packname:
break
self.getting_packname = False
filename = db_getfilename(self.packname)
greet_str, self.pack = load_pack(filename)
greet_string = f'Пак {greet_str[0]}, создатели: {greet_str[1]}, дата создания: {greet_str[2]}, ' \
f'количество раундов: {greet_str[3]}'
await ctx.send(greet_string)
await asyncio.sleep(5)
self.scores = {}
for round in range(greet_str[3]):
pic_name = self.pack[round][0][3]
await ctx.send(f'Раунд {self.pack[round][0][1]}', file=discord.File('gg.jpg'))
await asyncio.sleep(2)
for quest in range(1, int(self.pack[round][0][2]) + 1):
self.answers = []
print(self.pack[round])
question = self.pack[round][quest][0]
answers = self.pack[round][quest][1:]
answer = answers[-1]
r_ans = answers[-1]
shuffle(answers)
r_ans = answers.index(r_ans)
answers = [' '.join([str(i + 1), answers[i]]) for i in range(len(answers))]
print(answers)
answers = '\n'.join(answers)
await ctx.send(question)
await asyncio.sleep(10)
await ctx.send(f"```{answers}```")
self.receiving_answers = True
for sec in range(20, -1, -5):
await ctx.send(f"Осталось {sec} с")
await asyncio.sleep(5)
self.receiving_answers = False
if not self.answers:
await ctx.send('Никто не дал ответ')
else:
for ans, auth in self.answers:
r_auth = None
if ans == str(int(r_ans) + 1):
r_auth = auth
break
if not r_auth:
await ctx.send(f'Никто не дал правильный ответ. '
f'Правильный ответ: {answer}')
else:
await ctx.send(f'Правильно ответил {r_auth}')
if r_auth not in self.scores.keys():
self.scores[r_auth] = 1
else:
self.scores[r_auth] += 1
await ctx.send('Раунд окончен')
await asyncio.sleep(1)
if self.scores.keys():
await ctx.send('Итоги раунда:')
for user in self.scores.keys():
await ctx.send('Игрок \t Счет')
await ctx.send(f'{user} \t {self.scores[user]}\n')
for user in self.scores.keys():
update_results(self.packname, user, self.scores[user])
@commands.command(name='random')
async def my_randint(self, ctx, min_int, max_int):
num = random.randint(int(min_int), int(max_int))
await ctx.send(num)
@commands.command(name='set_timer')
async def timer(self, ctx, seconds):
resp = f"the timer starts in {seconds} seconds"
await ctx.send(resp)
await asyncio.sleep(int(seconds))
await ctx.send('the time x has come')
@commands.command(name='cat')
async def cat(self, ctx):
response = requests.get("https://api.thecatapi.com/v1/images/search")
json_response = response.json()
resp = json_response[0]["url"]
await ctx.send(resp)
@commands.command(name='dog')
async def dog(self, ctx):
response = requests.get("https://dog.ceo/api/breeds/image/random")
print(response)
json_response = response.json()
print(json_response)
resp = json_response["message"]
await ctx.send(resp)
@commands.command(name='place')
async def change_place(self, city='Moscow'):
self.city = city
@commands.command(name='current')
async def weather_rn(self, ctx):
await ctx.send(get_weather(self.city))
@commands.command(name='weather')
async def weather_with_days(self, ctx, days):
a = get_weather_days(days, self.city)
print(a)
for i in range(int(days)):
await ctx.send(f'{a[0][i]}, {a[1][i]}, {a[2][i]}, {a[3][i]}, {a[4][i]}')
@commands.command(name='truth')
async def truth(self, ctx, member):
await ctx.send(f'{member} {truth()}')
@commands.command(name='dare')
async def dare(self, ctx, member):
await ctx.send(f'{member} {dare()}')
@commands.command(name='append_truth')
async def append_t(self, ctx, *question):
append_truth(question)
await ctx.send('done!')
@commands.command(name='append_dare')
async def append_d(self, ctx, *action):
append_dare(action)
await ctx.send('done!')
@commands.command(name='translate')
async def translate(self, ctx, *words):
await ctx.send(translate(words[:-1], words[-1]))
@commands.command(name='quote')
async def quote(self, ctx):
text = get_quote().split('-')[0]
author = get_quote().split('-')[1]
await ctx.send(f"_{text}_ - {author}")
@commands.command(name='info')
async def help(self, ctx):
embed_obj = discord.Embed()
embed_obj.description = "текст"
await ctx.send(f"```set_timer - таймер, время указывается в секундах\n"
f"cat/dog - рандомные картинки с милыми животными\n"
f"place - смена местаположения для отображения погоды\n"
f"current - прогноз погоды на сегодня в указанном местоположении\n"
f"weather - прогноз погоды на несколько дней (до 8) в указанном местоположении\n"
f"random - рандомайзер чисел\n"
f"truth - задание для правды в правда или действие\n"
f"dare - задание для действия в правда или действие\n"
f"translate - перевод введенного текста, вводится сначала текст для перевода, "
f"потом язык, на который нужно перевести\n"
f"append_dare/append_truth - добавление заданий для правды или действия\n"
f"quote - цитаты известных```")
bot = commands.Bot(command_prefix='!', intents=intents)
bot.add_cog(Commands(bot))
bot.run(TOKEN)