Discord bot, extra input for functions and showing the input message without "returning" me from the function

28 views Asked by At

I need the discord bot to take extra input for functions or rather find a way so while I use the bot in python, not only return something without exiting the function made for the messages but also take a new input later different from the 'message'.

The code splits into three files, the 'main.py'

#!/usr/bin/env python
import bot

if __name__ == '__main__':
    bot.run_discord_bot()

The second file:

import sys
import discord
import responses


TOKEN = f"HERE GOES THE API I HAVE FROM DISCORD"


async def send_message(message, user_message, is_private):
    try:
        response = responses.get_response(user_message)  # needs to be implemented
        await message.author.send(response) if is_private else await message.channel.send(response)

    except Exception as e:
        print(e)

    except SystemExit:
        sys.exit()

    finally:
        pass


def run_discord_bot():
    TOKEN = f"HERE GOES THE API I HAVE FROM DISCORD"
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
        print(f"{client.user} is now running")

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return

        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)

        print(f'{username} said: "{user_message}" ({channel})')

        if user_message[0] == '?':
            user_message = user_message[1:]
            await send_message(message, user_message, is_private=True)
        else:
            await send_message(message, user_message, is_private=False)

    client.run(TOKEN)

And in the third file:

import  sys
import random


def get_response(message: str) -> str:
    p_message = message.lower()

    if message == 'Exitbot;':
        sys.exit()

    if p_message == 'hello':
        return 'Hey there!'

    if message == 'roll':
        return str(random.randint(1, 6))

    if p_message == '!help':
        return '`This is a help message that you can modify.`'

        if message.split(" ")[0] == "Find" and message.split(" ")[1] == "the" and message.split(" ")[2] == "average" and message.split(" ")[3] == "for" and message.split(" ")[4] == int or float and message.split(" ")[5] == "values:":
        print(message.split(" ")[3])
        average(int(message.split(" ")[3]))
        return f"The average is {av}"

    return 'I didn\'t understand what you wrote. Try typing "!help".'


def average(n):
    global av
    sum = 0
    for i in range(n):
        a = input("Enter a number: ").replace(",", ".")
        sum += float(a)
    av = (float(sum) / int(n))
    return av

In the last part, where I want it to find me the avarage I tried many things like using functions and in different ways in order to show as the discord response the input message and take it, but to no avail. (The part I have above is something after those tries in which I did now try to output the input message as well as the final output.) I also tried to use yield for that part instead of extra functions but the output at the bot was: <generator object get_response at 0x0000012B179E4150>.

In the end, I decided instead of what I was trying to do at first to write it all at once like this to solve that particullar problem:

    if message.split(" ")[0] == "Find" and message.split(" ")[1] == "the" and message.split(" ")[2] == "average" and message.split(" ")[3] == "for" and message.split(" ")[4] == int or float and message.split(" ")[5] == "values:":
        global sum, av
        sum = 0
        for i in range(int(message.split(" ")[4])):
            sum += float((message.split(" ")[6 + i]).replace(",", "."))
        av = (float(sum) / int(int(message.split(" ")[4])))
        return f"The average is {av}"

    return 'I didn\'t understand what you wrote. Try typing "!help".'

I am a beginner with creating disc bots and I haven't really understood if I could do something different.

Main problem reamains, is there a way after taking thee "message", talking another message too and furthermore for inputs to show at the bot the message? The reason for asking this is for the next time something similar comes up.

I hope you can help me.

0

There are 0 answers