-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
114 lines (93 loc) · 3.35 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
import discord
import random
import asyncio
import logging
import os
from dotenv import load_dotenv
from asyncio import Semaphore, Queue
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(message)s",
)
if os.path.exists(".env"):
load_dotenv()
else:
logging.error("No .env file found.")
# since v1.0.3 configuration was moved to .env
use_proxy = os.getenv("USE_PROXY") == "True"
proxy = os.getenv("PROXY")
allowed_ids = list(map(int, os.getenv("ALLOWED_IDS").split(",")))
reactions = os.getenv("REACTIONS").split(",")
min_delay = int(os.getenv("MIN_DELAY"))
max_delay = int(os.getenv("MAX_DELAY"))
user_token = os.getenv("USER_TOKEN")
semaphore = Semaphore(5)
if use_proxy:
client = discord.Client(chunk_guild_at_startup=False, proxy=proxy)
logging.info(f"Using proxy: {proxy}")
else:
client = discord.Client(chunk_guild_at_startup=False)
message_queue = Queue()
async def add_reactions(message):
async with semaphore:
message_deleted = False
for reaction in reactions:
if message_deleted:
break
try:
await message.add_reaction(reaction)
delay = random.randint(min_delay, max_delay)
await asyncio.sleep(delay)
except discord.errors.NotFound:
logging.error("Message not found. Skipping reactions.")
message_deleted = True
except discord.errors.Forbidden:
logging.error("Permisssions error. Skipping reactions.")
message_deleted = True
except discord.errors.HTTPException:
logging.warning("Rate limit or network error. Retrying after delay.")
await asyncio.sleep(10)
await message.add_reaction(reaction)
except Exception as e:
logging.exception(
f"An unexpected error occurred while adding reaction: {e}"
)
async def process_messages():
while True:
message = await message_queue.get()
await add_reactions(message)
message_queue.task_done()
@client.event
async def on_message(message):
if message.channel.id in allowed_ids:
await message_queue.put(message)
@client.event
async def on_ready():
try:
logging.info(f"Logged in as {client.user}")
asyncio.create_task(process_messages())
except discord.LoginFailure:
logging.warning("Login failed. Make sure you're using a valid user token.")
except Exception as e:
logging.exception(f"An error occurred while running the bot: {e}")
async def main():
try:
if not user_token:
raise ValueError("No user token provided")
logging.info(f"Trying to login with the provided token, stay tuned...")
await client.start(user_token)
except ValueError as ve:
logging.error(ve)
except discord.LoginFailure:
logging.error("Login failed. Make sure you're using a valid user token.")
except Exception as e:
logging.exception(f"An error occurred while running the bot: {e}")
finally:
await client.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nCtrl+C received, exiting...")
except Exception as e:
logging.exception("An unexpected error occurred during execution: %s", e)