generated from kkrypt0nn/Python-Discord-Bot-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
219 lines (200 loc) · 8.18 KB
/
stats.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
""""
Samuro Bot
Автор: *fennr*
github: https://github.com/fennr/Samuro-HotsBot
Бот для сообществ по игре Heroes of the Storm
"""
import os
from discord import Embed, Member, File
from discord.ext import commands
from utils import check
from enum import Enum
import openpyxl
from openpyxl.styles import Font
from openpyxl.worksheet.dimensions import ColumnDimension, DimensionHolder
from openpyxl.utils import get_column_letter
from utils.classes import Const
from utils import exceptions, library
from utils.classes.Const import config
class League(Enum):
Bronze = "Бронза"
Silver = "Серебро"
Gold = "Золото"
Platinum = "Платина"
Diamond = "Алмаз"
Master = "Мастер"
Grandmaster = "Грандмастер"
class Stats(commands.Cog, name="Stats"):
"""
— Просмотр таблиц лидеров
"""
def __init__(self, bot):
self.bot = bot
@commands.group(name="top")
async def top(self, ctx):
if ctx.invoked_subcommand is None:
pass
@top.command(name="excel")
@check.is_samuro_dev()
async def top_excel(self, ctx):
headings = ['id', 'guild_id', 'Победы', 'Поражения', 'Очки', 'Батлтег']
filepath = 'UserStats.xlsx'
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
select = Const.selects.US
cur.execute(select, (guild_id, ))
data = cur.fetchall()
cur.close()
# Создание документа, страницы
wb = openpyxl.Workbook()
ws = wb.active
# Spreadsheet row and column indexes start at 1
# so we use "start = 1" in enumerate so
# we don't need to add 1 to the indexes.
for colno, heading in enumerate(headings, start=1):
c = ws.cell(row=1, column=colno)
c.font = Font(bold=True)
c.value = heading
# This time we use "start = 2" to skip the heading row.
for rowno, row in enumerate(data, start=2):
for colno, cell_value in enumerate(row, start=1):
ws.cell(row=rowno, column=colno).value = cell_value
# Выравнивание длины колонок
dim_holder = DimensionHolder(worksheet=ws)
for col in range(ws.min_column, ws.max_column + 1):
dim_holder[get_column_letter(col)] = ColumnDimension(ws, min=col, max=col, width=20)
ws.column_dimensions = dim_holder
# сохранение, вывод, удаление файла
wb.save(filepath)
await ctx.author.send(file=File(filepath))
os.remove(filepath)
await ctx.send("Файл отправлен личным сообщением")
@top.command(name="mmr")
async def top_mmr(self, ctx, league_type="Грандмастер", count=5):
"""
- Лидеры по ммр
"""
if count > 15:
count = 15
test = library.get.league(league_type.capitalize())
league = League(test)
print(league, league.name, league.value)
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
select = Const.selects.PlayersLeague
cur.execute(select, (league.name, count))
records = cur.fetchall()
embed = Embed(
title=f"Таблица лиги {league.value}",
color=config.info
)
value = ""
for i, record in enumerate(records):
value += f"{i + 1}. {library.get.mention(record.id)} (mmr: {record.mmr})\n"
if len(value) > 0:
embed.add_field(
name=f"Топ {count} игроков",
value=value
)
await ctx.send(embed=embed)
else:
await ctx.send("Нет игроков выбранной лиги")
@top.command(name="wins")
async def top_wins(self, ctx, count=10):
"""
- Лидеры по числу побед
"""
if count > 15:
count = 15
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
select = Const.selects.USWins
cur.execute(select, (guild_id, count))
records = cur.fetchall()
embed = Embed(
title=f"Таблица лидеров",
color=config.info
)
value = ""
for i, record in enumerate(records):
value += f"{i + 1}. {library.get.mention(record.id)} — {record.win}\n"
embed.add_field(
name=f"Топ {count} игроков по числу побед",
value=value
)
await ctx.send(embed=embed)
@top.command(name="points")
async def top_points(self, ctx, count=10):
"""
- Лидеры по заработанным очкам
"""
if count > 15:
count = 15
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
select = Const.selects.USPoints
cur.execute(select, (guild_id, count, ))
records = cur.fetchall()
embed = Embed(
title=f"Таблица лидеров",
color=config.info
)
value = ""
# max_indent = len(max([self.bot.get_user(x.id).name for x in records])) + 1 # получать макс длину имени
for i, record in enumerate(records):
value += f"{i+1}. {library.get.mention(record.id)} — {record.points}\n"
embed.add_field(
name=f"Топ {count} игроков по числу баллов",
value=value
)
await ctx.send(embed=embed)
@top.command(name="remove")
@commands.check_any(commands.has_role(703884580041785344), # Создатель
commands.has_role(703884637755408466), # Админ
commands.has_role(711230509967212564), # Старший модер
commands.has_role(711230315540250624), # Модер
commands.has_role(959144584720580618), # Samuro_dev
commands.has_role(880865537058545686), # test
)
async def points_remove(self, ctx, user, count=0):
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
user_id = library.get.user_id(user)
select = Const.selects.USIdGuild
cur.execute(select, (user_id, guild_id))
record = cur.fetchone()
stats = library.get.stats(record)
if stats.points < count:
await ctx.send(f"Недостаточно баллов\n"
f"Баллов у {library.get.mention(stats.id)}: {stats.points}")
else:
stats.points -= count
update = Const.updates.USPoints
cur.execute(update, (stats.points, stats.id, stats.guild_id))
library.commit(con)
await ctx.send(f"Баллы успешно сняты\n"
f"Осталось баллов: {stats.points}")
@top.command(name="end_season")
@check.is_owner()
async def end_season(self, ctx):
con, cur = library.get.con_cur()
guild_id = library.get.guild_id(ctx)
update = Const.updates.USGuildRemoveStats
cur.execute(update, (guild_id, ))
library.commit(con)
row_count = cur.rowcount
if row_count > 0:
await ctx.send(f"Статистика профилей удалена\nОбновлено профилей: {row_count}")
else:
await ctx.send(f"Не обновлено ни одного профиля")
@points_remove.error
@top_mmr.error
async def points_handler(self, ctx, error):
error = getattr(error, 'original', error) # получаем пользовательские ошибки
print(error)
if isinstance(error, commands.errors.MissingRole):
await ctx.send("Недостаточно прав для выполнения команды")
elif isinstance(error, exceptions.WrongLeague):
await ctx.send("Выберите корректную лигу")
def setup(bot):
bot.add_cog(Stats(bot))