-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
123 lines (105 loc) · 4.82 KB
/
bot.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
import disnake
from disnake.ext import commands
import sqlite3
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
import logging
logging.basicConfig(level=logging.INFO)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD_IDS = [int(guild_id.strip()) for guild_id in os.getenv('TEST_GUILD_IDS').split(',')]
intents = intents = disnake.Intents(messages=False, members=True, guilds=True, presences=True)
bot = commands.InteractionBot(intents=intents)
# SQLite setup
conn = sqlite3.connect('bot_data.db')
cursor = conn.cursor()
# Database structure setup
cursor.execute('''
CREATE TABLE IF NOT EXISTS cooldowns (
user_id INTEGER PRIMARY KEY,
last_notified TIMESTAMP
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS watched_titles (
title TEXT PRIMARY KEY
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS opted_out_users (
user_id INTEGER PRIMARY KEY
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS lfg_channel (
guild_id INTEGER PRIMARY KEY,
channel_id INTEGER
)
''')
conn.commit()
# Config commands setup
class LFGCommands(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.slash_command(name="lfgchannel", description="Set the LFG notification channel", guild_ids=GUILD_IDS)
@commands.has_permissions(manage_guild=True)
async def lfg_channel(self, ctx, channel: disnake.TextChannel):
cursor.execute("INSERT OR REPLACE INTO lfg_channel (guild_id, channel_id) VALUES (?, ?)",
(ctx.guild.id, channel.id))
conn.commit()
await ctx.send(f"Set the LFG notification channel to {channel.mention}!", ephemeral=True)
@commands.slash_command(name="watchtitle", description="Add a title to be watched for LFG notifications", guild_ids=GUILD_IDS)
@commands.has_permissions(manage_guild=True)
async def watch_title(self, ctx, title: str):
cursor.execute("INSERT INTO watched_titles (title) VALUES (?)", (title,))
conn.commit()
await ctx.send(f"Added '{title}' to watched titles.", ephemeral=True)
@commands.slash_command(name="unwatchtitle", description="Remove a title from being watched", guild_ids=GUILD_IDS)
@commands.has_permissions(manage_guild=True)
async def unwatch_title(self, ctx, title: str):
cursor.execute("DELETE FROM watched_titles WHERE title=?", (title,))
conn.commit()
await ctx.send(f"Removed '{title}' from watched titles.", ephemeral=True)
@commands.slash_command(name="optout", description="Opt-out of LFG notifications", guild_ids=GUILD_IDS)
async def opt_out(self, ctx):
cursor.execute("INSERT INTO opted_out_users (user_id) VALUES (?)", (ctx.author.id,))
cursor.execute("DELETE FROM cooldowns WHERE user_id=?", (ctx.author.id,))
conn.commit()
await ctx.send("You've opted out of LFG notifications.", ephemeral=True)
bot.add_cog(LFGCommands(bot))
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
# Activity monitor setup
@bot.event
async def on_presence_update(before, after):
cursor.execute("SELECT user_id FROM opted_out_users WHERE user_id=?", (after.id,))
if cursor.fetchone():
return
if after.activity:
cursor.execute("SELECT title FROM watched_titles")
watched_titles = {row[0] for row in cursor.fetchall()}
if after.activity.name in watched_titles:
# logging.info(f"Activity detected: {after.activity.name} for user {after.display_name}")
cursor.execute("SELECT last_notified FROM cooldowns WHERE user_id=?", (after.id,))
result = cursor.fetchone()
if result:
last_notified = datetime.fromisoformat(result[0])
if datetime.utcnow() - last_notified < timedelta(hours=1):
logging.info(f"Cooldown active for user {after.display_name}. Skipping notification.")
return
cursor.execute("SELECT channel_id FROM lfg_channel WHERE guild_id=?", (after.guild.id,))
lfg_channel_id = cursor.fetchone()
if lfg_channel_id:
lfg_channel = after.guild.get_channel(lfg_channel_id[0])
if lfg_channel:
# logging.info(f"Sending notification in {lfg_channel.name} for user {after.display_name}.")
await lfg_channel.send(f"{after.display_name} has started playing {after.activity.name}!")
cursor.execute("INSERT OR REPLACE INTO cooldowns (user_id, last_notified) VALUES (?, ?)",
(after.id, datetime.utcnow().isoformat()))
conn.commit()
else:
pass
# logging.info(f"NOT sending notification in #{lfg_channel_id} for user {after.display_name}.")
bot.run(TOKEN)