-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (97 loc) · 4.71 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import discord
from discord.ext import commands
import pandas as pd
from pytz import timezone
from datetime import datetime
from dotenv import load_dotenv
import os
from pymongo import MongoClient
import sys
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
MONGODB_URI = os.getenv('MONGODB_URI')
# Leitura das datas de início e fim
START_DATE_STR = os.getenv('START_DATE', '2024-01-01')
END_DATE_STR = os.getenv('END_DATE', '2024-12-31')
# Converte as strings de data no formato YYYY-MM-DD para objetos datetime
start_date = datetime.strptime(START_DATE_STR, '%Y-%m-%d')
end_date = datetime.strptime(END_DATE_STR, '%Y-%m-%d')
DATABASE_NAME = 'discord_bot-2024'
COLLECTION_NAME = '2024-01-01 a 2024-12-31'
client = MongoClient(MONGODB_URI)
db = client[DATABASE_NAME]
collection = db[COLLECTION_NAME]
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
messages_data = []
saopaulo_tz = timezone('America/Sao_Paulo')
@bot.event
async def on_ready():
try:
print(f'Logged in as {bot.user.name}')
print(f'Coletando mensagens entre {start_date} e {end_date}')
for guild in bot.guilds:
print(f"Coletando mensagens do servidor: {guild.name}")
for channel in guild.text_channels:
# Histórico do canal no intervalo de datas
async for message in channel.history(after=start_date, before=end_date):
if isinstance(message.author, discord.Member):
role_name = message.author.top_role.name if message.author.top_role else "None"
else:
role_name = "None"
messages_data.append({
"Message": message.content,
"Message Datetime": message.created_at.astimezone(saopaulo_tz),
"User": message.author.name,
"Role": role_name,
"Channel": channel.name,
"Thread": "None",
"Server Name": guild.name,
"Category": channel.category.name if channel.category is not None else "None"
})
# Histórico das threads do canal no intervalo de datas
for thread in channel.threads:
async for message in thread.history(after=start_date, before=end_date):
if isinstance(message.author, discord.Member):
role_name = message.author.top_role.name if message.author.top_role else "None"
else:
role_name = "None"
messages_data.append({
"Message": message.content,
"Message Datetime": message.created_at.astimezone(saopaulo_tz),
"User": message.author.name,
"Role": role_name,
"Channel": channel.name,
"Thread": thread.name,
"Server Name": guild.name,
"Category": channel.category.name if channel.category is not None else "None"
})
# Histórico de fóruns (nem todo servidor possui)
if hasattr(guild, 'forums') and guild.forums:
for forum in guild.forums:
for thread in forum.threads:
async for message in thread.history(after=start_date, before=end_date):
if isinstance(message.author, discord.Member):
role_name = message.author.top_role.name if message.author.top_role else "None"
else:
role_name = "None"
messages_data.append({
"Message": message.content,
"Message Datetime": message.created_at.astimezone(saopaulo_tz),
"User": message.author.name,
"Role": role_name,
"Channel": forum.name,
"Thread": thread.name,
"Server Name": guild.name,
"Category": forum.category.name if forum.category is not None else "None"
})
if messages_data:
collection.insert_many(messages_data)
print(f'{len(messages_data)} mensagens foram inseridas no MongoDB.')
await bot.close()
sys.exit(0)
except Exception as e:
print(f'Ocorreu um erro: {e}')
await bot.close()
sys.exit(1)
bot.run(DISCORD_TOKEN)