This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoint.py
132 lines (126 loc) · 4.24 KB
/
Point.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
from typing import List, Union
import discord
import sqlite3
import random
import Util
import asyncio
from discord.ext import commands
class Point(commands.Cog):
"""
Contains commands for checking user's Bluelearn Coins balance and leaderboard
"""
def __init__(self, client):
self.client = client
@commands.command(name = 'give_coins', aliases = ['give_points'])
@commands.has_role('SleepBot Admin')
async def give_coins(self, ctx, target: Union[discord.User,discord.Role] = None, amount = 0):
if type(target) == discord.Role:
targets:List(discord.User) = target.members
t:discord.Member
for t in targets:
await Util.command_log(self.client, ctx, "give_points")
if t == None :
return
if not t.bot:
if t.id in Util.POINT:
Util.POINT[t.id] += amount
else:
Util.POINT[t.id] = amount
embed = discord.Embed(title = "Bluelearn Points", description = f"{amount} points have been given to {t.mention}", color = discord.Color.green())
# await ctx.send(embed = embed)
am = discord.AllowedMentions(
users=False,
)
await ctx.send(f"Gave {t.mention} {amount} coins.", allowed_mentions=am)
# await ctx.send(f'Gave {t.nick} {amount} coins')
else:
await Util.command_log(self.client, ctx, "give_points")
if target == None:
return
if target.id in Util.POINT:
Util.POINT[target.id] += amount
else:
Util.POINT[target.id] = amount
await ctx.send(f'Gave {target.mention} {amount} coins')
@commands.command(name='coins',
aliases=['point', 'points', 'coin'],
help="Shows the Coins that the mentioned user has.")
@Util.is_point_cmd_chnl()
async def coins(self, ctx, target: discord.Member = None):
await Util.command_log(self.client, ctx, "points")
total_point = dict(Util.POINT)
for user in Util.DB_POINT:
if user[0] in total_point:
total_point[user[0]] += user[1]
else:
total_point[user[0]] = user[1]
if target == None :
await ctx.send("Mention a user to check points.")
return
else:
msg = await ctx.send(embed = Util.loading_embed)
if not (target.id in total_point):
total_point[target.id] = 0
embed = discord.Embed(title = "User {}'s Coins".format(target.name),
description = "{} has {} coins since reset.".format(target.name,total_point[target.id]),
colour = random.randint(0,0xffffff)
)
await msg.edit(embed = embed)
return
@coins.error
async def coins_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("Points command can only be used in <#{}> channel!".format(Util.POINTCMD))
else:
await Util.ErrorHandler(ctx, error)
@commands.command(name='top',
aliases=['leaderboard', 'all_coins', 'all_points', 'lb'],
help="Shows the top 20 users in leaderboard.")
@Util.is_point_cmd_chnl()
async def top(self, ctx):
await Util.command_log(self.client, ctx, "top")
msg = await ctx.send(embed = Util.loading_embed)
total_point = dict(Util.POINT)
for user in Util.DB_POINT:
if user[0] in total_point:
total_point[user[0]] += user[1]
else:
total_point[user[0]] = user[1]
top_20 = 1
total_point = sorted(total_point.items(), key = lambda kv: kv[1])
total_point.reverse()
embed = discord.Embed(title = "Coins Leaderboard",
description = "Top Coins Since Last Reset.",
colour = random.randint(0,0xffffff)
)
for user in total_point:
username = self.client.get_user(int(user[0]))
if username == None:
continue
embed.add_field(name = "{} : {}".format(top_20, username.display_name), value = user[1])
top_20 += 1
if top_20 == 21:
break
await msg.edit(embed = embed)
@top.error
async def top_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("Points command can only be used in <#{}> channel!".format(Util.POINTCMD))
else:
await Util.ErrorHandler(ctx, error)
@commands.Cog.listener()
async def on_message(self, ctx):
if not Util.is_point_chnl(ctx):
return
if ctx.author.bot:
return
member = await ctx.guild.fetch_member(ctx.author.id)
if random.randint(0,200) == 0:
emoji = '🎖'
await ctx.add_reaction(emoji)
if ctx.author.id in Util.POINT:
Util.POINT[ctx.author.id] += 5
else:
Util.POINT[ctx.author.id] = 5
def setup(client):
client.add_cog(Point(client))