From 7c65b6e0637cd4b612d5243fb6b972569bc65859 Mon Sep 17 00:00:00 2001 From: liner <90984207+r-liner@users.noreply.github.com> Date: Sun, 1 Oct 2023 17:42:20 +0300 Subject: [PATCH] v0.7.5 --- main.py | 106 +++++++++++++++++++++++++++++++++----------------- setup.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ start_win.bat | 21 ++++++++-- version.txt | 2 +- 4 files changed, 195 insertions(+), 39 deletions(-) create mode 100644 setup.py diff --git a/main.py b/main.py index 08ff996..1097c3a 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,7 @@ import nextcord import nextcord.errors -from nextcord.ext import commands, tasks +from nextcord.ext import commands, application_checks import os -from itertools import cycle import sys import configparser @@ -11,79 +10,116 @@ config = configparser.ConfigParser() config.read("config.ini") - -if sys.version_info < (3, 8): - exit("You need Python 3.8+ to run the bot.") - -try: - from nextcord import Intents, Client - -except ImportError: - exit("Nextcord isn`t installed or it`s old, unsupported version.") +admin_guild = int(config['settings']['admin_guild']) client = commands.Bot(command_prefix=config["bot"]["prefix"], owner_id=int(config["bot"]["owner_id"]), intents=intents) client.remove_command('help') + @client.event async def on_ready(): await client.change_presence(activity=nextcord.Activity(type=nextcord.ActivityType.watching, name="bot status")) print("Logged in as {0.user}.".format(client)) print("Bot is using on {0} servers!".format(len(client.guilds))) -# загрузка когов + +@client.event +async def on_disconnect(): + if client.is_closed(): + await client.connect() + + +# WORKING WITH COGS -@client.command() -@commands.is_owner() -async def load(ctx, extension): +@application_checks.is_owner() +@client.slash_command(guild_ids=(admin_guild,)) +async def load(interaction, extension): + """ + Loading extension + + Parameters + ---------- + interaction: Interaction + extension: str + Type name of extension to load. + """ try: client.load_extension(f"cogs.{extension}") print(f"Cog {extension} is loaded.") - await ctx.send(f"Cog **{str.upper(extension)}** is loaded.") + await interaction.send(f"Cog **{str.upper(extension)}** is loaded.") except Exception as error: print(error) - await ctx.send("Incorrect name or not able to load") + await interaction.send("Incorrect name or not able to load") + +@application_checks.is_owner() +@client.slash_command(guild_ids=(admin_guild,)) +async def unload(interaction, extension): + """ + Unloading extension -@client.command() -@commands.is_owner() -async def unload(ctx, extension): + Parameters + ---------- + interaction: Interaction + extension: str + Type name of extension to unload. + """ try: client.unload_extension(f"cogs.{extension}") print(f"Cog {str.upper(extension)} is unloaded.") - await ctx.send(f"Cog **{str.upper(extension)}** is unloaded.") + await interaction.send(f"Cog **{str.upper(extension)}** is unloaded.") except Exception as error: print(error) - await ctx.send("Incorrect name or not able to unload") + await interaction.send("Incorrect name or not able to unload") -@client.command() -@commands.is_owner() -async def reload(ctx, extension): +@application_checks.is_owner() +@client.slash_command(guild_ids=(admin_guild,)) +async def reload(interaction, extension): + """ + Reloading extension + + Parameters + ---------- + interaction: Interaction + extension: str + Type name of extension to reload. + """ try: client.unload_extension(f"cogs.{extension}") client.load_extension(f"cogs.{extension}") print(f"Cog {str.upper(extension)} is reloaded.") - await ctx.send(f"Cog **{str.upper(extension)}** is reloaded.") + await interaction.send(f"Cog **{str.upper(extension)}** is reloaded.") except Exception as error: print(error) - await ctx.send("Incorrect name or not able to reload") + await interaction.send("Incorrect name or not able to reload") for filename in os.listdir("./cogs"): if filename.endswith(".py"): client.load_extension(f"cogs.{filename[:-3]}") -try: - client.run(config["bot"]["token"]) +if __name__ == '__main__': + if sys.version_info < (3, 8): + exit("You need Python 3.8+ to run the bot.") + + try: + from nextcord import Intents, Client + + except ImportError: + exit("Nextcord isn`t installed or it`s old, unsupported version.") + + try: + client.run(config["bot"]["token"]) -except Exception as err: - print(err) + except Exception as err: + print(err) -except nextcord.PrivilegedIntentsRequired: - exit("Login failure! Privileged Intents not enabled.") + except nextcord.PrivilegedIntentsRequired: + exit("Login failure! Privileged Intents not enabled.") -except nextcord.errors.LoginFailure: - exit("Login failure! Token is required.") + except nextcord.errors.LoginFailure: + exit("Login failure! Token is required.") diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3e17025 --- /dev/null +++ b/setup.py @@ -0,0 +1,105 @@ +import configparser +from shutil import copy + +print("""## Welcome to bot setup ## + +If you have additional questions type 'help' on each of answers. +If you wanna abort the setting up just close the program. +Please make sure that you will input the right data. +""") + +while True: + token = input('Please enter your token here: ') + + if token.lower() == 'help': + print("""Open the discord developer portal, create your application and copy your token. + https://discord.com/developers/applications""") + + elif token == '': + continue + + else: + break + +while True: + owner_id = input('\nPlease enter your discord id: ') + + if owner_id.lower() == 'help': + print("""Turn on developer mode in discord app settings. Then copy your id in the profile. + It will need for some commands.""") + + elif owner_id == '': + continue + + else: + break + +while True: + prefix = input('\nPlease enter bot prefix: ') + + if prefix.lower() == 'help': + print("""Prefix will be used for some ctx commands.""") + + elif prefix == '': + continue + + else: + break + +while True: + admin_guild = input('\nPlease enter admin guild id: ') + + if admin_guild.lower() == 'help': + print("""Admin guild id used for some commands.""") + + elif admin_guild == '': + continue + + else: + break + +while True: + openweather_token = input('\nPlease enter openweather token (type "skip" for skip): ') + + if openweather_token.lower() == 'help': + print("""Open https://openweathermap.org/ then login. Create token, copy and paste there.""") + + elif openweather_token.lower() == 'skip': + break + + elif openweather_token == '': + continue + + else: + break + +while True: + is_save = input('\nSave settings? (Yes/No): ') + + if is_save.lower() == 'yes': + break + + elif is_save.lower() == 'no': + exit('\nOkay. Try again.') + + else: + continue + +copy('config.ini.sample', 'config.ini') + +config = configparser.ConfigParser() +config.read('config.ini') + + +config['bot']['token'] = token +config['bot']['owner_id'] = owner_id +config['bot']['prefix'] = prefix + +config['settings']['admin_guild'] = admin_guild + +config['weather']['openweather_token'] = '' if openweather_token == 'skip' else openweather_token + +with open('config.ini', 'w') as f: + config.write(f) + +print('\nDone. Now you can run the bot.') \ No newline at end of file diff --git a/start_win.bat b/start_win.bat index 05ebdfb..4725dd7 100644 --- a/start_win.bat +++ b/start_win.bat @@ -1,8 +1,12 @@ @echo off -title discord-bot-ru +title DISCORD BOT cls +if not exist config.ini ( + goto setup +) + >nul 2>nul assoc .py if errorlevel 1 ( goto install_python @@ -12,10 +16,10 @@ if errorlevel 1 ( :install_python cls -echo You must download Python ver 3.8 or newer! +echo You must install Python 3.8 or above! echo Install Python and try again. echo. -echo If Python has installed, reinstall and add it to PATH via "Add Python to PATH" during installation +echo If python already installed, reinstall and add it to PATH via "Add Python to PATH" during installation start https://www.python.org/downloads/ echo. @@ -27,3 +31,14 @@ py main.py pause exit + +:setup +cls + +echo You must setup the bot firstly +echo. + +py setup.py + +pause +exit \ No newline at end of file diff --git a/version.txt b/version.txt index 0a1ffad..da2ac9c 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.7.4 +0.7.5 \ No newline at end of file