NameError: name 'member' is not defined - python

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.

Related

. Why does it give me a "TypeError: 'NoneType' object" error? Code's in python

import os
from discord import FFmpegPCMAudio
from discord.ext.commands import Bot
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('OTY2MDQ5MjA1ODg4MDk0MjY4.Yl8Fbw.k3ftJGOLznBIFmdnaFCNd73qaEk')
PREFIX = os.getenv('!')
client = Bot(command_prefix = list(PREFIX))
#client.event
async def on_ready():
print('Nichijou Shuffle Bot Ready')
#client.command(aliases=['p', 'play'])
async def play(ctx, url: str = 'https://node-33.zeno.fm/tncreqmcwnhvv?zs=tdK49JAMSyu6lo3su_Wf-A&rj-tok=AAABgEq0dCAA2dBg3bs1EtEs7Q&rj-ttl'):
channel = ctx.message.author.voice.channel
global player
try:
player = await channel.connect()
except:
pass
player.play(FFmpegPCMAudio('https://node-33.zeno.fm/tncreqmcwnhvv?zs=tdK49JAMSyu6lo3su_Wf-A&rj-tok=AAABgEq0dCAA2dBg3bs1EtEs7Q&rj-ttl'))
#client.command(aliases=['s', 'stop'])
async def stop(ctx):
player.stop()
client.run(TOKEN)
I'm trying out this discord bot that plays music through a stream link. The error is happening in line 11. Credit to davestsomewhere for the code. I didn't modify the code at all but it keeps showing me the "TypeError: 'NoneType' object is not iterable" error. Don't know how to code but I think this is the only place where I can get an answer.
It comes from the PREFIX that is equal to None. Thus the problem lies at the initialization of the PREFIX.
os.gentenv() returns the value of the environment variable key if it exists otherwise returns the default value. I don't know what you are trying to achieve with os.getenv('!'), but either the value associated with this key is None, either the key doesn't exist and the default value is in your case set to None.
the issue can be replicated in this minimal example:
import os
PREFIX = os.getenv('!')
list(PREFIX)
The environment variable ! is not set, PREFIX then is None and making a list out of it is not possible. I'm not sure what should be in the environment variable and ! seems like a uncommon name to me.

Error: 'Bot' object has no attribute 'get_user_info'

I'm currently programming my own discord bot.
Now i'm trying, to do that, when the bot startet it sends a dm to me.
This is my code
#bot.event
async def on_ready():
owner = await bot.get_user_info(my Userid)
await bot.send_message(owner, "Ready", tts=True)
but i'm always getting the error:
AttributeError: 'Bot' object has no attribute 'get_user_info'
and i have done some research but i found literally nothing
i hope you guys can help me:)
i appreciate it
get_user_info is not a valid attribute of Bot. The correct use would be get_user in order to get a User from a valid user ID.
Also, in order to send a message, you need to have a message channel object. Since you're working with DMs, you'll need to create one using the User.create_dm() function discord.py offers. Afterwhich you can then send a message.
A lot of what I referenced can be found in the discord.py documentation.
Below is how I solved this issue:
from discord.ext import commands
import discord
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="$", intents = intents)
#bot.event
async def on_ready():
owner = bot.get_user(my Userid) # Assuming 'my Userid' is an integer based user id like 278688959905660928
dm_channel = await owner.create_dm()
await dm_channel.send("Ready", tts=True)

How can you call a function in discord.py

I am new to discord.py and I am stuck on this error. I am trying to call the command removerole later in the code.
This first part works fine, I can type in discord chat:
.removerole admin Rythm
And it works, but I get an error in the second part:
'str' object has no attribute 'remove_roles'
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.command()
async def removerole(ctx, role: discord.Role, member: discord.Member):
await member.remove_roles(role)
await ctx.send(f'Successfully removed {role.mention} from {member.mention}')
I want to type .removeRythm in chat and the bot would remove the admin role from Rythm.
#client.command()
async def removeRythm():
await removerole(".remove", "admin", "Rythm")
Does anybody know how to do this if it even is possible to do?
Thanks MB
You are passing into the removerole method 3 strings. ".remove", "admin", "Rythm". "Rythm" is not a discord object but a String. Annotations in Python have no meaning and the compiler totally ignores them. So highlighting that member is of type discord.Member does not actaully change anything, but only confuses you.
Instead you should pass in a discord user object into the method.
I changed like so and it now works
#client.command()
async def removeRythm():
await removerole(ctx, ctx.message.guild.get_role(779716603448787004), ctx.message.guild.get_member(235088799074484224))
Thanks for the help.

How would I detect user activities? | discord.py

I'm trying to make a bot that when a command is entered it detects a users activities. I wrote some code, but the response from the bot I get is not what I wanted. That's my code:
from discord import Member
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command()
async def status(ctx):
await ctx.send(Member.activities)
bot.run('token')
And this is what I get in response:
<member 'activities' of 'Member' objects>
How can I fix this? Would anyone help me?
It seems you are new to python. Python is an object oriented programming language, means that you need to make the difference between a class and an instance.
In your case, you are fetching the class attribute, though you want the instance attribute.
What you wanted to do:
#bot.command
async def status(ctx):
await ctx.send(ctx.author.activities)
Though, that would send a python-formatted list, so that's still not what you want.
What I guess you want to do:
#bot.command
async def status(ctx):
await ctx.send(ctx.author.activities[0].name)
Please note, you need more code as this command like that would raise an error if the member doesn't have any activity.

Discord.py AttributeError: "User" object has no attribute "Guild"

I am working on a custom discord bot, mostly for fun. I found another discord bot from another server, named Kurisu, which is completely custom and open source. here is the github: https://github.com/nh-server/Kurisu. In mod.py (https://github.com/nh-server/Kurisu/blob/port/cogs/mod.py), on line 432, there is a function that basically gives a user the No-Help role (in the server this restricts access to specific channels). I am trying to make something similar, where having the No-Voice role restricts your acess to voice channels. I am using some of their code, namely in the parameters of the function. I am doing async def takevoice(ctx, member: FetchMember): as the function. the full function is here:
#bot.command(name="takevoice", pass_context=True, description="Removes access to voice channels. Admin only.")
#commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def takevoice(ctx, member: FetchMember):
role = get(member.guild.roles, name="No-Voice")
await member.add_roles(role)
other functions in my program work perfectly, but whenever I try and run this function with another user as the target, it doesn't work and gives me a traceback error. the end of it is: AttributeError: "user" object has no attribute "guild". I have looked everywhere for an answer, but can't find one. any help would be much appreciated.
thanks!
the full code for my bot is here: (I am using a couple other files, all of which can be found in the utils folder of the Kurisu github. namely, I am using checks.py, converters.py, database.py, manager.py, and utils.py. I am also using a file called keep_alive.py because i am running this on repl.it.)
import asyncio
import aiohttp
import json
import keep_alive
from discord import Game
from discord.ext.commands import Bot
import datetime
import re
import time
from subprocess import call
import discord
from discord.ext import commands
from checks import is_staff, check_staff_id, check_bot_or_staff
from database import DatabaseCog
from converters import SafeMember, FetchMember
import utils
from discord.ext import commands
from discord.utils import get
TOKEN = "" # Get at discordapp.com/developers/applications/me
bot=commands.Bot(command_prefix=".")
client=discord.Client()
#bot.command(name="hi")
async def hi(ctx):
await ctx.channel.send("hello")
#bot.command (name="elsewhere", description="Gives acess to the #elsewhere channel")
async def elsewhere(ctx):
role = discord.utils.get(ctx.guild.roles, name="Elsewhere")
user = ctx.message.author
if role in user.roles:
await user.remove_roles(role)
if role not in user.roles:
await user.add_roles(role)
That's because a User object cannot have a specific guild - a user means someone that is no longer in the guild. Change async def takevoice(ctx, member: FetchMember): to async def takevoice(ctx, member: discordMember): and the issue will be fixed. That way, you will get a Member instead of a User object, and there would be a specific guild. However (I don't know whether this matters) you can no longer perform this action on someone who is not in the guild.
Changing member: FetchMember to member: discord.Member solved it. the .Member needs to be capitalized.

Categories