-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (84 loc) · 3.66 KB
/
main.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
import discord
from discord.ext import commands
from config import server_id,user_category_id,discord_bot_token
from intents import intents
intents.message_content = True
bot = commands.Bot(command_prefix="/",intents=intents)
@bot.event
async def on_ready():
try:
server = bot.get_guild(server_id)
if server:
category = discord.utils.get(server.categories, name="server")
send_to_channel = discord.utils.get(category.text_channels, name="server-logs")
await send_to_channel.send("Bot is online in cloud-server")
except:
pass
@bot.event
async def on_message(message):
print(bot.user.id)
userchannelid =str(message.author.id)
if message.author.id == bot.user.id:
return
try:
category_name = "userchat"
channel_name = message.channel.name
if (
message.channel.category and
message.channel.category.name == category_name
):
try:
user_id = int(channel_name)
user = await bot.fetch_user(user_id)
print(user)
try:
await user.send(message.content)
except:
if message.attachments:
for attachment in message.attachments:
await user.send(attachment.url)
print(f"Message sent to user {user.name}#{user.discriminator}.")
except ValueError:
print("Invalid user ID.")
except discord.NotFound:
print("User not found or user is not a member of a shared server.")
await bot.process_commands(message)
except Exception as e:
print("error " + str(e))
try:
if isinstance(message.channel, discord.DMChannel):
server = bot.get_guild(server_id)
if server:
category = discord.utils.get(server.categories, name=category_name)
if category:
channel_names = [channel.name for channel in category.channels if isinstance(channel, discord.TextChannel)]
print(channel_names)
if userchannelid in channel_names:
send_to_channel = discord.utils.get(category.text_channels, name=userchannelid)
try:
await send_to_channel.send(message.content)
except:
if message.attachments:
for attachment in message.attachments:
await send_to_channel.send(attachment.url)
else:
await category.create_text_channel(userchannelid)
send_to_channel = discord.utils.get(category.text_channels, name=userchannelid)
try:
await send_to_channel.send(message.content)
except:
if message.attachments:
for attachment in message.attachments:
await send_to_channel.send(attachment.url)
except Exception as x:
try:
server = bot.get_guild(server_id)
if server:
category = discord.utils.get(server.categories, name="staff-channel")
send_to_channel = discord.utils.get(category.text_channels, name="server-logs")
await send_to_channel.send(x)
except:
pass
print(x)
pass
bot.run()