lunarflu HF staff commited on
Commit
b367fc9
1 Parent(s): 2ca1cec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -78
app.py CHANGED
@@ -20,23 +20,20 @@ from pytz import timezone # for times
20
  import asyncio # check if used
21
 
22
  zurich_tz = timezone("Europe/Zurich")
23
- intents = discord.Intents.all()
24
- bot = commands.Bot(command_prefix='!', intents=intents)
25
- LOG_CHANNEL = bot.get_channel(1107006391547342910)
26
- print(f"LOG_CHANNEL: {LOG_CHANNEL}")
27
- # test = 1126516932527534111
28
- # bot-test = 1100458786826747945
29
 
30
  def convert_to_timezone(dt, tz):
31
  return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
32
 
 
 
 
 
 
33
 
34
  #rate_limiter = RateLimiter(max_calls=10, period=60) # needs testing
35
 
36
  # todo
37
  '''
38
- Make sure only can post in #bot-test
39
-
40
  discord.gg / discord/invite (block completely, need to be cautious)
41
  attempted @everyone / @here pings, or trying to use those strings (ping @alerts)
42
  ping alerts for list of keywords
@@ -52,86 +49,73 @@ ping alerts for list of keywords
52
 
53
  @bot.event
54
  async def on_message_edit(before, after):
55
- try:
56
- if before.author == bot.user:
57
- return
58
-
59
- if before.content != after.content:
60
- embed = Embed(color=Color.orange())
61
- embed.set_author(name=f"{before.author} ID: {before.author.id}", icon_url=before.author.avatar.url)
62
- embed.title = "Message Edited"
63
- embed.description = f"**Before:** {before.content or '*(empty message)*'}\n**After:** {after.content or '*(empty message)*'}"
64
- embed.add_field(name="Author Username", value=before.author.name, inline=True)
65
- embed.add_field(name="Channel", value=before.channel.mention, inline=True)
66
- #embed.add_field(name="Message Created On", value=before.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
67
- embed.add_field(name="Message Created On", value=convert_to_timezone(before.created_at, zurich_tz), inline=True)
68
- embed.add_field(name="Message ID", value=before.id, inline=True)
69
- embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{before.guild.id}/{before.channel.id}/{before.id})", inline=True)
70
-
71
- if before.attachments:
72
- attachment_urls = "\n".join([attachment.url for attachment in before.attachments])
73
- embed.add_field(name="Attachments", value=attachment_urls, inline=False)
74
-
75
- #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
76
- embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
77
-
78
- if LOG_CHANNEL is not None:
79
- await LOG_CHANNEL.send(embed=embed)
80
- else:
81
- print("Log channel not found.")
82
- except Exception as e:
83
- print(f"Error: {e}")
84
-
85
- @bot.event
86
- async def on_message_delete(message):
87
- try:
88
- if message.author == bot.user:
89
- return
90
-
91
- embed = Embed(color=Color.red())
92
- embed.set_author(name=f"{message.author} ID: {message.author.id}", icon_url=message.author.avatar.url)
93
- embed.title = "Message Deleted"
94
- embed.description = message.content or "*(empty message)*"
95
- embed.add_field(name="Author Username", value=message.author.name, inline=True)
96
- embed.add_field(name="Channel", value=message.channel.mention, inline=True)
97
- #embed.add_field(name="Message Created On", value=message.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
98
- embed.add_field(name="Message Created On", value=convert_to_timezone(message.created_at, zurich_tz), inline=True)
99
- embed.add_field(name="Message ID", value=message.id, inline=True)
100
- embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})", inline=True)
101
-
102
- if message.attachments:
103
- attachment_urls = "\n".join([attachment.url for attachment in message.attachments])
104
  embed.add_field(name="Attachments", value=attachment_urls, inline=False)
105
-
106
  #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
107
  embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
108
-
109
- if LOG_CHANNEL is not None:
110
- await LOG_CHANNEL.send(embed=embed)
111
- else:
112
- print("Log channel not found.")
113
- except Exception as e:
114
- print(f"Error: {e}")
115
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  # nickname stuff
118
  @bot.event
119
  async def on_member_update(before, after):
120
  if before.nick != after.nick:
121
  embed = Embed(description=f'{before} changed their nickname to {after.nick}', color=Color.blue())
122
- await LOG_CHANNEL.send(embed=embed)
123
 
124
  @bot.event
125
  async def on_member_ban(guild, user):
126
  # member banned from the guild
127
  embed = Embed(description=f'Member {user.mention} was banned from the guild', color=Color.red())
128
- await LOG_CHANNEL.send(embed=embed)
129
 
130
  @bot.event
131
  async def on_member_unban(guild, user):
132
  # member unbanned from the guild
133
  embed = Embed(description=f'Member {user.mention} was unbanned from the guild', color=Color.green())
134
- await LOG_CHANNEL.send(embed=embed)
135
 
136
  # admin stuff---------------------------------------------------------------------------------------------------------------------------------------------------------------
137
 
@@ -139,62 +123,63 @@ async def on_member_unban(guild, user):
139
  async def on_guild_channel_create(channel):
140
  # creating channels
141
  embed = Embed(description=f'Channel {channel.mention} was created', color=Color.green())
142
- await LOG_CHANNEL.send(embed=embed)
143
 
144
  @bot.event
145
  async def on_guild_channel_delete(channel):
146
  # deleting channels, should ping @alerts for this
147
  embed = Embed(description=f'Channel {channel.name} ({channel.mention}) was deleted', color=Color.red())
148
- await LOG_CHANNEL.send(embed=embed)
149
 
150
  @bot.event
151
  async def on_guild_role_create(role):
152
  # creating roles
153
  embed = Embed(description=f'Role {role.mention} was created', color=Color.green())
154
- await LOG_CHANNEL.send(embed=embed)
155
 
156
  @bot.event
157
  async def on_guild_role_delete(role):
158
  # deleting roles, should ping @alerts for this
159
  embed = Embed(description=f'Role {role.name} ({role.mention}) was deleted', color=Color.red())
160
- await LOG_CHANNEL.send(embed=embed)
161
 
162
  @bot.event
163
  async def on_guild_role_update(before, after):
164
  # editing roles, could expand this
165
  if before.name != after.name:
166
  embed = Embed(description=f'Role {before.mention} was renamed to {after.name}', color=Color.orange())
167
- await LOG_CHANNEL.send(embed=embed)
168
 
169
  if before.permissions.administrator != after.permissions.administrator:
170
  # changes involving the administrator permission / sensitive permissions (can help to prevent mistakes)
171
  certain_role_id = 1106995261487710411 # role to ping (@alerts)
172
  certain_role = after.guild.get_role(certain_role_id)
173
  embed = Embed(description=f'Role {after.mention} had its administrator permission {"enabled" if after.permissions.administrator else "disabled"}', color=Color.red())
174
- await LOG_CHANNEL.send(content=certain_role.mention, embed=embed)
175
 
176
  @bot.event
177
  async def on_voice_state_update(member, before, after):
178
  if before.mute != after.mute:
179
  # muting members
180
  embed = Embed(description=f'{member} was {"muted" if after.mute else "unmuted"} in voice chat', color=Color.orange())
181
- await LOG_CHANNEL.send(embed=embed)
182
 
183
  if before.deaf != after.deaf:
184
  # deafening members
185
  embed = Embed(description=f'{member} was {"deafened" if after.deaf else "undeafened"} in voice chat', color=Color.orange())
186
- await LOG_CHANNEL.send(embed=embed)
187
 
188
  # bot stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------------
189
 
190
  # send messages
191
  #
192
- DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
193
 
194
  @bot.event
195
  async def on_ready():
196
  print('Logged on as', bot.user)
 
197
 
 
198
 
199
  def run_bot():
200
  bot.run(DISCORD_TOKEN)
 
20
  import asyncio # check if used
21
 
22
  zurich_tz = timezone("Europe/Zurich")
 
 
 
 
 
 
23
 
24
  def convert_to_timezone(dt, tz):
25
  return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
26
 
27
+ DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
28
+ intents = discord.Intents.default()
29
+ intents.message_content = True
30
+
31
+ bot = commands.Bot(command_prefix='!', intents=intents)
32
 
33
  #rate_limiter = RateLimiter(max_calls=10, period=60) # needs testing
34
 
35
  # todo
36
  '''
 
 
37
  discord.gg / discord/invite (block completely, need to be cautious)
38
  attempted @everyone / @here pings, or trying to use those strings (ping @alerts)
39
  ping alerts for list of keywords
 
49
 
50
  @bot.event
51
  async def on_message_edit(before, after):
52
+ if before.author == bot.user:
53
+ return
54
+
55
+ if before.content != after.content:
56
+ embed = Embed(color=Color.orange())
57
+ embed.set_author(name=f"{before.author} ID: {before.author.id}", icon_url=before.author.avatar.url)
58
+ embed.title = "Message Edited"
59
+ embed.description = f"**Before:** {before.content or '*(empty message)*'}\n**After:** {after.content or '*(empty message)*'}"
60
+ embed.add_field(name="Author Username", value=before.author.name, inline=True)
61
+ embed.add_field(name="Channel", value=before.channel.mention, inline=True)
62
+ #embed.add_field(name="Message Created On", value=before.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
63
+ embed.add_field(name="Message Created On", value=convert_to_timezone(before.created_at, zurich_tz), inline=True)
64
+ embed.add_field(name="Message ID", value=before.id, inline=True)
65
+ embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{before.guild.id}/{before.channel.id}/{before.id})", inline=True)
66
+
67
+ if before.attachments:
68
+ attachment_urls = "\n".join([attachment.url for attachment in before.attachments])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  embed.add_field(name="Attachments", value=attachment_urls, inline=False)
70
+
71
  #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
72
  embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
73
+
74
+ await bot.log_channel.send(embed=embed)
75
+
76
+ @bot.event
77
+ async def on_message_delete(message):
78
+ if message.author == bot.user:
79
+ return
80
+
81
+ embed = Embed(color=Color.red())
82
+ embed.set_author(name=f"{message.author} ID: {message.author.id}", icon_url=message.author.avatar.url)
83
+ embed.title = "Message Deleted"
84
+ embed.description = message.content or "*(empty message)*"
85
+ embed.add_field(name="Author Username", value=message.author.name, inline=True)
86
+ embed.add_field(name="Channel", value=message.channel.mention, inline=True)
87
+ #embed.add_field(name="Message Created On", value=message.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
88
+ embed.add_field(name="Message Created On", value=convert_to_timezone(message.created_at, zurich_tz), inline=True)
89
+ embed.add_field(name="Message ID", value=message.id, inline=True)
90
+ embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})", inline=True)
91
+
92
+ if message.attachments:
93
+ attachment_urls = "\n".join([attachment.url for attachment in message.attachments])
94
+ embed.add_field(name="Attachments", value=attachment_urls, inline=False)
95
+
96
+ #embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
97
+ embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
98
+
99
+ await bot.log_channel.send(embed=embed)
100
 
101
  # nickname stuff
102
  @bot.event
103
  async def on_member_update(before, after):
104
  if before.nick != after.nick:
105
  embed = Embed(description=f'{before} changed their nickname to {after.nick}', color=Color.blue())
106
+ await bot.log_channel.send(embed=embed)
107
 
108
  @bot.event
109
  async def on_member_ban(guild, user):
110
  # member banned from the guild
111
  embed = Embed(description=f'Member {user.mention} was banned from the guild', color=Color.red())
112
+ await bot.log_channel.send(embed=embed)
113
 
114
  @bot.event
115
  async def on_member_unban(guild, user):
116
  # member unbanned from the guild
117
  embed = Embed(description=f'Member {user.mention} was unbanned from the guild', color=Color.green())
118
+ await bot.log_channel.send(embed=embed)
119
 
120
  # admin stuff---------------------------------------------------------------------------------------------------------------------------------------------------------------
121
 
 
123
  async def on_guild_channel_create(channel):
124
  # creating channels
125
  embed = Embed(description=f'Channel {channel.mention} was created', color=Color.green())
126
+ await bot.log_channel.send(embed=embed)
127
 
128
  @bot.event
129
  async def on_guild_channel_delete(channel):
130
  # deleting channels, should ping @alerts for this
131
  embed = Embed(description=f'Channel {channel.name} ({channel.mention}) was deleted', color=Color.red())
132
+ await bot.log_channel.send(embed=embed)
133
 
134
  @bot.event
135
  async def on_guild_role_create(role):
136
  # creating roles
137
  embed = Embed(description=f'Role {role.mention} was created', color=Color.green())
138
+ await bot.log_channel.send(embed=embed)
139
 
140
  @bot.event
141
  async def on_guild_role_delete(role):
142
  # deleting roles, should ping @alerts for this
143
  embed = Embed(description=f'Role {role.name} ({role.mention}) was deleted', color=Color.red())
144
+ await bot.log_channel.send(embed=embed)
145
 
146
  @bot.event
147
  async def on_guild_role_update(before, after):
148
  # editing roles, could expand this
149
  if before.name != after.name:
150
  embed = Embed(description=f'Role {before.mention} was renamed to {after.name}', color=Color.orange())
151
+ await bot.log_channel.send(embed=embed)
152
 
153
  if before.permissions.administrator != after.permissions.administrator:
154
  # changes involving the administrator permission / sensitive permissions (can help to prevent mistakes)
155
  certain_role_id = 1106995261487710411 # role to ping (@alerts)
156
  certain_role = after.guild.get_role(certain_role_id)
157
  embed = Embed(description=f'Role {after.mention} had its administrator permission {"enabled" if after.permissions.administrator else "disabled"}', color=Color.red())
158
+ await bot.log_channel.send(content=certain_role.mention, embed=embed)
159
 
160
  @bot.event
161
  async def on_voice_state_update(member, before, after):
162
  if before.mute != after.mute:
163
  # muting members
164
  embed = Embed(description=f'{member} was {"muted" if after.mute else "unmuted"} in voice chat', color=Color.orange())
165
+ await bot.log_channel.send(embed=embed)
166
 
167
  if before.deaf != after.deaf:
168
  # deafening members
169
  embed = Embed(description=f'{member} was {"deafened" if after.deaf else "undeafened"} in voice chat', color=Color.orange())
170
+ await bot.log_channel.send(embed=embed)
171
 
172
  # bot stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------------
173
 
174
  # send messages
175
  #
 
176
 
177
  @bot.event
178
  async def on_ready():
179
  print('Logged on as', bot.user)
180
+ bot.log_channel = bot.get_channel(1107006391547342910)
181
 
182
+
183
 
184
  def run_bot():
185
  bot.run(DISCORD_TOKEN)