-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
266 lines (207 loc) · 7.44 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from dotenv import load_dotenv
import os
import datetime
import discord
from discord.ext import commands
from api import keep_alive
import statistics
import time
import sys
from constants import TOKEN
load_dotenv()
start_time = None
latencies = []
class zippyBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix=">> ", intents=discord.Intents.all(), help_command=None
)
self.synced = False
async def on_ready(self):
await load()
global start_time
start_time = datetime.datetime.now(datetime.timezone.utc)
await self.wait_until_ready()
await self.change_presence(
activity=discord.CustomActivity(
name="Custom Status",
state="Watching Ivirius Text Editor Plus",
),
status=discord.Status.do_not_disturb,
)
if not self.synced:
await self.tree.sync()
self.synced = True
print(f"Logged in as {self.user.name} (ID: {self.user.id})")
print(f"Connected to {len(self.guilds)} guilds")
bot = zippyBot()
async def load():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await bot.load_extension(f"cogs.{filename[:-3]}")
@bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
@bot.event
async def on_member_join(member):
if member.guild.id != 1137161703000375336:
return
embed = discord.Embed(
title="Welcome to the server!",
description=f"Welcome to the server {member.name}! Please read the rules and have fun!",
color=discord.Color.brand_red(),
)
await member.send(embed=embed)
@bot.event
async def on_member_remove(member):
if member.guild.id != 1137161703000375336:
return
user = member.name
nickname = member.nick
join_date = member.joined_at
total_time = datetime.datetime.now() - join_date
join_date_formatted = join_date.strftime("%Y-%m-%d %H:%M:%S")
total_time_formatted = str(total_time)
embed = discord.Embed(
title="Goodbye!",
description=f"**{user}** ({nickname}) has left the server.",
color=discord.Color.red(),
)
embed.add_field(name="Join Date", value=join_date_formatted, inline=False)
embed.add_field(
name="Total Time in Server", value=total_time_formatted, inline=False
)
roles = [role.mention for role in member.roles]
embed.add_field(name="Roles", value=", ".join(roles), inline=False)
channel = bot.get_channel(1188420266234228809)
await channel.send(embed=embed)
@bot.event
async def on_member_ban(guild, user):
ban_reason = None
banned_by = None
async for entry in guild.audit_logs(action=discord.AuditLogAction.ban, limit=1):
if entry.target == user:
ban_reason = entry.reason
banned_by = entry.user
break
response = (
f"{user.mention} has been **banned**!\n"
f"Banned by: {banned_by.mention}\n"
f"Reason: {ban_reason}"
)
target_channel = bot.get_channel(1188420266234228809)
await target_channel.send(response)
@bot.event
async def on_member_unban(guild, user):
unbanned_by = None
async for entry in guild.audit_logs(action=discord.AuditLogAction.unban, limit=1):
if entry.target == user:
unbanned_by = entry.user
break
response = (
f"{user.mention} had their ban **revoked**!\n"
f"Revoked by: {unbanned_by.mention}\n"
)
target_channel = bot.get_channel(1188420266234228809)
await target_channel.send(response)
@bot.event
async def on_member_kick(guild, user):
kick_reason = None
kicked_by = None
async for entry in guild.audit_logs(action=discord.AuditLogAction.kick, limit=1):
if entry.target == user:
kick_reason = entry.reason
kicked_by = entry.user
break
response = (
f"{user.mention} has been **kicked**!\n"
f"Kicked by: {kicked_by.mention}\n"
f"Reason: {kick_reason}"
)
target_channel = bot.get_channel(1188420266234228809)
await target_channel.send(response)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
return
elif isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have the required permissions to run this command!")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("You are missing a required argument!")
elif isinstance(error, commands.BadArgument):
await ctx.send("You have provided a bad argument!")
elif isinstance(error, commands.CommandOnCooldown):
await ctx.send(
f"This command is on cooldown! Please try again in {error.retry_after:.2f} seconds."
)
elif isinstance(error, commands.NotOwner):
await ctx.send("You are not the owner of this bot!")
elif isinstance(error, commands.MissingRole):
await ctx.send("You are missing a required role!")
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send("I don't have the required permissions to run this command!")
@bot.command()
@commands.is_owner()
async def sync(ctx):
await bot.tree.sync()
synced = await bot.tree.sync()
if len(synced) > 0:
await ctx.send(f"Successfully Synced {len(synced)} Commands ✔️")
else:
await ctx.send("No Slash Commands to Sync :/")
@bot.event
async def on_command_completion(ctx):
end = time.perf_counter()
start = ctx.start
latency = (end - start) * 1000
latencies.append(latency)
if len(latencies) > 10:
latencies.pop(0)
@bot.before_invoke
async def before_invoke(ctx):
start = time.perf_counter()
ctx.start = start
@bot.command()
async def ping(ctx):
try:
embed = discord.Embed(title="Pong!", color=discord.Color.brand_red())
message = await ctx.send(embed=embed)
end = time.perf_counter()
latency = (end - ctx.start) * 1000
embed.add_field(
name="Bot Latency", value=f"{bot.latency * 1000:.2f} ms", inline=False
)
embed.add_field(name="Message Latency", value=f"{latency:.2f} ms", inline=False)
# Calculate the average ping of the bot in the last 10 minutes
if latencies:
average_ping = statistics.mean(latencies)
embed.add_field(
name="Average Message Latency",
value=f"{average_ping:.2f} ms",
inline=False,
)
global start_time
current_time = datetime.datetime.now(datetime.timezone.utc)
delta = current_time - start_time
hours, remainder = divmod(int(delta.total_seconds()), 3600)
minutes, seconds = divmod(remainder, 60)
embed.add_field(
name="Uptime",
value=f"{hours} hours {minutes} minutes {seconds} seconds",
inline=False,
)
embed.set_footer(
text="Information requested by: {}".format(ctx.author.name),
icon_url=ctx.author.avatar.url,
)
embed.set_thumbnail(
url="https://uploads.poxipage.com/7q5iw7dwl5jc3zdjaergjhpat27tws8bkr9fgy45_938843265627717703-webp"
)
await message.edit(embed=embed)
except Exception as e:
print(e, file=sys.stdout)
if __name__ == "__main__":
keep_alive()
bot.run(token=TOKEN)