AttributeError: 'Client' object has no attribute 'command' - python-3.x

I have added this code:
#client.command(pass_context=True)
async def name(ctx):
username = ctx.message.author.display_name
On to the end of my line of code, and I get this error when trying to get the bot online:
AttributeError: 'Client' object has no attribute 'command'

You need to use discord.ext.commands.Bot instead of discord.Client. Bot is a subclass of Client, so you can also use all of the Client functionality with a Bot instance
from discord.ext.commands import Bot
bot = Bot("!")
#bot.command()
async def test(ctx):
await ctx.send("Command executed")
await bot.run("TOKEN")

client = discord.Client(intents=intents )
Check this :)
client=commands.Bot(command_prefix=".")
This resolves my isssue :)

Related

Pycord mute command doesn't throw error but doesn't add role

I am using pycord for this so I can use slash commands.
Here is my code:
import discord, os
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv(f'{os.getcwd()}/token.env')
bot = commands.Bot()
token = str(os.getenv('TOKEN'))
#bot.slash_command(description = 'mutes member of choice')
#commands.has_permissions(administrator = True)
async def mute(ctx, member: discord.Member):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name="Muted")
if not mutedRole:
mutedRole = await guild.create_role(name="Muted")
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=False)
await member.add_roles(mutedRole)
await ctx.send(f"Muted {member.mention}")
#mute.error
async def permserror(ctx, error):
if isinstance(error, commands.MissingPermissions):
ctx.respond('Sorry! You dont have permission.')
#bot.event
async def on_ready():
print(f'Client ready as {bot.user}')
bot.run(token)
This isn't my full code, but the other commands don't seem necessary for my problem.
Anyway, when I try to use this command, it doesn't throw an error in the console, but the application doesn't respond and it doesn't add the role.
I put a print statement between each line of code, and it seems to stop just before it adds the role.
How can I fix this?
Update: My previous code did not run so I have updated it.
This doesn't actually have to do with the code of the program. All you have to do is move the bot role to the top and it'll work. The error is 403 Forbidden (error code: 50013): Missing Permissions. Also, you have to reinvite the bot and add applications.commands if you haven't already.

Discord.py API get user data from IP

I've been looking into the Discord API source code and I was wondering if you could get a user's data. Not just returning their username. There is nothing that I could find in the API's docks.
This is the furthest I got:
data = await discord.client.Client.fetch_user_profile(discord.client.Client.fetch_user_profile, id)
But I keep on getting the error:
CommandInvokeError: Command raised an exception: AttributeError: 'function' object has no attribute '_connection'
Caused by a what I think may be a function and a variable having the same name. Are there any ways to fix this, or any other methods to get a users data that works. Preferably without having to change the source code. Thanks for any help in advance.
And just in case you need it, here is the entirety of my code:
#import libarys
import discord
from discord.ext import commands
import os
#setup bot
bot = commands.Bot(command_prefix='&', owner_id=MyID, case_insensitive=True)
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='for &help'))
print(f'\n\nLogged in as: {bot.user} - {bot.user.id}\nVersion: {discord.__version__}\n')
#find user data command
#bot.command()
async def find(ctx, id: int):
member = await ctx.guild.fetch_member(id)
data = await discord.client.Client.fetch_user_profile(discord.client.Client.fetch_user_profile, id)
print(member, data)
#connect bot
bot.run(os.environ.get('TOKEN'), bot=True, reconnect=True)
EDIT: This code is just a mock up to get a working concept, I don't care to make it more refined yet.
This is a really bad code, I don't even know how you came up with that code. You're not supposed to refer to the class itself but to the instance, your code fixed
#bot.command()
async def find(ctx, id: int):
member = await ctx.guild.fetch_member(id)
data = await bot.fetch_user_profile(id)
print(member, data)
Also note that the fetch_user_profile method it's deprecated since version 1.7 and bot accounts cannot use this endpoint
Reference:
Client.fetch_user_profile

on_member_join can't print a message

I'm doing a Discord bot with Python 3.8 and I have a problem with my event on_member_join().
#bot.event
async def on_member_join(member):
embed = discord.Embed(
title=f"that doesn't function")
channel = discord.utils.get(member.guild.channels, name='WELCOME_CHANNEL_NAME')
await member.send('Hello')
await channel.send(embed=embed)
member.send sends a private message with hello but channel.send sends this error await channel.send(embed=embed) AttributeError: 'NoneType' object has no attribute 'send'
I really don't know how to do,
Thanks for your help!
Because you probably don't have a channel named WELCOME_CHANNEL_NAME. That's why channel returns None. You need to type the real welcome channel name instead of WELCOME_CHANNEL_NAME in channel = discord.utils.get(member.guild.channels, name='WELCOME_CHANNEL_NAME').

How do I add a reaction emoji to the message the bot sends after doing the user's command?

I get this error: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'message'--when trying to do await self.client.message.add_reaction(emoji).
I tried changing it to await ctx.message.add_reaction(emoji), and I realized that it reacted to the command the user sent rather than the bot's new message.
import discord
from discord.ext import commands
class MovieNight(commands.Cog):
"""Polls for Movie Night."""
def __init__(self, client):
self.client = client
#commands.command(aliases=['m'])
async def movie(self, ctx, year, *movie):
movie_title = ' '.join(movie[:-1])
await ctx.send(f"`{year}` - `{movie_title}` | **{movie[-1]}**")
emoji = '👎'
await self.client.message.add_reaction(emoji)
def setup(client):
client.add_cog(MovieNight(client))
self.client doesn't know about the message, the is stored as part of the invocation context:
await ctx.message.add_reaction(emoji)
Adding onto Patrick's answer here from what you said in the comment.
await self.client.message.add_reaction(emoji) won't work because the bot doesn't know what message you're referring to, and client doesn't have an attribute called message.
Adding reactions requires a discord.Message object, which in your case can be either the command that the user executed (e.g. !movie 2020 movie title) which you can retrieve via ctx.message, or a message which you're making the bot send.
If you wanted to get the message object from the message that the bot sent, you can assign it to a variable:
msg = await ctx.send(f"`{year}` - `{movie_title}` | **{movie[-1]}**")
And this allows you to then add a reaction to it or access any other message attributes you would like:
emoji = '👎'
await msg.add_reaction(emoji)
References:
discord.Message
Message.add_reaction()
TextChannel.send() - Here you can see it returns the message that was sent

Python - How do I make a join command

I'm trying to create a ".join" command for my bot but everything I've tried didn't work & I tried a ton of things
Here's something I've tried, I tried it in a few other ways too:
#client.command(pass_context=True)
async def join(ctx):
channel=ctx.message.author.voice.VoiceChannel
await client.join_VoiceChannel(channel)
It gives this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'VoiceChannel'
join_voice_channel
works for older versions of discord.py
This code block should work:
#client.command(pass_context=True)
async def join(ctx):
message = ctx.message
channel_id = ctx.message.author.voice.channel.id
channel = client.get_channel(channel_id)
await channel.connect()
You use join_voice_channel to make the bot join a voice channel as it says in the Async docs
Try this:
#client.command(pass_context=True)
async def join(ctx):
author=ctx.message.author
await bot.join_voice_channel(author.voice_channel)

Resources