Search code examples
python-requestsdiscord.pytimezonedb

My Discord bot isn't responding to a command to pull data from timezonedb


So it can actually show the info I want it to in the terminal. But when I prompt it to send it as a discord message it appears to be attempting to send a blank message. It's probably something stupid, but thank you for looking. The language is Python.

import os
import discord
import requests
import json
import pprint

client = discord.Client()

def get_time():
  response = requests.get("http://api.timezonedb.com/v2.1/get-time-zone?key=W9BJQ3QMGG69&format=json&by=position&lat=37.9838&lng=23.7275")
  return pprint.pprint(response.json())
  

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


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

    if message.content.startswith('$petertest'):
        clock = get_time()
        await message.channel.send(clock)


client.run(os.environ['TOKEN'])

Solution

  • You are using the pprint module to print the data to the console itself. That is the issue there

    Changing your code to simply return the data will fix the error.

    return response.json()
    

    If you want to send the formatted json data to discord, you can use json.dumps:

    if message.content.startswith('test'):
        clock = get_time()
        clock = json.dumps(clock, indent=4)
        await message.channel.send(clock)