-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_bot.py
More file actions
190 lines (157 loc) Β· 6.07 KB
/
quick_bot.py
File metadata and controls
190 lines (157 loc) Β· 6.07 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
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
import os
import logging
from dotenv import load_dotenv
import discord
from discord.ext import commands, tasks
from discord import app_commands
from datetime import datetime
# ----------------------------
# Setup & Configuration
# ----------------------------
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
REMINDER_CHANNEL_ID = int(os.getenv("REMINDER_CHANNEL_ID", 0)) # π optional fallback channel
if not TOKEN:
raise RuntimeError("Set DISCORD_TOKEN in .env")
# Logging
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
logging.basicConfig(level=logging.DEBUG)
# Intents
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
# Bot
bot = commands.Bot(command_prefix="!", intents=intents)
# ----------------------------
# Task Manager
# ----------------------------
tasks_data = []
class MarkDoneView(discord.ui.View):
def __init__(self, task_id, owner_id):
super().__init__(timeout=None)
self.task_id = task_id
self.owner_id = owner_id
@discord.ui.button(label="Mark done β
", style=discord.ButtonStyle.green)
async def mark_done(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id != self.owner_id:
await interaction.response.send_message("This is not your task.", ephemeral=True)
return
for t in tasks_data:
if t["id"] == self.task_id and t["status"] == "pending":
t["status"] = "done"
await interaction.response.edit_message(
content=f"β
Task **{t['title']}** marked done.",
view=None
)
return
await interaction.response.send_message("Task not found or already done.", ephemeral=True)
# ----------------------------
# Slash Commands
# ----------------------------
@bot.tree.command(name="task_add", description="Add a new task with a title and due date")
@app_commands.describe(
title="Short title of the task",
due_date="Due date in format YYYY-MM-DD"
)
async def task_add(interaction: discord.Interaction, title: str, due_date: str):
# β
Validate date format
try:
due_date_obj = datetime.strptime(due_date, "%Y-%m-%d").date()
except ValueError:
await interaction.response.send_message(
"β Invalid date format. Please use `YYYY-MM-DD` (e.g., `2025-09-20`).",
ephemeral=True
)
return
# β
Check if due date is in the past
if due_date_obj < datetime.utcnow().date():
await interaction.response.send_message(
"β οΈ The due date cannot be in the past.",
ephemeral=True
)
return
task_id = len(tasks_data) + 1
tasks_data.append({
"id": task_id,
"title": title,
"assignee": interaction.user.id,
"due_date": str(due_date_obj),
"status": "pending",
"created_at": datetime.utcnow().isoformat(),
"reminded": False
})
view = MarkDoneView(task_id, interaction.user.id)
await interaction.response.send_message(
f"Task added: **{title}** (due: {due_date_obj})",
view=view
)
@bot.tree.command(name="tasks_my", description="List your pending tasks")
async def tasks_my(interaction: discord.Interaction):
user_tasks = [t for t in tasks_data if t["assignee"] == interaction.user.id and t["status"] == "pending"]
if not user_tasks:
await interaction.response.send_message("You have no pending tasks.")
return
embed = discord.Embed(title=f"{interaction.user.name}'s tasks", color=discord.Color.blue())
for t in user_tasks:
embed.add_field(name=t["title"], value=f"Due: {t['due_date']} | ID: {t['id']}", inline=False)
await interaction.response.send_message(embed=embed)
# ----------------------------
# Reminder System
# ----------------------------
@tasks.loop(minutes=1)
async def task_reminder_loop():
today = datetime.utcnow().date()
for t in tasks_data:
if t["status"] != "pending" or t["reminded"]:
continue
due_date = datetime.strptime(t["due_date"], "%Y-%m-%d").date()
days_left = (due_date - today).days
if days_left <= 1: # today or tomorrow
user = await bot.fetch_user(t["assignee"])
reminder_message = f"β° Reminder: Your task **{t['title']}** is due on **{t['due_date']}**!"
try:
await user.send(reminder_message)
print(f"Sent DM to {user.name} for task {t['id']}")
except discord.Forbidden:
print(f"Could not DM {user.name}, trying fallback channel...")
if REMINDER_CHANNEL_ID:
channel = bot.get_channel(REMINDER_CHANNEL_ID)
if channel:
await channel.send(f"{user.mention} {reminder_message}")
t["reminded"] = True # avoid duplicate reminders
# ----------------------------
# Moderation & Events
# ----------------------------
@bot.event
async def on_member_join(member):
try:
await member.send(f"Welcome to the server, {member.name}!")
except discord.Forbidden:
print(f"Could not DM {member.name}")
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if "shit" or "fuck" in message.content.lower():
await message.delete()
await message.channel.send(f"{message.author.mention} - Don't use that word again!")
await bot.process_commands(message)
# ----------------------------
# Lifecycle Events
# ----------------------------
async def setup_hook():
try:
synced = await bot.tree.sync()
print(f"β
Synced {len(synced)} slash commands")
except Exception as e:
print(f"β Failed syncing commands: {e}")
task_reminder_loop.start()
bot.setup_hook = setup_hook
@bot.event
async def on_ready():
print(f"Bot ready: {bot.user} (id: {bot.user.id})")
# ----------------------------
# Run Bot
# ----------------------------
if __name__ == "__main__":
bot.run(TOKEN, log_handler=handler, log_level=logging.DEBUG)