Discord.py change_nickname error too many arguments - python

I am currently trying to make a command with discord.py to reset all user nicknames on a server (!!!mreset). However I'm getting a type error which tells me 4 arguments were given.
Code:
#client.event
async def on_message(message):
  if message.author == client.user:
     return
  if message.content.startswith('!!!mreset'):
     serverid = message.server.id
     x = message.server.members
     for member in x:
       await client.change_nickname(serverid, member.name, None)
Error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Saber\PycharmProjects\my_bot\venv\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Saber/PycharmProjects/my_bot/my_bot.py", line 32, in on_message
await client.change_nickname(serverid, member.name, None)
TypeError: change_nickname() takes 3 positional arguments but 4 were given

You don't need to pass the serverid, or a Server object to change_nickname. Member objects have a server associated with them, so the command can get the appropriate Server from the Member object. You should also be passing a Member object and not the Member.name attribute.
if message.content.startswith('!!!mreset'):
for member in message.server.members:
        await client.change_nickname(member, None)

Related

AttributeError: 'Button' object has no attribute 'get' button error [discord.py]

I would like to make a test button but it writes me an error shows which I did not find anywhere on the internet
code:
#slash.slash(
name="ButtonTest",
description='Test a button function (this not work)',
guild_ids=[894190915097337876]
)
async def _ButtonTest(ctx: SlashContext):
await ctx.send("test", components = [Button(style=ButtonStyle.blue, label='sus')])
instruction=await bot.wait_for("button_click", check=lambda i: i.component.label.startswith("Click"))
await instruction.send(content='Test button function succeeded!!')
error:
An exception has occurred while executing command `buttontest`:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/client.py", line 1352, in invoke_command
await func.invoke(ctx, **args)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/model.py", line 210, in invoke
return await self.func(*args, **kwargs)
File "main.py", line 42, in _ButtonTest
await ctx.send("test", components = [Button(style=ButtonStyle.blue, label='sus')])
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/context.py", line 215, in send
if components and not all(comp.get("type") == 1 for comp in components):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord_slash/context.py", line 215, in <genexpr>
if components and not all(comp.get("type") == 1 for comp in components):
AttributeError: 'Button' object has no attribute 'get'
I don't know if you solved the problem or not. Maybe it will help someone.
This error occurs due to slash commands, or rather local ctx. The problem is solved by creating an async def function in which messages will be sent. Just do not pass ctx to this function, pass only the necessary parameters, if you need to send a message, pass ctx.channel (then use await channel.send()), if you need a user, then ctx.author and ect
Corrected code:
#slash.slash(
name="ButtonTest",
description='Test a button function (this not work)',
guild_ids=[894190915097337876]
)
async def _ButtonTest(ctx: SlashContext):
for_Button_Test(ctx.channel)
async def for_Button_Test(ch):
await ch.send("test", components=[Button(style=ButtonStyle.blue, label='sus')])
instruction = await bot.wait_for("button_click", check=lambda i: i.component.label.startswith("Click"))
await instruction.send(content='Test button function succeeded!!')

Add Role on Server

I intend, if you send the bot a certain DM that it then gives a role on a certain server.
This is my code what I tried:
#bot.event
async def on_message(message):
if message.author.bot:
return
elif message.content.startswith("++neunundvierzig++"):
role = message.author.guild.get_role(861543456908640298)
guild = bot.get_guild(816021180435529758)
await message.channel.send("Erfolgreich Zerifiziert!")
await member.add_roles(role)
It says in the log:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\aaa12\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "f:\HTML CSS JS Py Projekte\Python\bot_check\bot.py", line 36, in on_message
role = message.author.guild.get_role(861543456908640298)
AttributeError: 'User' object has no attribute 'guild'
I think you defined role wrong. Your code says that message.author.guild doesn't exist, so you should try message.guild.
Like this:
role = message.guild.get_role(861543456908640298)
Aside from that, you don't defined member. Just rename it to await message.author.add_roles(role) instead of await member.add_roles(role)
Sources
Discord Messages

on_raw_reaction_add(payload) troubleshooting

Hey i am developing a Discord Bot with Python.
Here is the documentation: https://discordpy.readthedocs.io/en/latest/index.html
This program should give a user a role if he or she reacts to a message
I tried to define the ctx in the function itself
#client.event
async def on_raw_reaction_add(payload):
member = discord.utils.get(client.get_all_members())
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
check_in = ["612637944544624690"]
if str(payload.channel_id) in check_in:
await client.add_roles(member, role, reason="check-in")
That is the error without ctx in the function itself:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "C:/Users/kaitr/PycharmProjects/Discord Tutorial/bot.py", line 39, in on_raw_reaction_add
role = discord.utils.find(lambda r: r.name == "check-in", ctx.guild.roles)
NameError: name 'ctx' is not defined
That is the error with ctx in the function itself:
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\kaitr\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
TypeError: on_raw_reaction_add() missing 1 required positional argument: 'ctx'
You don't have access to an invocation context because this isn't a command. You can only use the attributes of the payload, a RawReactionActionEvent object. This includes a guild_id attribute that you can use to get the guild:
#client.event
async def on_raw_reaction_add(payload):
if payload.guild_id is None:
return # Reaction is on a private message
guild = client.get_guild(payload.guild_id)
role = discord.utils.get(guild.roles, name="check-in")
member = guild.get_member(payload.user_id)
if str(payload.channel_id) in check_in:
await member.add_roles(role, reason="check-in")

TypeError: send() takes from 1 to 2 positional arguments but 3 were given

Here is part of code
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
# The command /patch return a link with the latest patch note
if message.content.startswith('/patch'):
await message.channel.send(message.channel, 'Last patchnotes: https://www.epicgames.com/fortnite/en/news')
# The command /rank return attribute a rank according to the K/D of the user
used discord.py
when you type /patch
here's what the console shows
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\FeNka\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py", line 227, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 107, in on_message
await message.channel.send(message.channel, 'Last patchnotes: https://www.epicgames.com/fortnite/en/news')
TypeError: send() takes from 1 to 2 positional arguments but 3 were given
What could be wrong?
Your call should be changed to
await message.channel.send('Last patchnotes: https://www.epicgames.com/fortnite/en/news')
send is a function of the message.channel class, and thus has access to self. Its call probably looks something like:
def send(self, message):
#does things
self is implicit here, you don't send it, and that's why it looks like 2 args were passed when 3 actually were sent

get_member_named() missing 1 required positional argument: 'name'

#my_bot.event
async def on_message(message):
if message.content.startswith('!test'):
server = discord.Server
await my_bot.send_message(server.get_member_named('mystery#5137'), 'Hello')
Why didn't work?
Error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Roman-PC\AppData\Local\Programs\Python\Python36-32\lib\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Roman-PC/Desktop/t1.py", line 11, in on_message
await my_bot.send_message(server.get_member_named('name'), 'Hello')
TypeError: get_member_named() missing 1 required positional argument: 'name'
You shouldn't create a discord.Server, rather you should get the server from the message object as (see the docs for discord.Message)
s = message.server
then you can
s.get_member_named('mystery#5137')
You need to define the Member with a name.
Instead of putting:
server = discord.Server
await my_bot.send_message(server.get_member_named('mystery#5137'), 'Hello')
You need:
server = message.server
await my_bot.send_message(server.get_member_named(name='mystery#5137'), 'Hello')

Categories

Resources