import discord, toml, os from discord.ext import commands from discord.commands import option with open("config.toml", "r") as f: config = toml.load(f) guild_ids=config["bot"]["guild_ids"] MYDIR = ("cogs/essentials/reputation") CHECK_FOLDER = os.path.isdir(MYDIR) # If folder doesn't exist, then create it. if not CHECK_FOLDER: os.makedirs(MYDIR) print("created folder : ", MYDIR) else: print(MYDIR, "folder already exists.") with open("cogs/essentials/config.toml", "r") as f: esconf = toml.load(f) class EssentialCommands(commands.Cog): def __init__(self, bot): self.bot = bot self.rep_file = "cogs/essentials/reputation" @discord.Cog.listener() async def on_member_join(self, member): # Send a welcome message with an embed to the specified channel if not esconf["events"]["welcome_enabled"]: return 0 welcome_title = esconf['events']['welcome_title'] welcome_message = esconf['events']['welcome_content'] welcome_channel = self.bot.get_channel(esconf["events"]['welcome_channel_id']) embed = discord.Embed(title=welcome_title.format(member=member), description=welcome_message.format(member=member), color=0x00ff00) await welcome_channel.send(embed=embed) @discord.Cog.listener() async def on_member_remove(self, member): # Send a goodbye message with an embed to the specified channel if not esconf["events"]["goodbye_enabled"]: return 0 gb_title = esconf['events']['goodbye_title'] gb_message = esconf['events']['goodbye_content'] gb_channel = self.bot.get_channel(esconf["events"]['welcome_channel_id']) embed = discord.Embed(title=gb_title.format(member=member), description=gb_message.format(member=member), color=0xff0000) await gb_channel.send(embed=embed) @commands.Cog.listener() async def on_message(self, message): #print(message.content) if message.content.startswith('+rep') or message.content.startswith('-rep'): # extract the necessary information from the message content reputation_type = message.content[0] # '+' or '-' mentioned_users = message.mentions author_id = message.author.id if message.author in mentioned_users: await message.channel.send("You can't give reputation to yourself.") return elif message.author.bot: await message.channel.send("Bots can't give ratings...") return comment = message.content.split(' ', 2)[2] if len(message.content.split(' ')) > 2 else '' #print(f"Rep for: {mentioned_users}") # load the reputation data from the TOML file try: with open(f"{self.rep_file}/{message.guild.id}.toml", 'r') as f: reputation_data = toml.load(f) #print(reputation_data) except: with open(f"{self.rep_file}/{message.guild.id}.toml", "w+") as f: f.write("[reputations]") with open(f"{self.rep_file}/{message.guild.id}.toml", 'r') as f: reputation_data = toml.load(f) #print(reputation_data) # loop through the mentioned users for user in mentioned_users: # create the reputation entry if it doesn't exist if str(user.id) not in reputation_data: reputation_data[str(user.id)] = {} # check if the author has already given reputation to the user if str(author_id) in reputation_data[str(user.id)]: if reputation_data[str(user.id)][str(author_id)]["type"] == reputation_type: await message.channel.send(f'You have already given reputation to {user.mention} before.') continue # update the reputation data with the new entry reputation_data[str(user.id)][str(author_id)] = {'type': reputation_type, 'points': 1 if reputation_type == '+' else -1, 'comment': comment} # save the reputation data back to the TOML file with open(f"{self.rep_file}/{message.guild.id}.toml", 'w') as f: toml.dump(reputation_data, f) embed = discord.Embed(description=f"You have given {reputation_type} reputation to {user.mention} with the comment: {comment}", title="You have given reputation.") await message.channel.send(embed=embed) @commands.slash_command() async def reputation(self, ctx, user: discord.User): # load the reputation data from the TOML file try: with open(f"{self.rep_file}/{ctx.guild.id}.toml", 'r') as f: reputation_data = toml.load(f) except: reputation_data = "" # check if the user has any reputation entries if str(user.id) not in reputation_data or reputation_data == "": await ctx.respond(f'{user.mention} has not received any reputation yet.') return # get the total reputation points for the user total_points = sum([data['points'] for author_id, data in reputation_data[str(user.id)].items()]) # create the reputation leaderboard embed embed = discord.Embed(title=f'{user.name}#{user.discriminator}\'s Reputation', color=discord.Color.blue()) if total_points == 0: embed.color = discord.Color.yellow() elif total_points < 0: embed.color = discord.Color.red() else: embed.color = discord.Color.green() embed.add_field(name='Total Reputation Points', value=total_points) # loop through the reputation entries for the user and add them to the embed for author_id, data in reputation_data[str(user.id)].items(): author = self.bot.get_user(int(author_id)) reputation_type = '+' if data['type'] == '+' else '-' comment = data['comment'] if data['comment'] else 'No comment.' embed.add_field(name=f'{author.name}#{author.discriminator} {reputation_type}', value=comment, inline=False) await ctx.respond(embed=embed) @commands.slash_command(guild_ids=guild_ids) async def ping(self, ctx): if not esconf["commands"]["ping"]: await ctx.respond("This command is disabled.") return 0 await ctx.respond('Pong!') @commands.slash_command(guild_ids=guild_ids) @option('msg', description="The message to say.") async def say(self, ctx, message): if not esconf["commands"]["say"]: await ctx.respond("This command is disabled.") return 0 await ctx.send(message) @commands.slash_command(guild_ids=guild_ids) async def serverinfo(self, ctx): if not esconf["commands"]["serverinfo"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() embed = discord.Embed(title=f"{ctx.guild.name} Info", colour=discord.Colour.blurple()) embed.add_field(name="Roles", value=len(ctx.guild.roles), inline=False) embed.add_field(name="Members", value=ctx.guild.member_count, inline=False) embed.add_field(name="Owner", value=ctx.guild.owner.mention, inline=False) embed.add_field(name="Text Channels", value=len(ctx.guild.text_channels), inline=False) embed.add_field(name="Voice Channels", value=len(ctx.guild.voice_channels), inline=False) embed.add_field(name="Boost Level", value=ctx.guild.premium_tier, inline=False) embed.add_field(name="Created", value=ctx.guild.created_at, inline=False) embed.add_field(name="ID", value=ctx.guild.id, inline=False) embed.add_field(name="Max File Size", value=f"{str(int(ctx.guild.filesize_limit / (1024 * 1024)))} MB", inline=False) icon = ctx.guild.icon embed.set_thumbnail(url=icon) await ctx.followup.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @option('ammount', description="The of messages to clear.", required=True) @commands.has_permissions(manage_messages=True) async def clean(self, ctx, amount: int = 10): if not esconf["commands"]["clean"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() try: await ctx.channel.purge(limit=amount + 1) except: await ctx.followup.send(f"Could not purge all of the messages.") else: await ctx.followup.send(f"Purged {amount} messages.") @commands.slash_command(guild_ids=guild_ids) @commands.has_permissions(manage_channels=True) async def lock(self, ctx, *, reason=None): if not esconf["commands"]["lock"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() channel = ctx.channel overwrite = channel.overwrites_for(ctx.guild.default_role) overwrite.send_messages = False await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) embed = discord.Embed(title="Locked", description=f"Reason: {reason}") await ctx.followup.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @commands.has_permissions(manage_channels=True) async def unlock(self, ctx, *, reason=None): if not esconf["commands"]["unlock"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() channel = ctx.channel overwrite = channel.overwrites_for(ctx.guild.default_role) overwrite.send_messages = True await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) embed = discord.Embed(title="Unlocked", description=f"Reason: {reason}") await ctx.followup.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @commands.has_permissions(manage_channels=True) async def lockdown(self, ctx, *, reason=None): if not esconf["commands"]["lockdown"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() for channel in ctx.guild.channels: if isinstance(channel, discord.TextChannel): overwrite = channel.overwrites_for(ctx.guild.default_role) overwrite.send_messages = False await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) embed = discord.Embed(title="Server Locked Down", description=f"Reason: {reason}") await ctx.followup.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @commands.has_permissions(manage_channels=True) async def unlockdown(self, ctx): if not esconf["commands"]["unlockdown"]: await ctx.respond("This command is disabled.") return 0 await ctx.defer() for channel in ctx.guild.channels: if isinstance(channel, discord.TextChannel): overwrite = channel.overwrites_for(ctx.guild.default_role) overwrite.send_messages = None await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite) embed = discord.Embed(title="Server Unlocked", description="All channels have been unlocked.") await ctx.followup.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @option('user', description="The username of the user you want the avatars picture incl 1234 numbers", required=True) async def avatar(self, ctx, member: discord.Member): if not esconf["commands"]["avatar"]: await ctx.respond("This command is disabled.") return 0 try: avatar_url = member.avatar.url except: await ctx.respond(f"Unable to get {member.name}'s avatar as he probably has a default avatar.") else: await ctx.respond(f"{member.name}'s avatar: {avatar_url}") @commands.slash_command(guild_ids=guild_ids) @option('member', description="The username of the user to run the check on.") async def whois(self, ctx, member: discord.Member = None): if not esconf["commands"]["whois"]: await ctx.respond("This command is disabled.") return 0 member = member or ctx.author roles = [role.mention for role in member.roles[1:]] if not roles: roles = ["None"] embed = discord.Embed(title=f"{member.display_name}'s Info", color=member.color) try: embed.set_thumbnail(url=member.avatar.url) except: pass embed.add_field(name="Name", value=str(member), inline=False) embed.add_field(name="ID", value=member.id, inline=False) embed.add_field(name="Account Created", value=member.created_at.strftime("%d/%m/%Y %H:%M:%S"), inline=False) embed.add_field(name="Join Date", value=member.joined_at.strftime("%d/%m/%Y %H:%M:%S"), inline=False) if not member.activity == None: embed.add_field(name="Activity", value=member.activity, inline=False) embed.add_field(name="Roles", value=" ".join(roles), inline=False) await ctx.send(embed=embed) @commands.slash_command(guild_ids=guild_ids) @option('title', description="The biggest text.", required=True) @option('content', description="The actuall text to put in the embed.", required=True) @option('footer', description="The lower text.", required=False) @option('color', description="The color as a word example: Color: dark_blue", required=False) async def embed(self, ctx, title: str, content: str, color: str = None, footer: str = None): if not esconf["commands"]["embed"]: await ctx.respond("This command is disabled.") return 0 embed = discord.Embed(title=title, description=content) if color: try: atr = getattr(discord.Color, color) embed.color = atr() except Exception as e: bot.api.logger("inf", f"Command ivoked a error wrong color: {e}") ctx.send("ERR: Invalid color.") return 1 if footer: embed.set_footer(text=footer) await ctx.send(embed=embed) #testing webhook def setup(bot): bot.add_cog(EssentialCommands(bot))