-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
115 lines (99 loc) · 4.81 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
import discord
from discord.ext import commands
import json
import os
import re # Import the regex module
# Your bot token
TOKEN = 'YOUR_BOT_TOKEN_HERE'
# Create an instance of a bot
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True # Enable message content intent
bot = commands.Bot(command_prefix='!', intents=intents)
# Dictionary to store user preferences
user_preferences = {}
# Load user preferences from a JSON file
def load_preferences():
global user_preferences
if os.path.exists('user_preferences.json'):
with open('user_preferences.json', 'r') as f:
user_preferences = json.load(f)
# Save user preferences to a JSON file
def save_preferences():
with open('user_preferences.json', 'w') as f:
json.dump(user_preferences, f, indent=4)
@bot.event
async def on_ready():
load_preferences()
print(f'Logged in as {bot.user.name} (ID: {bot.user.id})')
print('------')
# Set custom status
activity = discord.Game(name="!adsbx-help for commands")
await bot.change_presence(status=discord.Status.online, activity=activity)
@bot.command(name='adsbx-notify', help='Set the channel and keywords (regex supported) to monitor. Usage: !adsbx-notify #channel-name keyword1 keyword2 ...')
async def notify(ctx, channel: discord.TextChannel, *keywords):
user_id = ctx.message.author.id
if user_id not in user_preferences:
user_preferences[user_id] = {}
user_preferences[user_id]['channel'] = channel.name
user_preferences[user_id]['keywords'] = list(keywords)
save_preferences()
await ctx.author.send(f'You will be notified for keywords: {", ".join(keywords)} in {channel.mention}')
@bot.command(name='adsbx-show', help='Show your current keyword subscriptions. Usage: !adsbx-show')
async def show(ctx):
user_id = ctx.message.author.id
if user_id in user_preferences:
preferences = user_preferences[user_id]
channel = preferences.get('channel')
keywords = preferences.get('keywords', [])
await ctx.author.send(f'You are subscribed to keywords: {", ".join(keywords)} in #{channel}')
else:
await ctx.author.send('You have no keyword subscriptions.')
@bot.command(name='adsbx-remove', help='Remove keywords from your subscription. Usage: !adsbx-remove keyword1 keyword2 ...')
async def remove(ctx, *keywords):
user_id = ctx.message.author.id
if user_id in user_preferences:
for keyword in keywords:
if keyword in user_preferences[user_id]['keywords']:
user_preferences[user_id]['keywords'].remove(keyword)
save_preferences()
await ctx.author.send(f'Keywords {", ".join(keywords)} have been removed from your subscription.')
else:
await ctx.author.send('You have no keyword subscriptions.')
@bot.command(name='adsbx-help', help='Show a list of available commands and how to use them.')
async def help_command(ctx):
help_text = (
"**Available Commands:**\n"
"- **!adsbx-notify #channel-name keyword1 keyword2 ...** - Set the channel and keywords to monitor (regex supported).\n"
"- **!adsbx-show** - Show your current keyword subscriptions.\n"
"- **!adsbx-remove keyword1 keyword2 ...** - Remove keywords from your subscription.\n"
"- **!adsbx-help** - Show this help message."
)
await ctx.author.send(help_text)
@bot.command(name='adsbx-summary', help='Show a summary of all user subscriptions (admin only).')
@commands.has_permissions(administrator=True)
async def summary(ctx):
summary_text = "**User Subscriptions Summary:**\n"
for user_id, preferences in user_preferences.items():
user = await bot.fetch_user(user_id)
channel = preferences.get('channel')
keywords = preferences.get('keywords', [])
summary_text += f'- **User:** {user.name}#{user.discriminator} **Channel:** #{channel} **Keywords:** {", ".join(keywords)}\n'
await ctx.author.send(summary_text)
@bot.event
async def on_message(message):
if message.author == bot.user:
return
for user_id, preferences in user_preferences.items():
if 'channel' in preferences and message.channel.name == preferences['channel']:
for keyword in preferences.get('keywords', []):
if re.search(keyword, message.content, re.IGNORECASE): # Use regex to match keywords
await notify_user(message, keyword, user_id)
break
await bot.process_commands(message)
async def notify_user(message, keyword, user_id):
user = await bot.fetch_user(user_id)
message_link = f"https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id}"
await user.send(f'Keyword "{keyword}" mentioned in #{message.channel.name} by {message.author}: {message.content}\nLink: {message_link}')
# Run the bot
bot.run(TOKEN)