-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
282 lines (231 loc) · 10.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import discord
import pymongo
import random
import asyncio
import aiohttp
from discord.ui import Button, View
from discord import Activity, ActivityType
from discord.ext import commands
from datetime import datetime
import json
import time
import os
import requests
import psutil
import apicalls
from PIL import Image, ImageDraw, ImageOps, ImageFont
from io import BytesIO
with open('config.json') as f:
data = json.load(f)
intents = discord.Intents.all()
client = pymongo.MongoClient(data["mongodb"])
db = client.test
bot = commands.Bot(command_prefix=data["prefix"], intents=intents)
@bot.remove_command("help")
@bot.slash_command()
async def help(ctx):
em = discord.Embed(title="Help", description="All the available help commands.", color=discord.Colour.blurple())
em.add_field(name="Welcome Commands", value="`setwelcomechannel` , `delwelcomechannel` , `setleavechannel` , `delleavechannel` , `setwelcomemessage` , `delwelcomemessage` , `setleavemessage` , `delleavemessage`")
em.add_field(name="Fun Commands", value="`joke` , `darkjoke` , `fact` , `roast <user>` , `question` , `quote` , `meme`", inline=False)
em.add_field(name="Util & Bot", value="`avatar` , `coinflip` , `userinfo` , `uptime` , `ping` , `stats` , `servers`")
em.add_field(name="Owner", value="`subreddit`", inline=False)
await ctx.respond(embed=em)
startup_time = datetime.now()
@bot.slash_command()
async def servers(ctx):
em = discord.Embed(title="Servers", description=f"I'm in {len(bot.guilds)} servers", color=discord.Colour.blurple())
await ctx.respond(embed=em)
@bot.slash_command()
async def stats(ctx):
cpu_percent = psutil.cpu_percent()
memory = psutil.virtual_memory()
ram_usage = memory.used / 1024 / 1024
ram_total = memory.total / 1024 / 1024
ram_percent = memory.percent
disk_usage = psutil.disk_usage('/')
total_space = disk_usage.total / (1024 ** 3) # Convert bytes to gigabytes
used_space = disk_usage.used / (1024 ** 3)
percent_used = disk_usage.percent
em = discord.Embed(title="Nuko Stats", description="Check my statistics like Ram, Storage, CPU, Ping", colour=discord.Colour.blurple())
em.add_field(name="Latency", value=f"{bot.latency * 1000:.2f}ms", inline=False)
em.add_field(name="CPU Usage", value=f"{cpu_percent}%", inline=False)
em.add_field(name="RAM Usage", value=f"{ram_usage:.2f} MB / {ram_total:.2f} MB ({ram_percent}%)", inline=False)
em.add_field(name="Storage", value=f"{used_space:.2f} GB / {total_space:.2f} GB ({percent_used}%)", inline=False)
await ctx.respond(embed = em)
@bot.slash_command(description="Show how long the bot has been running since it was last started.")
async def uptime(ctx):
current_time = datetime.now()
uptime = current_time - startup_time
days = uptime.days
hours, remainder = divmod(uptime.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
uptime_str = f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds"
em = discord.Embed(title="Bot Uptime", description=uptime_str, color=discord.Colour.blurple())
await ctx.respond(embed=em)
@bot.slash_command(description="Check the bot's ping latency.")
async def ping(ctx):
start_time = time.time()
message = await ctx.respond("Pinging...")
latency = (time.time() - start_time) * 1000
await message.edit_original_response(content=f"Pong! **Latency:** {latency:.2f}ms | **API Latency:** {bot.latency * 1000:.2f}ms")
@bot.event
async def on_member_join(member):
db = client['nuko']
channels = db['channels']
messages = db["messages"]
query = channels.find_one({"guild_id": member.guild.id})
query2 = messages.find_one({"guild_id": member.guild.id})
if not query or not query2:
return
channel = bot.get_channel(query["channel_id"])
url = query2["image_url"]
background_response = requests.get(url)
background_response.raise_for_status()
background_data = BytesIO(background_response.content)
img = Image.open(background_data)
fontXL = ImageFont.truetype("fonts/robotom.ttf", 75)
fontX = ImageFont.truetype("fonts/popregular.ttf", 37)
fontXC = ImageFont.truetype("fonts/mid.ttf", 40)
if img.size != (1500, 500):
img = img.resize((1500, 500))
uavatar = member.avatar
data = BytesIO(await uavatar.read())
pfp = Image.open(data).convert("RGBA")
pfp = pfp.resize((367, 375))
mask = Image.new("L", pfp.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, pfp.width, pfp.height), fill=255)
rounded_pfp = ImageOps.fit(pfp, mask.size)
rounded_pfp.putalpha(mask)
bordered_pfp = ImageOps.expand(rounded_pfp)
img.paste(bordered_pfp, (143, 60), mask=bordered_pfp)
welcome = query2["image_title"]
username = "@" + str(member)
userid = "Creation Date: " + member.created_at.strftime("%d %B, %Y | %H:%M")
message = query2["message"]
message = message.replace("{user.name}", f"{member.name}")
message = message.replace("{user}", f"{member}")
message = message.replace("{user.mention}", f"{member.name}")
print(message)
if len(message) > 40:
message_lines = [message[i:i + 40] for i in range(0, len(message), 40)]
message = "\n".join(message_lines)
draw = ImageDraw.Draw(img)
draw.text((820, 55), f"{welcome}", (20, 17, 20), font=fontXL)
draw.text((610, 170), username, (20, 17, 20), font=fontXC)
draw.text((610, 235), userid, (20, 17, 20), font=fontX)
draw.text((610, 290), message, (20, 17, 20), font=fontX)
profile_path = "rounded_profile.png"
img.save(profile_path)
with open(profile_path, "rb") as f:
profile_image = discord.File(f, filename=profile_path)
if query2["mention_user"] == "true":
await channel.send(f"{member.mention}", file=profile_image)
else:
await channel.send(file=profile_image)
@bot.event
async def on_member_remove(member):
db = client['nuko']
channels = db['leavechannel']
messages = db["leaves"]
query = channels.find_one({"guild_id": member.guild.id})
query2 = messages.find_one({"guild_id": member.guild.id})
if not query or not query2:
return
channel = bot.get_channel(query["channel_id"])
url = query2["image_url"]
background_response = requests.get(url)
background_response.raise_for_status()
background_data = BytesIO(background_response.content)
img = Image.open(background_data)
fontXL = ImageFont.truetype("fonts/robotom.ttf", 75)
fontX = ImageFont.truetype("fonts/popregular.ttf", 37)
fontXC = ImageFont.truetype("fonts/mid.ttf", 40)
if img.size != (1500, 500):
img = img.resize((1500, 500))
uavatar = member.avatar
data = BytesIO(await uavatar.read())
pfp = Image.open(data).convert("RGBA")
pfp = pfp.resize((367, 375))
mask = Image.new("L", pfp.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, pfp.width, pfp.height), fill=255)
rounded_pfp = ImageOps.fit(pfp, mask.size)
rounded_pfp.putalpha(mask)
bordered_pfp = ImageOps.expand(rounded_pfp)
img.paste(bordered_pfp, (143, 60), mask=bordered_pfp)
title = query2["image_title"]
username = "@" + str(member)
userid = "Creation Date: " + member.created_at.strftime("%d %B, %Y | %H:%M")
message = query2["message"]
message = message.replace("{user}", f"{member}")
message = message.replace("{user.mention}", f"{member.name}")
message = message.replace("{user.name}", f"{member.name}")
print(message)
if len(message) > 40:
message_lines = [message[i:i + 40] for i in range(0, len(message), 40)]
message = "\n".join(message_lines)
draw = ImageDraw.Draw(img)
draw.text((820, 55), f"{title}", (20, 17, 20), font=fontXL)
draw.text((610, 170), username, (20, 17, 20), font=fontXC)
draw.text((610, 235), userid, (20, 17, 20), font=fontX)
draw.text((610, 290), message, (20, 17, 20), font=fontX)
profile_path = "deltapath.png"
img.save(profile_path)
with open(profile_path, "rb") as f:
profile_image = discord.File(f, filename=profile_path)
if query2["mention_user"] == "true":
await channel.send(f"{member.mention}", file=profile_image)
else:
await channel.send(file=profile_image)
@bot.event
async def on_application_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.respond(f"Error 213: Missing Permission | You do not have sufficient permession to use this command.", ephemeral=True)
else:
pass
def get_recomend():
obj = apicalls.fetch_song()
string = obj['song'] + " - " + obj['artist']
return string
statuses = [
discord.Game(name="Minecraft"),
discord.Streaming(name="How to setup Nuko.", url="https://www.youtube.com/watch?v=_HAuSkW-1ek"),
discord.Activity(type=discord.ActivityType.listening, name=get_recomend()),
discord.Activity(type=discord.ActivityType.watching, name="Naruto | Season 283293 | Episode 3923829")
]
async def change_status():
while True:
for status in statuses:
await bot.change_presence(activity=status)
await asyncio.sleep(50)
async def update():
inp = input("Do you want to announce any updates? Type 'Yes' to confirm:\n")
if inp == "Yes":
channel = input("Select Channel:\n1. [📢] announcement\n2. [🤖] bot-updates\n")
if channel == "1":
content = input("Write the Announcement Content:\n")
channel = bot.get_channel(1109754654331519029)
await channel.send(content)
elif channel == "2":
content = input("Write the Bot Update Content:\n")
channel = bot.get_channel(1119700162013577294)
await channel.send(content)
else:
print("Please select a valid channel")
else:
return
@bot.event
async def on_ready():
await update()
print("Hello World, Nuko is alive!")
bot.loop.create_task(change_status())
for f in os.listdir("./cogs"):
if f.endswith(".py"):
cog_name = "cogs." + f[:-3]
try:
bot.load_extension(cog_name)
print(f"Cog [{cog_name}] Loaded Successfully.")
except Exception as e:
print(f"An error occurred while loading [{cog_name}]: {str(e)}")
bot.run(data["token"])