-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
124 lines (101 loc) · 4.55 KB
/
Copy pathbot.py
File metadata and controls
124 lines (101 loc) · 4.55 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
import os
import discord
from discord.ext import commands
import aiohttp
import datetime
import re
# Bot configuration - Langsung masukkan nilai di sini
TOKEN = "YOUR_BOT_TOKEN_HERE" # Discord bot token
SOURCE_CHANNEL_ID = 123456789 # ID channel yang akan dimonitor
WEBHOOK_URL = "YOUR_WEBHOOK_URL_HERE" # Webhook URL channel tujuan
# Create bot instance
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Define theme colors for different message types
THEME_COLORS = {
'default': 0x2F3136, # Discord dark theme color
'market': 0x00B0F4, # Biru untuk market update
'risk': 0xED4245, # Merah untuk risk warning
'news': 0x57F287 # Hijau untuk news
}
def format_message(content):
"""Format message with better structure"""
# Split content into lines and remove empty ones
lines = [line.strip() for line in content.split('\n') if line.strip()]
formatted_lines = []
current_category = None
for line in lines:
# Detect market updates (usually contains specific keywords)
if any(keyword in line.lower() for keyword in ['yuan', 'pboc', 'market', 'trade', 'price']):
if current_category != '📊 MARKET UPDATE':
current_category = '📊 MARKET UPDATE'
formatted_lines.append(f"\n{current_category}")
formatted_lines.append(f"• {line}")
# Detect risk warnings
elif any(keyword in line.lower() for keyword in ['risk', 'hedge', 'warning', 'susceptible', 'leverage']):
if current_category != '⚠️ RISK WARNING':
current_category = '⚠️ RISK WARNING'
formatted_lines.append(f"\n{current_category}")
formatted_lines.append(f"• {line}")
# Everything else goes to news
else:
if current_category != '📰 NEWS':
current_category = '📰 NEWS'
formatted_lines.append(f"\n{current_category}")
formatted_lines.append(f"• {line}")
return '\n'.join(formatted_lines).strip()
def get_embed_color(content):
"""Determine embed color based on content"""
if any(word in content.lower() for word in ['risk', 'warning', 'hedge', 'susceptible']):
return THEME_COLORS['risk']
elif any(word in content.lower() for word in ['market', 'trade', 'price', 'yuan', 'pboc']):
return THEME_COLORS['market']
elif any(word in content.lower() for word in ['news', 'report', 'update']):
return THEME_COLORS['news']
return THEME_COLORS['default']
async def send_webhook(content, author, avatar_url=None):
"""Send message through webhook with enhanced formatting"""
# Format the content
formatted_content = format_message(content)
# Create embed with dynamic color
embed = discord.Embed(
description=formatted_content,
color=get_embed_color(content),
timestamp=datetime.datetime.utcnow()
)
# Set footer
embed.set_footer(text="© thetrader.id")
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(WEBHOOK_URL, session=session)
await webhook.send(embed=embed)
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
print(f'Monitoring channel ID: {SOURCE_CHANNEL_ID}')
@bot.event
async def on_message(message):
# Ignore messages from bots to prevent loops
if message.author.bot:
return
# Check if message is in the source channel
if message.channel.id == SOURCE_CHANNEL_ID:
# Forward the message with enhanced formatting
await send_webhook(
content=message.content,
author=message.author
)
# Handle attachments
if message.attachments:
files = [await attachment.to_file() for attachment in message.attachments]
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(WEBHOOK_URL, session=session)
embed = discord.Embed(color=THEME_COLORS['default'])
if any(att.filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp')) for att in message.attachments):
embed.set_image(url=message.attachments[0].url)
embed.set_footer(text="© thetrader.id")
await webhook.send(embed=embed, files=files)
await bot.process_commands(message)
# Run the bot
if __name__ == "__main__":
bot.run(TOKEN)