forked from blankkiri/heart-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
101 lines (81 loc) · 2.72 KB
/
Copy pathbot.py
File metadata and controls
101 lines (81 loc) · 2.72 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
import discord
from discord.ext import commands
import json
import os
from dotenv import load_dotenv
import re
from threading import Thread
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hey there! Bot is running ✅"
def run_web():
port = int(os.environ.get("PORT", 8080))
app.run(host="0.0.0.0", port=port)
# Load .env file
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
# Setup intents
intents = discord.Intents.default()
intents.message_content = True
intents.reactions = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
# Load hearts data
if os.path.exists("hearts.json"):
with open("hearts.json", "r") as f:
hearts = json.load(f)
else:
hearts = {}
def save_hearts():
"""Save hearts to file"""
with open("hearts.json", "w") as f:
json.dump(hearts, f, indent=4)
@bot.event
async def on_ready():
print(f"✅ Logged in as {bot.user}")
@bot.event
async def on_reaction_add(reaction, user):
# Ignore bot reactions
if user.bot:
return
# Only count heart emoji ❤️
if str(reaction.emoji) == "❤️":
message_author = str(reaction.message.author.id)
# Increment heart count
if message_author not in hearts:
hearts[message_author] = 0
hearts[message_author] += 1
save_hearts()
# Send message in same channel
await reaction.message.channel.send(
f"❤️ <@{message_author}> received a heart from {user.mention}! "
f"They now have **{hearts[message_author]} hearts**."
)
@bot.command()
async def hearts_of(ctx, member: discord.Member):
"""Check how many hearts a user has"""
count = hearts.get(str(member.id), 0)
await ctx.send(f"❤️ {member.mention} has {count} hearts!")
@bot.command()
async def tophearts(ctx):
"""Show leaderboard of top users"""
if not hearts:
await ctx.send("No hearts yet 💔")
return
sorted_hearts = sorted(hearts.items(), key=lambda x: x[1], reverse=True)
top = sorted_hearts[:5]
msg = "🏆 **Top Hearts Leaderboard** 🏆\n"
for i, (user_id, count) in enumerate(top, start=1):
msg += f"{i}. <@{user_id}> — ❤️ {count}\n"
await ctx.send(msg)
@bot.command()
async def count_expressions(ctx, message: str):
"""Count the number of expressions in a message"""
expressions = re.findall(r'\b\w+\b', message)
await ctx.send(f"The message contains {len(expressions)} expressions.")
if __name__ == "__main__":
# Start web server in a separate thread
Thread(target=run_web, daemon=True).start()
bot.run(TOKEN)