@bot.command() и @bot.event() клонируются. discord.py
from discord.ext import commands
import random
from asyncio import sleep
bot = commands.Bot(command_prefix='!') #инициализируем бота с префиксом(то есть бот отвечает на сообщения, начинающиеся с префикса '!')
hello_words = ['hello', 'hi', 'privet', 'qq', 'ky', 'ку', 'q', 'helo','привет']
goodbye_word = ['бб', 'bb', 'goodbye', 'пока', 'poka', 'прощай', 'до завтра', 'bb all', 'бб олл', 'бб алл',]
pep = ["Ая, как дела?","Ая, как дела","ая, как дела?","ая, как дела","Ая, как ты себя чувствуешь?","ая, как ты себя чувствуешь?","Ая, как ты себя чувствуешь","ая, как ты себя чувствуешь","pep"]
@bot.event
async def on_ready():
print("Я готова:З")
@bot.command()
async def шанс(ctx):
if ctx.author != bot.user:
choice = random.randint(0, 100)
await ctx.send(f"шанс равен {choice} %")
@bot.command()
async def орел_решка(ctx):
await ctx.send(random.choice(['орел', 'решка']))
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound ):
await ctx.send(embed = discord.Embed(description = f'** {ctx.author.name}, данной команды не существует.**', color=0x0c0c0c))
@bot.listen()
async def on_message(message):
msg = message.content.lower()
if msg in hello_words:
await message.channel.send(f'Привет, чем могу помочь?')
await bot.process_commands(message)
@bot.listen()
async def on_message(message):
msg = message.content.lower()
if msg in goodbye_word:
await message.channel.send(f'Что?, ты уже уходишь? Ладно, пока-пока!')
await bot.process_commands(message)
@bot.listen()
async def on_message(message):
msg = message.content.lower()
if msg in pep:
await message.channel.send(f"Я еще не чувствую боли... Мой создатель явно халтурщик..")
await bot.process_commands(message)
bot.run(token)```
Сообщения от бота клонируются, причем и ивенты, и команды
Ответы (1 шт):
Автор решения: Corrygan
→ Ссылка
А в чём проблема оптимизировать код и делать все проверки в одном ивенте on_message()
@bot.event
async def on_message(message):
msg = message.content.lower()
if msg in hello_words:
await message.channel.send(f'Привет, чем могу помочь?')
elif msg in goodbye_word:
await message.channel.send(f'Что?, ты уже уходишь? Ладно, пока-пока!')
elif msg in pep:
await message.channel.send(f"Я еще не чувствую боли... Мой создатель явно халтурщик..")
await bot.process_commands(message)