-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (181 loc) · 7.07 KB
/
Copy pathmain.py
File metadata and controls
221 lines (181 loc) · 7.07 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import discord
from discord.ext import commands
import logging
from dotenv import load_dotenv
import os
import json
from datetime import timedelta
load_dotenv()
token = os.getenv('DISCORD_TOKEN') # This is the discord token provided by the bot (put it in a .env file)
GUILD_ID = "YOUR_SERVER_ID" # Replace with your actual guild ID
ALERT_CHANNEL_ID = "CHANNEL_ALERT_ID" # Set this to your alert channel ID
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.reactions = True
bot = commands.Bot(command_prefix='!', intents=intents)
#Function to check in the console if the bot is online
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
#
REACTION_ROLE_FILE = "reaction_role_messages.json"
def save_reaction_role_messages():
with open(REACTION_ROLE_FILE, "w") as f:
json.dump(list(reaction_role_messages), f)
def load_reaction_role_messages():
if os.path.exists(REACTION_ROLE_FILE):
with open(REACTION_ROLE_FILE, "r") as f:
try:
ids = json.load(f)
return set(ids)
except Exception:
return set()
return set()
# Store all reaction-role message IDs (persistent)
reaction_role_messages = load_reaction_role_messages()
# Event to welcome new members to the server
@bot.event
async def on_member_join(member):
channel = bot.get_channel("WELCOME_CHANNEL_ID") # Replace with your welcome channel ID
if channel is not None:
await channel.send(
f"Welcome {member.mention} to the server! " #Here you can add a welcome message
)
# Helper function to send long messages in case the message exceeds Discord's limit
async def send_long_message(channel, text):
max_length = 2000
for i in range(0, len(text), max_length):
await channel.send(text[i:i+max_length])
@bot.command()
async def send_to_channel(ctx, channel_id: int, *, message: str):
try:
channel = bot.get_channel(channel_id)
if channel:
await send_long_message(channel, message)
await ctx.send(f"Message sent to channel {channel.name} (`{channel_id}`).")
else:
await ctx.send(f"Could not find channel with ID `{channel_id}`. Please check the ID and bot permissions.")
except discord.Forbidden:
await ctx.send(f"I don't have permission to send messages in channel `{channel_id}`.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
# Map emojis to role IDs
EMOJI_ROLE_MAP = {
# Replace ROLE_ID with the ID of the roles you want to assign
#This will be the reaction emojis the bot will add to the message
"🚀": "ROLE_ID",
"📱": "ROLE_ID",
}
# Usage: !setup_reaction_roles <channel_id> <-- This is how you set up the reaction roles
# Example: !setup_reaction_roles 123456789012345678
@bot.command()
@commands.has_permissions(manage_roles=True)
async def setup_reaction_roles(ctx, channel_id: int):
channel = bot.get_channel(channel_id)
if channel is None:
await ctx.send(f"Could not find channel with ID `{channel_id}`.")
return
message = await channel.send(
#Example of the message content
"React based on what department you chose\n\n"
"🚀 - Tech Department\n"
"📱 - Social Media Department\n"
)
# Add the reactions to the bot message
for emoji in EMOJI_ROLE_MAP.keys():
await message.add_reaction(emoji)
# Store the message ID for reaction tracking (persistent)
reaction_role_messages.add(message.id)
save_reaction_role_messages()
await ctx.send(f"Reaction roles set up in {channel.mention}.")
@bot.event
async def on_raw_reaction_add(payload):
print(f"Reaction add: emoji={payload.emoji}, message_id={payload.message_id}, user_id={payload.user_id}")
if payload.user_id == bot.user.id:
print("Ignored bot's own reaction.")
return
if payload.message_id not in reaction_role_messages:
print("Not the reaction role message.")
return
guild = bot.get_guild(payload.guild_id)
if guild is None:
print("Guild not found.")
return
print(f"Emoji from payload: {payload.emoji} | Map keys: {list(EMOJI_ROLE_MAP.keys())}")
role_id = None
for key in EMOJI_ROLE_MAP:
if str(payload.emoji) == key:
role_id = EMOJI_ROLE_MAP[key]
break
print(f"Role ID found: {role_id}")
if role_id is None:
print("Emoji not in map.")
return
role = guild.get_role(role_id)
if role is None:
print("Role not found in guild.")
return
member = guild.get_member(payload.user_id)
if member is None:
print("Member not found.")
return
try:
await member.add_roles(role)
print(f"Role {role.name} added to {member.name}.")
except Exception as e:
print(f"Failed to add role: {e}")
@bot.event
async def on_raw_reaction_remove(payload):
print(f"Reaction remove: emoji={payload.emoji}, message_id={payload.message_id}, user_id={payload.user_id}")
if payload.message_id not in reaction_role_messages:
print("Not the reaction role message.")
return
guild = bot.get_guild(payload.guild_id)
if guild is None:
print("Guild not found.")
return
print(f"Emoji from payload: {payload.emoji} | Map keys: {list(EMOJI_ROLE_MAP.keys())}")
role_id = None
for key in EMOJI_ROLE_MAP:
if str(payload.emoji) == key:
role_id = EMOJI_ROLE_MAP[key]
break
print(f"Role ID found: {role_id}")
if role_id is None:
print("Emoji not in map.")
return
role = guild.get_role(role_id)
if role is None:
print("Role not found in guild.")
return
member = guild.get_member(payload.user_id)
if member is None:
print("Member not found.")
return
try:
await member.remove_roles(role)
print(f"Role {role.name} removed from {member.name}.")
except Exception as e:
print(f"Failed to remove role: {e}")
# Timeout command to temporarily remove a member's ability to send messages
# Usage: !timeout @member <seconds> <reason>
#Exanple: !timeout @user 300 Spamming
@bot.command()
async def timeout(ctx, member: discord.Member, seconds: int, *, reason: str):
if not ctx.author.guild_permissions.manage_roles:
await ctx.send("You don't have the permission to use this command.")
return
if member == ctx.author:
await ctx.send("You cannot timeout yourself.")
return
try:
timeout_until = discord.utils.utcnow() + timedelta(seconds=seconds)
await member.timeout(timeout_until, reason=reason)
await ctx.send(f"{member.mention} has been timed out for {seconds} seconds. Reason: {reason}")
except discord.Forbidden:
await ctx.send("I do not have permission to timeout this user.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
bot.run(token, log_handler=handler, log_level=logging.DEBUG)