I'm trying to build a bot that changes the name of a channel when someone joins. Here's my code:
#client.event
async def on_member_join(guild):
await update_channel(guild)
new_name = f"{member_count(guild)}"
await channelid.edit(name=new_name)
channelid = *redacted*
def update_channel(guild):
return len(guild.members)
However, I get this error:
AttributeError: 'Member' object has no attribute 'members'
What's the reason for this error, and how can I resolve it?
You have imported the wrong module, You should import the Guild module instead of the Member module, as mentioned in this documentation, members is an attribute from class discord.Guild
See this for more details:
Discord API Reference
Related
pls help
i need help with an error AttributeError: module 'discord.ext.commands' has no attribute 'Client'
If you are trying to make a Discord client object to use the Discord API use:
import discord
client = discord.Client()
Client is not part of discord.ext.commands, therefore your import is resulting in an error.
Currently, this is my code:
import os
import discord
from discord.ext.commands import Bot
bot = Bot(command_prefix='$')
#bot.event
async def on_ready():
print('Logged in as ' f'{bot.user}')
#bot.command()
async def vclist(ctx):
channel = bot.get_channel('944509044599697458')
userids = []
users = channel.members
for member in users:
userids.append(member.id)
print(userids)
When I try to run it I keep getting this Error
AttributeError: 'NoneType' object has no attribute 'members'
and I can't seem to solve it, can anyone help me?
get_channel takes an integer, not a string.
ALSO:
Make sure you have guild intents enabled otherwise get_channel will still return None. If you need help understanding/setting-up intents see the docs here
Im coding a discord bot and I'm trying to make it so when someone mentions someone else, in this case it's midway, it sends a message. But whenever I try it, it comes up with AttributeError: 'NoneType' object has no attribute 'mentioned_in'. This is my code:
import discord
import os
client = discord.Client()
#client.event
async def on_message(message):
midway = client.get_user('midways ID goes here')
if midway.mentioned_in(message):
await message.channel.send('words here')
client.run(os.environ['environ'])
A Message object should go there to the mentioned_in, check out docs:
https://discordpy.readthedocs.io/en/stable/api.html#discord.ClientUser.mentioned_in
(Insert greetings)
I wanted to make the discord bot reply to a message which was replied to in the command message,below is the code so far:
#bot.command(brief="Iri? Bilang Bos!", description="Iri? Bilang Bos!")
async def iri(ctx):
ref = ctx.reference
x=['https://i.imgur.com/8mfw6Nv.jpg', 'https://tenor.com/view/iri-bilang-bos-spell-power-up-skill-gif-17176211', 'https://i.imgur.com/hOvruLZ.jpg']
await ctx.delete()
await ctx.send(random.choice(x), reference=ref)
This raises the exception AttributeError: 'Context' object has no attribute 'reference'
How do i solve this? Thanks.
You have the right idea but didn't look into documentation enough. Usually if you don't know what attribute you need to use it's helpful to search in the documentation.
Context doesn't have attribute reference. That is because reference is an attribute of our message. Luckily for us context (ctx) has a message attribute: ctx.message. We use this to to get the message object which we then use to get the reference attribute: ctx.message.reference.
The same goes for the delete method. Context object doesn't have delete method so we first need to get the message object: ctx.message and then use the delete method of the message object: await ctx.message.delete() (We have to use await because the delete method is asynchronous)
Note: It's usually good practise to give your variables meaningful names. It improves the readability of your code. That's why I changed your x variable to choices
So the end result should be something like this:
#bot.command(brief="Iri? Bilang Bos!", description="Iri? Bilang Bos!")
async def iri(ctx):
ref = ctx.message.reference
choices = ['https://i.imgur.com/8mfw6Nv.jpg', 'https://tenor.com/view/iri-bilang-bos-spell-power-up-skill-gif-17176211', 'https://i.imgur.com/hOvruLZ.jpg']
await ctx.message.delete()
await ctx.send(random.choice(choices), reference = ref)
Hopefully this answers your question :)
I have been trying to create a Discord command that adds a role based of one that is already created and am not that great at python, whenever i try to run
from discord.utils import get
from discord.ext.commands import bot
from discord.ext import commands
import discord
#client.command(pass_context = True)
async def pine(ctx):
role = get(member.guild.roles, name="bot")
await member.add_roles(role)
await ctx.channel.purge(1)
but always get the error "NameError: name 'member' is not defined"
i have tried changing member to user and i get name 'user' is not defined.
You are using the variable member and it is not defined or imported anywhere.
You don't define who is member.
async def xyz(ctx, member:discord.Member):
This command is be like: I give a rank, but I don't know who, maybe me, maybe not.