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
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