-
Notifications
You must be signed in to change notification settings - Fork 0
/
pybot.py
402 lines (284 loc) · 11.5 KB
/
pybot.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import asyncio
import json
from logging import PlaceHolder
import os
import random
import aiohttp
import nextcord
import discord
import pretty_errors
from random import choice, randint
from datetime import datetime
from typing import Optional
from nextcord import Embed
from nextcord.ext.commands import BucketType
from nextcord.ext.commands import (CommandNotFound, BadArgument, MissingRequiredArgument, CommandOnCooldown)
from nextcord.ext.commands import cog
from nextcord.ext.commands.cog import Cog
from nextcord.ext.commands.errors import BadArgument
from nextcord.ext.commands import cooldown
from nextcord.ext.commands import has_permissions, MissingPermissions
from nextcord.ext import commands
from nextcord.ext.commands.help import DefaultHelpCommand
from nextcord.utils import get
from nextcord import client
from dotenv import load_dotenv
from datetime import date
from nextcord.ext.commands import command, cooldown
from better_profanity import profanity
from nextcord.ext import commands
from nextcord import Forbidden
from nextcord.utils import get
from nextcord import Interaction, SlashOption, ChannelType
from nextcord.abc import GuildChannel
today = date.today()
load_dotenv()
intents = nextcord.Intents.all()
def get_prefix(bot, message):
return prefixes.get(str(message.guild.id), DEFAULT_PREFIX)
bot = commands.Bot(command_prefix=get_prefix, intents=nextcord.Intents.all())
intents.members = True
initial_extensions =[]
if os.path.exists(os.getcwd() + "/config.json"):
with open("./config.json") as f:
configData = json.load(f)
else:
configTemplate = {"Token": "", "Prefix": "!"}
with open(os.getcwd() + "/config.json", "w+") as f:
json.dump(configTemplate, f)
Token = configData["Token"]
prefix = configData["Prefix"]
bot.remove_command("help")
DEFAULT_PREFIX = "!"
class JsonDict(dict):
def __init__(self, file_name: str):
self.file_name = file_name
try:
with open(self.file_name, "r") as file:
self.update(json.load(file))
except FileNotFoundError:
pass
def save(self):
with open(self.file_name, "w+") as file:
json.dump(self, file, indent=2)
prefixes = JsonDict("prefixes.json")
def syntax(command):
cmd_and_aliases = "|".join([str(command), *command.aliases])
params = []
for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
params = " ".join(params)
return f"{cmd_and_aliases} {params}"
@bot.event
async def on_ready():
print(f"logged in as {bot.user}")
@bot.event
async def on_message_delete(message):
embed = nextcord.Embed(title=f"{message.author.name} has edited a message | {message.author.id}", description=f"{message.content}")
channel = bot.get_channel(792547157629993032)
await channel.send(embed=embed)
@bot.event
async def on_message_edit(message_before, message_after):
embed = nextcord.Embed(title=f"{message_before.author.name} has edited a message | {message_before.author.id}")
embed.add_field(name="Before Message", value=f"{message_before.content}", inline=False)
embed.add_field(name="After Message", value=f"{message_after.content}", inline=False)
channel = bot.get_channel(792547157629993032)
await channel.send(embed=embed)
@bot.event
async def on_member_join(member):
welcomeEmbed = nextcord.Embed(title = f"New member", description = f"{member.name} has joined the server", color = nextcord.Color.blue())
await bot.get_channel(792547157629993032).send(embed = welcomeEmbed)
testingServerID = 815719310198177792
@bot.command()
async def help(ctx):
embed = nextcord.Embed(title="help commands")
embed.add_field(name = 'admin', value = "`ban`, `clear`, `changeprefix`, `mute`, `unmute`, `slowmode`, `kick`, `tempban`")
embed.add_field(name = 'member', value = "`ping`, `roll`, `fact`, `version`")
await ctx.send(embed = embed)
@bot.event
async def on_guild_join(guild):
prefixes[str(guild.id)] = DEFAULT_PREFIX
prefixes.save()
@bot.command()
@commands.cooldown(1, 60, commands.cooldowns.BucketType.user)
async def ping(ctx: commands.Context):
"""
Returns the bots estimated latency to discord servers
"""
await ctx.send(f"pong {round(bot.latency * 1000)}ms")
@bot.command(name = "roll")
async def roll(ctx: commands.Context):
"""
Get a random number from 0-500
"""
n = random.randint(0, 500)
embed = nextcord.Embed(colour=nextcord.Colour.green())
embed.set_thumbnail(
url="https://cdn.discordapp.com/attachments/762430826755260449/764946212527931392/die.png"
)
embed.add_field(
name="Roll", value=(f"Your random number is {n}/500."), inline=False
)
await ctx.send(embed=embed)
@bot.command()
async def fact(ctx: commands.Context):
"""
Returns a random fact
"""
url = "https://useless-facts.sameerkumar.website/api"
async with aiohttp.ClientSession() as session:
response = await session.get(url)
if response.status == 200:
_json = await response.json()
await ctx.send(_json.get("data"))
@bot.command()
async def serverinfo(ctx):
role_count = len(ctx.guild.roles)
list_of_bots = [bot.mention for bot in ctx.guild.members if bot.bot]
serverinfoEmbed = nextcord.Embed(timestamp=ctx.message.created_at, color=ctx.author.color)
serverinfoEmbed.add_field(name='Name', value=f"{ctx.guild.name}", inline =False)
serverinfoEmbed.add_field(name='Member Count', value=f"{ctx.guild.member_count}", inline =False)
await ctx.send(embed = serverinfoEmbed)
@bot.command(pass_context=True)
async def version(ctx):
myEmbed = nextcord.Embed (title="current Version", description= " version 1.0 :)", color=0x00ff00)
myEmbed.add_field( name = "Version Code", value = 'V1.0' , inline=False)
myEmbed.add_field( name = "released", value = today , inline=False)
myEmbed.set_author (name= "")
myEmbed.set_footer (text = ".")
await ctx.channel.send(embed=myEmbed)
bot.command()
async def load(ctx, extension):
bot.load_extension(f"cogs.{extension}")
bot.command()
async def unload(ctx, extension):
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
client.load_extension(f"cogs.{filename[:-3]}")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
await ctx.send(f"Invalid command used")
else:
raise error
class DurationConverter(commands.Converter):
async def convert(self, ctx, argument):
ammount = argument[:-1]
unit = argument[-1]
if ammount.isdigit() and unit in ["s," "m"]:
return(int(ammount), unit)
raise commands.BadArgument(message="Not a valid duration")
#Admin commands
@bot.command()
@commands.has_role('administrator')
async def clear(ctx, amount=6):
await ctx.channel.purge(limit=amount)
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("Looks like you don't have the permissions to do that action.")
@bot.command()
async def changeprefix(ctx, prefix):
"""
Change the prefix for your guild
Args:
prefix: The new prefix to use
"""
prefixes[str(ctx.guild.id)] = prefix
prefixes.save()
await ctx.send(f"Prefix has been changed to `{prefix}`")
@bot.command()
@commands.has_role('administrator')
async def mute(ctx, member : nextcord.Member, *, reason=None):
guild = ctx.guild
muteRole = nextcord.utils.get(guild.roles, name="Muted")
if not muteRole:
muteRole = await guild.create_role(name="Muted")
for channel in guild.channels:
await ctx.send("No mute roll has been found Creating the roll now")
await channel.set_permissions(muteRole, speak=False, send_messages=False, read_message_history=True, read_messages=True)
await member.add_roles(muteRole, reason=reason)
await member.send(f"you have been muted from **{guild.name}** | Reason: **{reason}**")
@bot.command()
@commands.has_role('administrator')
async def unmute(ctx, member : nextcord.Member, *, reason=None):
guild = ctx.guild
muteRole = nextcord.utils.get(guild.roles, name="Muted")
if not muteRole:
muteRole = await guild.create_role(name="Muted")
await member.remove_roles(muteRole, reason=reason)
await member.send(f"you have been unmuted from **{guild.name}** | Reason: **{reason}**")
@bot.command()
async def slowmode(ctx, time:int):
try:
if time == 0:
await ctx.send("Slowmode off")
await ctx.channel.edit(slowmode_delay = 0)
elif time > 21600:
await ctx.send("you can not set the slowmode above 6 hours")
return
else:
await ctx.channel.edit(slowmode_delay = time)
await ctx.semd(f" slow mode set to {time} seconds")
except Exception:
await print("failed")
@slowmode.error
async def slowmode_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("Looks like you don't have the permissions to do that action.")
@bot.command()
@commands.has_role('administrator')
async def kick(ctx, member : nextcord.Member, *, reason=None):
await member.kick(reason=reason)
@bot.command()
@commands.has_role('administrator')
async def ban(ctx, member : nextcord.Member, *, reason=None):
await member.ban(reason=reason)
@ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("Looks like you don't have the permissions to do that action.")
@kick.error
async def kick_error(ctx, error):
if isinstance(error, BadArgument):
await ctx.send("user not found.")
@ban.error
async def ban_error(ctx, error):
if isinstance(error, BadArgument):
await ctx.send("user not found.")
@bot.command()
async def unban(ctx, user: nextcord.User):
banned_users = await ctx.guild.bans()
if user in banned_users:
await ctx.guild.unban(user)
await ctx.send(f"Unbanned {user.mention}")
@bot.command()
async def tempban(ctx, member: commands.MemberConverter, duration: DurationConverter):
multiplier = {"s": 1, "m" : 60}
amount, unit = duration
await ctx.guild.ban(member)
await ctx.send(f"{member} has been banned for {amount}{ unit}.")
await asyncio.sleep(amount * multiplier [unit])
await ctx.guild.unban(member)
@mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("Looks like you don't have the permissions to do that action.")
@unmute.error
async def unmute_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("Looks like you don't have the permissions to do that action.")
@bot.event
async def on_message(message):
if profanity.contains_profanity(message.content):
await message.delete()
await message.author.send("""Those Words are Not Allowed To Be Used! Continued Use Of Mentioned Words Will Lead To a Punishment!""")
else:
await bot.process_commands(message)
@ping.error
async def error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("please try this command again after {:.2f}s".format(error.retry_after))
print(configData)
bot.run(Token)