#bot.command() AttributeError: 'Client' object has no attribute 'command' - python

from discord.ext import commands
client = discord.Client(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
client.run("NzA2MzE0MTc3NjQzNDEzNTY1.Xq4cYw.A-6MruzAgtLC1maW4VVIB2HlFM4")
Why doesn't it work?
I tried almost every common fix but didn't get any positive results

You need to use the Bot class from discord.ext.commands
from discord.ext import commands
client = commands.Bot(command_prefix='x!')
#client.event
async def on_ready():
print("Successfully booted the bot up!")
#client.event
async def on_message(message):
if message.content.find("minecraft") != -1:
await message.channel.send('#Kerina#4436 ajde majnkraft jebemlite')
await client.process_commands(message)
#client.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
I also added the await client.process_commands(message) line so that your commands are processed.

The sample code at https://discordpy.readthedocs.io/en/latest/ext/commands/extensions.html#id1 indicates that the correct code would look like this:
#commands.command()
async def nwordpass(ctx):
await ctx.send('Proof of you having the nword pass: https://lh3.googleusercontent.com/1HBSAiHdRdM1UVJJZlOUnMkihkiMOPPYSMTjI5WzHuvDVIBztueZR83rkUiHwIJvrfU')
That is, the command() decorator is defined on the commands module, not on the Client instance objects.

Related

Discord Bot not responding to commands... (Using Pycord)

Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong!" when someone types "!ping" but when I try to run the command, nothing happens.. I've tried using other commands too but they all result in errors.. I'm a beginner to using Python, so please, can someone tell me what's wrong with my code, and how I can fix it?
In short, my bot doesn't respond back when I type the command I made for it to do so.
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
Instead of #bot.command you should use #client.command.
Also no need for the from discord.ext.commands import Bot import
So your new code would be
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
You could just do inside on_message code block
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')

Python Bot command not working but event is

Commands not working but events are tried overriding my on_message but that didn't work. When I comment out the second client.event and down client.command works. Any idea of what I could be doing wrong? am I missing something?
import discord
from discord.ext import commands
import random
import time
from datetime import date
client = commands.Bot(command_prefix = '.')
#client = discord.Client()
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
#client.command()
async def clr(ctx, amount=5):
await ctx.channel.purge(limit=amount)
#client.command(aliases =['should', 'will'])
async def _8ball(ctx):
responses =['As i see it, yes.',
'Ask again later.',
'Better not tell you now.',
"Don't count on it",
'Yes!']
await ctx.send(random.choice(responses))
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
Based on the docs (the ones mentioned by moinierer3000 in the comments) as well as other questions on stack (listed below), on_message will stop your commands from working if you do not process the commands.
#client.event()
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hello'):
await message.channel.send('Hello how are you?')
await client.process_commands(message)
Other questions like this:
discord.py #bot.command() not running
Discord.py Commands not working because of a on_message event
Prefixed and non prefix commands are not working together on python discord bot

command cant working on discord bot at new version

import discord
import random
from discord.ext import commands
client = commands.Bot(command_prefix = '!')
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
#client.event
async def on_ready():
deneme=["Besim Tibuk Online!","Bu Kitap benim","Satacağız!","Özelleştireceğiz!"]
await client.get_channel(#########).send(random.choice(deneme))
#client.command()
async def ping(ctx):
await ctx.send("Pong")
"client.command()" side cant working.When ı write "ping" I cant get response
Its because of your on_message, you arent processing commands. To process commands, do await client.process_commands(message). Here is the reworked code.
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
await client.process_commands(message)

discord.py ctx commands do not return anything

I'm trying to use bot commands in discord so I can start using the bot prefix. I tried using ctx, but from the code that I have written, when I use .ping, it returns nothing. Anyone got any idea why?
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix= ".")
#client.event
async def on_ready():
print("Logged in") #login message
#bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
#client.event
async def on_message(message):
if message.author == client.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
client.run('#bot token here')
There are a lot of things wrong in your code:
Use discord.Client or commands.Bot not both
You didn't enable intents, so message.author is None, here's how:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(..., intents=intents)
Also make sure to enable privileged intents in the developer portal
You're running the client, not the bot, if you want the commands to work run the bot and get rid of client
Change the decorator of the on_message and on_ready events and add process_commands at the end of on_message:
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
Here's your fixed code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix= ".", intents=intents)
#bot.event
async def on_ready():
print("Logged in")
#bot.command()
async def ping(ctx):
await ctx.send("test")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
There are some bugs in your code that I have found.
Don’t use both discord.Client() and commands.Bot()
You need to enable intents. Go to the discord developers page then to your bot and enable both intents. Quick Link To The Developer Page On Discord
Then write this code:
intents = discord.Intents.all()
bot = commands.Bot(commands_prefix=“.”, intents=intents)
Make sure you are not using any client here.
There are even more stuff needed to fix. Since now we are using bot, change everything saying client to bot. You also have to add await bot.process_commands(message) to the end of the on_message function. Also change ctx.channel.send to just ctx.send
#bot.event
async def on_ready():
print("Logged in") #login message
#bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
#bot.event
async def on_message(message):
if message.author == bot.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
Just use simple ctx.send, it's common for commands,
#bot.command()
async def ping(ctx):
await ctx.send("test")

My python code refuses to run for this discord bot I am making

Nothing works or runs
The client simply will not run and I get a blank console and I can not get the bot to respond in discord
I have ran the bot and it refuses to work
It has stopped reponding to my text input in discord chat and will not respond to it
import discord
from discord.ext import commands
import asyncio
from itertools import cycle
token = 'Dummy Token ACGT'
status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix = ",")
#client.event
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def change_status():
await client.wait_until_ready()
statuses = cycle(status)
while not client.is_closed:
current_status = next(statuses)
await client.change_presence(activity=discord.Game(name=current_status))
await asyncio.sleep(2)
#client.event
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello {0.author.mention}'.format(message))
if message.content.startswith('!who is online'):
await message.channel.send('Not Sure {0.author.mention}'.format(message))
if message.content.endswith('whammy'):
await message.channel.send('response {0.author.mention}'.format(message))
client.loop.create_task(change_status())
client.run(token)
No output or error messages
You are using self as a variable in your functions. This is typically used when creating classes, which you are not doing.
Remove self from your functions and it works fine.
Also, I advise that you regenerate your bot token. Since you have included it in your question, anyone can use it to hijack your bot. Regenerate the token and do not share it with anyone.
import discord
from discord.ext import commands
import asyncio
from itertools import cycle
token = 'token'
status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix = ",")
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
async def change_status():
await client.wait_until_ready()
statuses = cycle(status)
while not client.is_closed:
current_status = next(statuses)
await client.change_presence(activity=discord.Game(name=current_status))
await asyncio.sleep(2)
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author.id == client.user.id:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello {0.author.mention}'.format(message))
if message.content.startswith('!who is online'):
await message.channel.send('Not Sure {0.author.mention}'.format(message))
if message.content.endswith('whammy'):
await message.channel.send('response {0.author.mention}'.format(message))
client.loop.create_task(change_status())
client.run(token)
I removed all selfs from funcs and used client where self was used
Hope this helps
import discord
import asyncio
from discord.ext import commands
from itertools import cycle
from toke import token
status = ['EpicGamerTime', 'Help', 'GamerStuff']
client = commands.Bot(command_prefix=",")
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
async def change_status():
await client.wait_until_ready()
statuses = cycle(status)
while not client.is_closed:
current_status = next(statuses)
await client.change_presence(activity=discord.Game(name=current_status))
await asyncio.sleep(2)
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author.id == client.user.id:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello {0.author.mention}'.format(message))
if message.content.startswith('!who is online'):
await message.channel.send('Not Sure {0.author.mention}'.format(message))
if message.content.endswith('whammy'):
await message.channel.send('response {0.author.mention}'.format(message))
client.loop.create_task(change_status())
client.run(token)
BTW, pls don't post your token

Categories

Resources