[Python] Space separated words (strings) in user input in discordpy

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I am working with discordpy in order to make my first simple bot that retrieves data from the open weather API and then displays it to a discord channel based on the $weather City Name and:

    I am trying to figure out how to make it so that when the input is entered, a string that has 2 or more words, e.g. "New York" can be interpreted by Python even without wrapping it between " ".

    This is what I've got until now. It works via $weather "City Name", but if I try without " ", it will only take the first word as input and neglect the rest of the string.

    import discord
    from discord.ext import commands
    import requests
    import json

    def get_weather(city):
    url = "https://api.openweathermap.org/data/2.5/weather"
    parameters = {

    "q":city,
    "appid":"d9451e04c3b2a30d46a555f28a3c3724",
    "units": "metric",
    "lang": "ro"

    }
    response = requests.get(url=url, params=parameters,)
    response.raise_for_status()

    json_data = response.json()

    weather_description = json_data["weather"][0]["description"]
    main_temp = json_data["main"]["temp"]
    return weather_description, main_temp

    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)

    bot = commands.Bot(command_prefix='$', intents=intents)


    @bot.event
    async def on_ready():
    print("Bot Ready!")

    @bot.command()
    async def weather(ctx, arg):
    await ctx.send(arg) #debugs
    converted_arg = "+".join(arg.split())
    print(converted_arg) #debugs
    await ctx.send(get_weather(converted_arg))

    @bot.command()
    async def test2(ctx, arg):
    print(ctx.send(arg))

    Continue reading...

Compartilhe esta Página