diff --git a/cogs/listeners.py b/cogs/listeners.py index 895cdb7..927ad7b 100644 --- a/cogs/listeners.py +++ b/cogs/listeners.py @@ -6,6 +6,7 @@ from rich import print from config import settings + class ErrorListener(commands.Cog): def __init__(self, bot): self.bot = bot @@ -23,12 +24,15 @@ async def on_command_error(self, ctx, error): else: raise error + class OnReady(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_ready(self): + await self.bot.tree.sync() + print('[b green] Bot is ready! Just type d.help to see all bot commands.') while True: activity = random.choice(settings['activities']) diff --git a/cogs/slash.py b/cogs/slash.py new file mode 100644 index 0000000..07a09ae --- /dev/null +++ b/cogs/slash.py @@ -0,0 +1,64 @@ +from config import settings + +import discord +from discord import app_commands +from discord.ext import commands +from googletrans import Translator + + +class Slash(commands.Cog): + """Основные слэш-команды""" + + def __init__(self, bot): + self.bot = bot + + @app_commands.command(name="echo", description="Выводит текст от лица бота") + @app_commands.describe(message="Твоё сообщение, которое я напишу за тебя") + async def echo(self, interaction: discord.Interaction, message: str): + await interaction.response.send_message(message) + + @app_commands.command(name="poll", description="ГАЛАСАВАНИЕ") + @app_commands.describe(question="Задай тему голосования", + option1="Первый вариант ответа (обязательно)", + option2="Второй вариант ответа", + option3="Третий варик", + option4="Ну ты понял короче") + async def poll(self, interaction: discord.Interaction, + question: str, option1: str, option2: str=None, + option3: str="None", option4: str="None", option5: str="None", + option6: str="None", option7: str="None", option8: str="None"): + + options_template = [option1, option2, option3, + option4, option5, option6, + option7, option8] + + options = [] + for opt in options_template: + if opt is not "None": + options.append(opt) + + if len(options) < 1: + await interaction.response.send_message('❌ Для создания голосования нужно хотя-бы 1 ответ!') + return + + reactions_template = ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '🔟'] + reactions = [] + for emoji in range(len(options)): + reactions.append(reactions_template[emoji]) + + description = [] + for x, option in enumerate(options): + description += '\n{} {}'.format(reactions[x], option) + + embed = discord.Embed(color=0xffcd4c, + title=f'{self.bot.get_emoji(settings["emojis"]["stonks"])} {interaction.user}: {question}', + description=''.join(description)) + + await interaction.response.send_message(embed=embed) + react_message = await interaction.original_response() + for reaction in reactions[:len(options)]: + await react_message.add_reaction(reaction) + + +async def setup(bot): + await bot.add_cog(Slash(bot)) diff --git a/cogs/text.py b/cogs/text.py index 71e152c..915236c 100644 --- a/cogs/text.py +++ b/cogs/text.py @@ -3,6 +3,7 @@ from googletrans import Translator from config import settings + class Text(commands.Cog): def __init__(self, bot): self.bot = bot @@ -26,14 +27,11 @@ async def translate(self, ctx, lang, *, text): title = "❌ Указан неверный язык!", description = warntext)) - @commands.command() async def echo(self, ctx, *, arg): await ctx.message.delete() await ctx.send(arg) - - @commands.command() async def poll(self, ctx, question, *options: str): lowercase = [opts.lower() for opts in options] @@ -63,5 +61,6 @@ async def poll(self, ctx, question, *options: str): embed.set_footer(text= f'Poll ID: {react_message.id} \nКстати! Вопрос нужно указывать в кавычках!' ) await react_message.edit(embed=embed) + async def setup(bot): await bot.add_cog(Text(bot)) \ No newline at end of file diff --git a/main.py b/main.py index 7edd645..43e0237 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,11 @@ -from cogs import converters, fun, help, moderation, information, music, listeners, text +from cogs import converters, fun, help, moderation, information, music, listeners, text, slash from config import settings import sys import asyncio from rich import print -from discord import Intents +from discord import Intents, app_commands from discord.ext import commands print(f'[b yellow]Python {sys.version}') @@ -40,6 +40,9 @@ asyncio.run(text.setup(bot)) print('[blue]text.py file has been loaded!') +asyncio.run(slash.setup(bot)) +print('[blue]slash.py file has been loaded!') + print('[b i blue]All cogs has been loaded. Starting...') bot.run(settings['token'])