Python Forum
'Client' object has no attribute 'send_message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'Client' object has no attribute 'send_message
#1
this is my discord bot what am I doing wrong PLEASE HELP

import discord

import request

import json




# command handler class



class CommandHandler:



# constructor

def __init__(self, client):

self.client = client

self.commands = []



def add_command(self, command):

self.commands.append(command)



def command_handler(self, message):

for command in self.commands:

if message.content.startswith(command['trigger']):

args = message.content.split(' ')

if args[0] == command['trigger']:

args.pop(0)

if command['args_num'] == 0:

return self.client.send_message(message.channel, str(command['function'](message, self.client, args)))

break

else:

if len(args) >= command['args_num']:

return self.client.send_message(message.channel, str(command['function'](message, self.client, args)))

break

else:

return self.client.send_message(message.channel, 'command "{}" requires {} argument(s) "{}"'.format(command['trigger'], command['args_num'], ', '.join(command['args_name'])))

break

else:

break





# create discord client

client = discord.Client()

token = 'NDI3MjIyOTExMjg0NDEyNDQ2.DZhfMA.h3sx9iq6vO_ROYtlxHHTSZJz0xs'



# create the CommandHandler object and pass it the client

ch = CommandHandler(client)



## start commands command

def commands_command(message, client, args):

try:

count = 1

coms = '**Commands List**\n'

for command in ch.commands:

coms += '{}.) {} : {}\n'.format(count, command['trigger'], command['description'])

count += 1

return coms

except Exception as e:

print(e)

ch.add_command({

'trigger': '!commands',

'function': commands_command,

'args_num': 0,

'args_name': [],

'description': 'Prints a list of all the commands!'

})

## end commands command



## start ip commad

def ip_command(message, client, args):

try:

req = requests.get('http://ip-api.com/json/{}'.format(args[0]))

resp = json.loads(req.content.decode())

if req.status_code == 200:

if resp['status'] == 'success':

template = '**{}**\n**IP: **{}\n**City: **{}\n**State: **{}\n**Country: **{}\n**Latitude: **{}\n**Longitude: **{}\n**ISP: **{}'

out = template.format(args[0], resp['query'], resp['city'], resp['regionName'], resp['country'], resp['lat'], resp['lon'], resp['isp'])

return out

elif resp['status'] == 'fail':

return 'API Request Failed'

else:

return 'HTTP Request Failed: Error {}'.format(req.status_code)

except Exception as e:

print(e)

ch.add_command({

'trigger': '!ip',

'function': ip_command,

'args_num': 1,

'args_name': ['IP\Domain'],

'description': 'Prints information about provided IP/Domain!'

})

## end ip command



# bot is ready

@client.event

async def on_ready():

try:

print(client.user.name)

print(client.user.id)

except Exception as e:

print(e)



# on new message

@client.event

async def on_message(message):

# if the message is from the bot itself ignore it

if message.author == client.user:

pass

else:

# try to evaluate with the command handler

try:

await ch.command_handler(message)

# message doesn't contain a command trigger

except TypeError as e:

pass

# generic python error

except Exception as e:

print(e)



# start bot

client.run('NjI2NTQxNTU4ODQ1NjY5Mzg4.XY0atQ.i5F30aG-S38pf4o9-tQt5lTYh5w')

Client' object has no attribute 'send_message

that's the only error message ill provide some screenshots

starting the bot
http://prntscr.com/pbj2o7

entering the !commands command
http://prntscr.com/pbj2td

After entering command and bot not forfilling command I get this error
http://prntscr.com/pbj2zb
Reply
#2
Code in order froom Pycharm
SS 1
http://prntscr.com/pbj4xd

SS2
http://prntscr.com/pbj54y

SS3
http://prntscr.com/pbj5b4

SS4
http://prntscr.com/pbj5g3

SS5
http://prntscr.com/pbj5ic

SS6
http://prntscr.com/pbj5l3

SS7
http://prntscr.com/pbj5pm

Hope this helps ty <3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,533 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,668 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 687 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,216 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,303 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,780 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,352 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,000 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,202 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,721 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020