From 8b983618f2b37f8a80ca1a696c2f8f95484aa567 Mon Sep 17 00:00:00 2001 From: ElBe <90863907+ElBe-Plaq@users.noreply.github.com> Date: Sat, 12 Nov 2022 22:12:42 +0100 Subject: [PATCH] Updated to v1.9.3 --- Bot/bot_functions.py | 106 +++++++++++ Bot/command.json | 3 + Bot/command.py | 116 ++++++++++++ Bot/command_creator.py | 62 +++---- Bot/config.json | 44 +++-- Bot/main.py | 407 +++++++++++++++++++++++++++++++++++++++++ Bot/version.py | 1 + 7 files changed, 685 insertions(+), 54 deletions(-) create mode 100644 Bot/bot_functions.py create mode 100644 Bot/command.json create mode 100644 Bot/command.py create mode 100644 Bot/main.py create mode 100644 Bot/version.py diff --git a/Bot/bot_functions.py b/Bot/bot_functions.py new file mode 100644 index 0000000..1b725ba --- /dev/null +++ b/Bot/bot_functions.py @@ -0,0 +1,106 @@ +''' +Functions for the discord.py Bot. +© by ElBe. + +Version: 0.1.1 +''' + +#Imports +import json +import os +import colorama +import datetime +import re + +class variables(): + '''All variables used in this module.''' + standart_config_file = 'config.json' + standart_datetime_format = datetime.date.isoformat + +class json_module(): + def get_config(name: str, file = variables.standart_config_file): + '''Returns a value from the given/standart JSON file.''' + with open(file, 'r') as f: + return json.load(f)[name] + + def write_json(data, show_text = False, file = variables.standart_config_file): + '''Writes the text to the given/standart JSON file.''' + with open(file, 'w') as f: + json.dump(data, f) + f.close() + + if show_text: + print(console.log('Data ' + str(data) + ' added to ' + str(file) + '.')) + +class console(): + def info(text: str): + '''Returns a info text.''' + i = 0 + if len(re.findall('\n', text)) > 1: + text = '\n' + text + search = len(re.findall('\n', text)) + + if search > 1: + for i in range(search): + text = text.replace('\n', '//n[' + colorama.Fore.LIGHTBLUE_EX + str(i + 1) + colorama.Style.RESET_ALL + '] ', 1) + i = i + 1 + text = text.replace('//n', '\n') + return colorama.Fore.LIGHTBLUE_EX + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + colorama.Style.RESET_ALL + ' [' + colorama.Fore.GREEN + 'INFO' + colorama.Style.RESET_ALL + '] ' + text + + def error(text: str): + '''Returns a error text.''' + i = 0 + if len(re.findall('\n', text)) > 1: + text = '\n' + text + search = len(re.findall('\n', text)) + + if search > 1: + for i in range(search): + text = text.replace('\n', '//n[' + colorama.Fore.LIGHTBLUE_EX + str(i + 1) + colorama.Style.RESET_ALL + '] ', 1) + i = i + 1 + text = text.replace('//n', '\n') + return colorama.Fore.LIGHTBLUE_EX + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + colorama.Style.RESET_ALL + ' [' + colorama.Fore.RED + 'ERROR' + colorama.Style.RESET_ALL + '] ' + text + + def warn(text: str): + '''Returns a warn text.''' + i = 0 + if len(re.findall('\n', text)) > 1: + text = '\n' + text + search = len(re.findall('\n', text)) + + if search > 1: + for i in range(search): + text = text.replace('\n', '//n[' + colorama.Fore.LIGHTBLUE_EX + str(i + 1) + colorama.Style.RESET_ALL + '] ', 1) + i = i + 1 + text = text.replace('//n', '\n') + return colorama.Fore.LIGHTBLUE_EX + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + colorama.Style.RESET_ALL + ' [' + colorama.Fore.YELLOW + 'WARNING' + colorama.Style.RESET_ALL + '] ' + text + + def log(text: str): + '''Returns a log text.''' + i = 0 + if len(re.findall('\n', text)) > 1: + text = '\n' + text + search = len(re.findall('\n', text)) + + if search > 1: + for i in range(search): + text = text.replace('\n', '//n[' + colorama.Fore.LIGHTBLUE_EX + str(i + 1) + colorama.Style.RESET_ALL + '] ', 1) + i = i + 1 + text = text.replace('//n', '\n') + return colorama.Fore.LIGHTBLUE_EX + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + colorama.Style.RESET_ALL + ' [LOG] ' + text + + def clear(): + '''Clear the console.''' + os.system('cls') + + def crusor_up(): + '''Changes the position of the crusor to the line above.''' + print('\x1b[1A') + + def erase_line(): + '''Erases the current line.''' + print('\x1b[2K') + + def erase_last(): + '''Erases the last line.''' + print('\x1b[1A' + '\x1b[2K' + '\x1b[1A') \ No newline at end of file diff --git a/Bot/command.json b/Bot/command.json new file mode 100644 index 0000000..280367b --- /dev/null +++ b/Bot/command.json @@ -0,0 +1,3 @@ +{ + "Created": 0 +} \ No newline at end of file diff --git a/Bot/command.py b/Bot/command.py new file mode 100644 index 0000000..772d4df --- /dev/null +++ b/Bot/command.py @@ -0,0 +1,116 @@ +''' +Command creator for the discord.py Bot. + +© by ElBe. + +Version: 0.1.6 + +NOTE: Only execute once. +''' + +#Imports +import command_creator +import bot_functions + +class run(): + def __init__(self): + #Stop command + json = { + 'name': 'stop', + 'type': 1, + 'description': 'Stops the bot.' + } + command_creator.create_command(json) + + #Help command + json = { + 'name': 'help', + 'type': 1, + 'description': 'Shows you help about the bot.' + } + command_creator.create_command(json) + + #Ping command + json = { + 'name': 'ping', + 'type': 1, + 'description': 'Shows you the ping of the bot in ms.' + } + command_creator.create_command(json) + + #Info command + json = { + 'name': 'info', + 'type': 1, + 'description': 'Shows you information about the bot.' + } + command_creator.create_command(json) + + #Kick command + json = { + 'name': 'kick', + 'type': 1, + 'description': 'Kicks a member from the server.', + "options": [ + { + "name": "member", + "description": "The member to kick.", + "type": 6, + "required": True + }, + { + "name": "reason", + "description": "Reason for the kick.", + "type": 3, + "required": False + } + ] + } + command_creator.create_command(json) + + #Ban command + json = { + 'name': 'ban', + 'type': 1, + 'description': 'Bans a member from the server.', + "options": [ + { + "name": "member", + "description": "The member to ban.", + "type": 6, + "required": True + }, + { + "name": "reason", + "description": "Reason for the ban.", + "type": 3, + "required": False + } + ] + } + command_creator.create_command(json) + + #Unban command + json = { + 'name': 'unban', + 'type': 1, + 'description': 'Unbans a member from the server.', + "options": [ + { + "name": "member", + "description": "The member to unban.", + "type": 6, + "required": True + }, + { + "name": "reason", + "description": "Reason for the unban.", + "type": 3, + "required": False + } + ] + } + command_creator.create_command(json) + + #File + bot_functions.json_module.write_json({"Created": 1}, False, 'command.json') \ No newline at end of file diff --git a/Bot/command_creator.py b/Bot/command_creator.py index 548ccfb..3159218 100644 --- a/Bot/command_creator.py +++ b/Bot/command_creator.py @@ -1,31 +1,31 @@ -''' -Slash command creator for the discord.py Bot. -© by ElBe. - -Version: 0.1.4 -''' - -#Imports -import requests -import functions - -#Variables -url = 'https://discord.com/api/v10/applications/' + str(functions.json_module.get_config('Config')['Application ID']) + '/commands' -headers = { - 'Authorization': 'Bot ' + str(functions.json_module.get_config('Config')['Token']) -} - -#Example Command -json = { - 'name': 'ping', - 'type': 1, - 'description': 'Shows you the ping of the bot.' -} - -#Function -def create_command(json): - try: - r = requests.post(url, headers=headers, json=json) - print('Command /' + str(json['name']) + ' was sucessfully created.') - except Exception as e: - print('Error while trying to create the command /' + str(json['name']) + '.\n' + str(e)) +''' +Slash command creator for the discord.py Bot. +© by ElBe. + +Version: 0.1.5 +''' + +#Imports +import os +import requests + +#Variables +url = 'https://discord.com/api/v10/applications/' + str(os.environ['application_id']) + '/commands' +headers = { + 'Authorization': 'Bot ' + str(os.environ['token']) +} + +#Example Command +json = { + 'name': 'ping', + 'type': 1, + 'description': 'Shows you the ping of the bot.' +} + +#Function +def create_command(json): + try: + r = requests.post(url, headers=headers, json=json) + print('Command /' + str(json['name']) + ' was sucessfully created.') + except Exception as e: + print('Error while trying to create the command /' + str(json['name']) + '.\n' + str(e)) \ No newline at end of file diff --git a/Bot/config.json b/Bot/config.json index 660690a..c79a8c7 100644 --- a/Bot/config.json +++ b/Bot/config.json @@ -1,23 +1,21 @@ -{ - "Config": { - "Token": "", - "Application ID": "", - "Version": "0.1.9.2 final", - "Version-Comment": "Added error handling.", - "Footer": "Made by ElBe.", - "Credits": false - }, - "Channels": { - "Welcome": "", - "Goodbye": "" - }, - "Commands": { - "stop": true, - "help": true, - "info": true, - "ping": true, - "kick": true, - "ban": true, - "unban": true - } -} +{ + "Config": { + "Version": "1.9.2 final-r", + "Version-Comment": "Bugfix and error handler.", + "Footer": "Bot made by ElBe.", + "Credits": true + }, + "Channels": { + "Welcome": "", + "Goodbye": "" + }, + "Commands": { + "stop": true, + "help": true, + "info": true, + "ping": true, + "kick": true, + "ban": true, + "unban": true + } +} \ No newline at end of file diff --git a/Bot/main.py b/Bot/main.py new file mode 100644 index 0000000..93a2f12 --- /dev/null +++ b/Bot/main.py @@ -0,0 +1,407 @@ +''' +Discord bot template. +© by ElBe. + +Version: 1.9.2-R +''' + +#Imports +import os +import discord +from discord import utils +import asyncio +import datetime +import time +import logging +import platform +import psutil + +#Bot modules +import bot_functions +import command + +#Start +print('Discord.py Bot') +print('----------------------------') +print('© by ElBe 2022.') +print('') +print('Start Informations') +print('------------------') +print('Discord version: ' + discord.__version__) +print('Bot version: ' + + bot_functions.json_module.get_config('Config')['Version']) +print('') +print('Starting') +print('--------') + +#Variables +bold = '**' +italic = '*' +underline = '_' +stroke = '~~' +MISSING = utils.MISSING + +#Starttime +starttime = time.time() + +#JSON data +try: + token = os.environ['token'] + version = bot_functions.json_module.get_config('Config')['Version'] + credits = bot_functions.json_module.get_config('Config')['Credits'] + footer = bot_functions.json_module.get_config('Config')['Footer'] + welcomeChannel = bot_functions.json_module.get_config('Channels')['Welcome'] + goodbyeChannel = bot_functions.json_module.get_config('Channels')['Goodbye'] + commands = bot_functions.json_module.get_config('Commands') +except Exception as e: + print(bot_functions.console.error('Error while trying to get data from the config file.\n' + str(e))) + +#Setup +logging.basicConfig(filename='log.txt', level=logging.INFO) +intents = discord.Intents.all() + +#Create commands +try: + if bot_functions.json_module.get_config('Created', 'command.json') == 0: + command.run() +except Exception as e: + print(bot_functions.console.error('Error while trying to create the commands.\n' + str(e))) + +#Main +class Bot(discord.Client): + '''Bot.''' + + async def on_connect(self): + logging.info( + str(datetime.datetime.now()) + ' Bot connected to the Discord API.') + print(bot_functions.console.info('Bot connected to the Discord API.')) + + async def on_ready(self): + logging.info( + str(datetime.datetime.now()) + ' Bot logged in as ' + client.user.name + + '.') + print( + bot_functions.console.info('Bot logged in as ' + client.user.name + '.')) + print('') + print('Log (Consolebased)') + print('------------------') + + while True: + await client.change_presence(activity=discord.Activity( + type=discord.ActivityType.watching, name=f'/help for help.')) + await asyncio.sleep(10) + await client.change_presence(activity=discord.Game( + name=f'Version {version}')) + await asyncio.sleep(10) + if credits: + await client.change_presence(activity=discord.Game( + name=' programmed by ElBe.')) + await asyncio.sleep(10) + + async def on_resumed(self): + logging.info( + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + + ' -- Bot resumed session.') + print(bot_functions.console.log('Bot resumed a session.')) + + async def on_interaction(self, interaction): + + async def log(text: str): + '''Log a text and save it in the logfile and the console.''' + logging.info( + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + + str(text)) + print(bot_functions.console.log(str(text))) + + if interaction.type == discord.InteractionType.application_command: + command_name = str(interaction.data['name']) + await log('Command /' + command_name + ' was used by @' + + str(interaction.user) + '.') + + if command_name == 'ping' and commands['ping']: + pingEmbed = discord.Embed(title='Ping', + description='Ping of the bot in ms.') + pingEmbed.add_field(name='Latency: ', + value=str(round(client.latency * 1000)) + ' ms', + inline=False) + pingEmbed.set_thumbnail(url=client.user.avatar.url) + pingEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=pingEmbed, + ephemeral=True) + elif command_name == 'ping' and not commands['ping']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + if command_name == 'info' and commands['info']: + infoEmbed = discord.Embed(title='Information', + description='Informations about the bot.') + infoEmbed.add_field(name='Running on: ', + value=str(platform.system()) + ' ' + + str(platform.release()), + inline=False) + infoEmbed.add_field(name='CPU usage: ', + value=str(psutil.cpu_percent(2)) + '%', + inline=False) + infoEmbed.add_field( + name='Uptime: ', + value=str( + datetime.timedelta(seconds=int(round(time.time() - starttime)))), + inline=False) + infoEmbed.set_thumbnail(url=client.user.avatar.url) + infoEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=infoEmbed, + ephemeral=True) + elif command_name == 'info' and not commands['info']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + if command_name == 'stop' and commands['stop']: + if interaction.user.guild_permissions.administrator: + await log('Client stoped.') + exit() + else: + noPermissionsEmbed = discord.Embed( + title='Missing permissions', + description='You don\'t have permissions to do that.', + color=discord.Color.red()) + noPermissionsEmbed.set_thumbnail(url=client.user.avatar.url) + noPermissionsEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=noPermissionsEmbed, + ephemeral=True) + elif command_name == 'stop' and not commands['stop']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + if command_name == 'help' and commands['help']: + helpEmbed = discord.Embed( + title='Help', + description='Help for commands and the usage of the bot.') + helpEmbed.add_field(name='Ping', + value='Shows the ping of the bot in ms.', + inline=True) + helpEmbed.add_field(name='Info', + value='Shows information about the bot.', + inline=True) + helpEmbed.add_field( + name='Help', + value='Shows you help for commands and the usage of the bot.', + inline=True) + helpEmbed.add_field(name='Kick', + value='Kicks a member from the server.', + inline=True) + helpEmbed.add_field(name='Ban', + value='Bans a member from the server.', + inline=True) + helpEmbed.add_field(name='Unban', + value='Unbans a member from the server.', + inline=True) + helpEmbed.add_field( + name='more', + value= + 'For more help write a direct message to <@!745695237380243457>.', + inline=False) + helpEmbed.set_thumbnail(url=client.user.avatar.url) + helpEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=helpEmbed, + ephemeral=True) + + if command_name == 'kick' and commands['kick']: + options = interaction.data['options'] + if interaction.user.guild_permissions.administrator: + member = options[0]['value'] + member = await interaction.guild.fetch_member(int(member)) + reason = options[1]['value'] + if reason == None: + reason = 'Kicked by @' + str( + interaction.user) + ' with the /kick command.' + + await member.kick(reason=reason) + + kickEmbed = discord.Embed(title='Kick', + description='Succesfully kicked <@!' + + options[0]['value'] + '>!') + kickEmbed.set_thumbnail(url=client.user.avatar.url) + kickEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=kickEmbed, + ephemeral=True) + else: + noPermissionsEmbed = discord.Embed( + title='Missing permissions', + description='You don\'t have permissions to do that.', + color=discord.Color.red()) + noPermissionsEmbed.set_thumbnail(url=client.user.avatar.url) + noPermissionsEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=noPermissionsEmbed, + ephemeral=True) + elif command_name == 'kick' and not commands['kick']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + if command_name == 'ban' and commands['ban']: + options = interaction.data['options'] + if interaction.user.guild_permissions.administrator: + member = options[0]['value'] + member = await interaction.guild.fetch_member(int(member)) + reason = options[1]['value'] + if reason == None: + reason = 'Banned by @' + str( + interaction.user) + ' with the /ban command.' + + await member.ban(reason=reason) + + banEmbed = discord.Embed(title='Ban', + description='Succesfully banned <@!' + + options[0]['value'] + '>!') + banEmbed.set_thumbnail(url=client.user.avatar.url) + banEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=banEmbed, + ephemeral=True) + else: + noPermissionsEmbed = discord.Embed( + title='Missing permissions', + description='You don\'t have permissions to do that.', + color=discord.Color.red()) + noPermissionsEmbed.set_thumbnail(url=client.user.avatar.url) + noPermissionsEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=noPermissionsEmbed, + ephemeral=True) + elif command_name == 'ban' and not commands['ban']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + if command_name == 'unban' and commands['unban']: + options = interaction.data['options'] + if interaction.user.guild_permissions.administrator: + member = options[0]['value'] + member = await client.fetch_user(int(member)) + reason = options[1]['value'] + if reason == None: + reason = 'Unbanned by @' + str( + interaction.user) + ' with the /unban command.' + + await interaction.guild.unban(member, reason=reason) + + unbanEmbed = discord.Embed(title='Unban', + description='Succesfully unbanned <@!' + + options[0]['value'] + '>!') + unbanEmbed.set_thumbnail(url=client.user.avatar.url) + unbanEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=unbanEmbed, + ephemeral=True) + else: + noPermissionsEmbed = discord.Embed( + title='Missing permissions', + description='You don\'t have permissions to do that.', + color=discord.Color.red()) + noPermissionsEmbed.set_thumbnail(url=client.user.avatar.url) + noPermissionsEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=noPermissionsEmbed, + ephemeral=True) + elif command_name == 'unban' and not commands['unban']: + commandDisabledEmbed = discord.Embed( + title='Command disabled', + description= + 'This command was disabled in the bot config file. Ask a member with access to the bot to enable this command.' + ) + commandDisabledEmbed.set_thumbnail(url=client.user.avatar.url) + commandDisabledEmbed.set_footer(text=footer) + await interaction.response.send_message(embed=commandDisabledEmbed, + ephemeral=True) + + async def on_member_join(self, member): + + async def log(text: str): + '''Log a text and save it in the logfile and the console.''' + logging.info( + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + + str(text)) + print(bot_functions.console.log(str(text))) + + if not member == client.user: + channel = discord.utils.get(member.guild.text_channels, + name=welcomeChannel) + joinEmbed = discord.Embed( + title='Welcome!', + description='Hello <@!' + str(member.id) + + f'>! \nThank you for joining {member.guild.name}!') + joinEmbed.set_thumbnail(url=member.avatar.url) + joinEmbed.set_footer(text=footer) + await channel.send(embed=joinEmbed) + await member.add_roles( + discord.utils.get(member.guild.roles, name='member')) + await log('@' + str(member) + ' joined to ' + str(member.guild.name) + + '.') + + async def on_member_remove(self, member): + + async def log(text: str): + '''Log a text and save it in the logfile and the console.''' + logging.info( + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- ' + + str(text)) + print(bot_functions.console.log(str(text))) + + if not member == client.user: + channel = discord.utils.get(member.guild.text_channels, + name=goodbyeChannel) + leaveEmbed = discord.Embed(title='Goodbye!', + description='<@!' + str(member.id) + + f'> left {member.guild.name}.') + leaveEmbed.set_thumbnail(url=member.avatar.url) + leaveEmbed.set_footer(text=footer) + await channel.send(embed=leaveEmbed) + await log('@' + str(member) + ' left ' + str(member.guild.name) + '.') + + async def on_disconnect(self): + + async def error(text: str): + '''Send a error text and save it in the logfile.''' + logging.info( + str(datetime.datetime.now().strftime('%d.%m.%Y %T')) + ' -- Error: ' + + str(text)) + print(bot_functions.console.error(str(text))) + + await error('Bot disconected from discord.') + + +#Run +try: + client = Bot(intents=intents, max_messages=None) + client.run(token, log_handler=None) +except Exception as e: + print(bot_functions.console.error('Error while trying to connect to discord.\n' + str(e))) \ No newline at end of file diff --git a/Bot/version.py b/Bot/version.py new file mode 100644 index 0000000..acbf182 --- /dev/null +++ b/Bot/version.py @@ -0,0 +1 @@ +__version__ = '1.9.2' \ No newline at end of file