-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_v3.py
171 lines (142 loc) · 6.77 KB
/
main_v3.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
import difflib
import discord
import typing
from discord.ext import commands
from typing import Literal
import json
from pathlib import Path # Importing Path from pathlib for file operations
intents = discord.Intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix='/', intents=intents)
asked = False
global_sub = ""
global_answer = ""
user_points = {}
global_user_points = {}
asker = str()
####################################################################
def save_user_points():
with open('user_points.json', 'w') as file:
json.dump(user_points, file)
def load_user_points():
global user_points
try:
with open('user_points.json', 'r') as file:
user_points = json.load(file)
except FileNotFoundError:
pass
###################################################################
def save_global_user_points():
with open('global_user_points.json', 'w') as file:
json.dump(global_user_points, file)
def load_global_user_points():
global global_user_points
try:
with open('global_user_points.json', 'r') as file:
global_user_points = json.load(file)
except FileNotFoundError:
pass
####################################################################
@bot.event
async def on_ready():
print("Bot Ready")
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
except Exception as e:
print("error")
# Load user_points when the bot starts
load_user_points()
load_global_user_points()
@bot.tree.command(name='ask')
async def ask(interaction: discord.Interaction, *, question: str, answer: str, subject: Literal['Science', 'Maths', 'English', 'Sanskrit', 'Social Science']):
global asked
global global_answer
global asker
global global_sub
global_sub = subject
global_answer = answer
asker = interaction.user.name
if asked:
await interaction.response.send_message("There's already a question asked. Please answer it before asking a new one.", ephemeral=True)
else:
asked = True
embed = discord.Embed(
title=question,
description=f"Subject: {subject}",
color=discord.Color.blue()
)
embed.set_author(name=interaction.user.name, icon_url=interaction.user.avatar.url)
await interaction.response.send_message(embed=embed)
@bot.tree.command(name='answer')
async def answer(interaction: discord.Interaction, *, answer: str):
global asked
global global_answer
global asker
global global_sub
similarity = difflib.SequenceMatcher(None, answer.lower(), global_answer.lower()).ratio()
similarity_threshold = 0.65
if asker == interaction.user.name:
await interaction.response.send_message(f'{interaction.user.mention}, You cannot answer your own question! ')
else:
if not asked:
await interaction.response.send_message("There's no question to be answered currently, you can use **/ask** to ask questions")
else:
user_name = interaction.user.name
if similarity >= similarity_threshold:
#########################################################################################################################
if user_name not in user_points:
user_points[user_name] = {global_sub: 1} # Initialize points for the user and subject if not present
else:
if global_sub not in user_points[user_name]:
user_points[user_name][global_sub] = 1
else:
user_points[user_name][global_sub] += 1
##########################################################################################################################
if user_name not in global_user_points:
global_user_points[user_name] = {global_sub: 1} # Initialize points for the user and subject if not present
else:
if global_sub not in global_user_points[user_name]:
global_user_points[user_name][global_sub] = 1
else:
global_user_points[user_name][global_sub] += 1
#########################################################################################################################
await interaction.response.send_message(f'{interaction.user.mention} answered it. He gets +1 point.')
asked = False
save_global_user_points()
save_user_points() # Save user_points after each correct answer
else:
await interaction.response.send_message(f'{interaction.user.mention} tried, but it was incorrect.')
@bot.tree.command(name='leaderboard')
async def leaderboard(interaction, *, leaderboard_type: Literal['Lifetime', 'Temporary'] ,subject: Literal['Total', 'Science', 'Maths', 'English', 'Sanskrit', 'Social Science']):
global user_points
global global_user_points
def check_subject(a):
if subject=='Total':
leaderboard_message = "Leaderboard:\n```\n"
# Sort the user_points dictionary based on total points
sorted_leaderboard = sorted(a.items(), key=lambda x: sum(x[1].values()), reverse=True)
for position, (user, subjects) in enumerate(sorted_leaderboard, start=1):
total_points = sum(subjects.values())
leaderboard_message += f"{position}. {user}: {total_points} points\n"
leaderboard_message += "```"
return leaderboard_message
else:
leaderboard_message = f"{subject} Leaderboard:\n```\n"
def get_marks(user_data):
return user_data[1].get(subject, 0)
# Using sorted to sort users based on subject marks
sorted_leaderboard = sorted(a.items(), key=get_marks, reverse=True)
# Display the result
for position, (user, _) in enumerate(sorted_leaderboard, start=1):
points = a[user].get(subject, 0)
leaderboard_message += f"{position}. {user}: {points} points\n"
leaderboard_message += "```"
return leaderboard_message
if leaderboard_type== 'Lifetime':
lead_msg = check_subject(global_user_points)
elif leaderboard_type == 'Temporary':
lead_msg = check_subject(user_points)
await interaction.response.send_message(lead_msg)
# Run the bot
bot.run('Your API Key')