Error creating bot to display weather on discordbot.py: 'Client' object has no attribute' send_message'

Asked 2 years ago, Updated 2 years ago, 143 views

I use Python 3.7.
It may be difficult to understand because it is the first programming and the first question.
Please specify more and more if you like.

source code

#coding:UTF-8
import discord
import urllib.request
import jso
import re

client=discord.Client()

citycodes={
    Tsuchiura: '080020',
    Mito: '080010',
    Sapporo: '016010',
    Sendai: '040010',
    "Tokyo": '130010',
    Yokohama: '140010',
    "Nagoya": '230010',
    "Osaka": '270000',
    "Hiroshima": '340010',
    Fukuoka: '400010',
    Kagoshima: '460010',
    "Naha": '471010'
}

@client.event
async def on_ready():
  print("logged in as" + client.user.name)

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

    reg_res=re.compile(u"Bot, what's the weather like in (.+)?").search(message.content)
    if reg_res:

      ifreg_res.group(1)incodes.keys():

        citycode=citycodes [reg_res.group(1)]
        resp=urllib.request.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read()
        resp=json.loads(resp.decode('utf-8'))

        msg = resp ['location'] ['city']
        msg+="The weather is \n"
        for fin resp ['foreecasts']:
          msg+=f['dateLabel']+" is "+f['telop']+"\n"
        msg+="."

        wait client.send_message(message.channel, message.author.ment+msg)

      else:
        wait client.send_message(message.channel, "I don't know the weather there...")

client.run ("TOKEN")

Error displayed

Ignoring exception in on_message
Traceback (most recent call last):
  File"/home/pi/.local/lib/python 3.7/site-packages/discord/client.py", line 270, in_run_event
    wait coro (*args, **kwargs)
  File "/home/pi/Desktop/DiscordBOT/TenkiBOT/discordbot.py", line 47, in on_message
    wait client.send_message(message.channel, message.author.ment+msg)
AttributeError: 'Client' object has no attribute' send_message'

python discord

2022-09-30 14:39

1 Answers

Line 47
wait message.channel.send(message.author.ment+msg)
Line 50
wait message.channel.send("I don't know the weather there...")
I'd like you to try it.If you still can't use it,
from discord.ext import commands and
bot=commands.Bot(command_prefix='!') and 5
Line 50 followed by
Enter wait bot.process_commands(message).


2022-09-30 14:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.