-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (73 loc) · 3.74 KB
/
main.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
from telegram import Update
from telegram.ext import Application, MessageHandler, CommandHandler, ConversationHandler, ContextTypes, filters
from supabase import create_client
from keys import key, SUPABASE_URL, SUPABASE_KEY
def words():
response = supabase.table("words").select("word").execute()
word_list = [row["word"] for row in response.data]
return word_list
# Start function to test if bot is working
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Starting to count...")
async def message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
text = update.message.text.lower()
word_list = words()
insert_list = []
# For loop to check for existence of banned word
for word in word_list:
if word in text:
# Count of instances
count = text.count(word) if word != "testword" else 0
values = {"username": update.message.from_user.username, "word": word, "count": count}
insert_list.append(values)
await update.message.reply_text(f"@{update.message.from_user.username} said '{word}' {count} time(s)")
data = supabase.table("records").insert(insert_list).execute()
async def score(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
response = supabase.table("score").select("username", "total_count").execute() # Changed the "*" to specific columns
# Try out optimised code, old one commented
scoreboard = "\n".join(f'@{row["username"]}: {row["total_count"]}' for row in response.data)
await update.message.reply_text(scoreboard)
INSERT = range(1)
async def addword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
word_list = words()
current_word_list = "\n".join(f'{word}' for word in word_list)
await update.message.reply_text(f"The current list:\n{current_word_list}\n\nSend the word you would like to add in this format - word1 word2 ...\n/cancel to end")
return INSERT
async def insertword(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
word_list = words()
text = update.message.text
# Format input to get a list of words
user_text_list = list(set(text.lower().split()))
insert_list = []
for user_text in user_text_list:
if user_text in word_list:
await update.message.reply_text(f"{user_text} is already in the list! Remove it and /addword again")
else:
insert_list.append({"word": user_text})
data = supabase.table("words").insert(insert_list).execute()
word_list = words()
current_word_list = "\n".join(f'{word}' for word in word_list)
await update.message.reply_text(f"The current list:\n{current_word_list}")
return ConversationHandler.END
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
word_list = words()
current_word_list = "\n".join(f'{word}' for word in word_list)
await update.message.reply_text(f"The current list:\n{current_word_list}")
return ConversationHandler.END
if __name__ == "__main__":
application = Application.builder().token(key).build()
# Create supabase client
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
conv_handler = ConversationHandler(
entry_points=[CommandHandler("addword", addword)],
states={
INSERT: [MessageHandler(filters.TEXT & ~filters.COMMAND, insertword)]},
fallbacks=[CommandHandler("cancel", cancel)]
)
application.add_handler(conv_handler)
# Register handlers
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("score", score))
application.add_handler(MessageHandler(filters.TEXT, message_handler))
# Start Bot
application.run_polling()