Member is a required argument that is missing error - python

Hello so I’m trying to create a code for my bot that can ping a specific member in discord but ran into the issue member is a required argument but is missing. i tried searching it up and tried fixing it but nothing worked. I am very new to python and have no idea how to fix it.
My code for reference
import discord, datetime, time
import os
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
member = 548378867723665409
BOT_PREFIX = ("!")
bot = commands.Bot(command_prefix=BOT_PREFIX)
#bot.command()
async def pong(ctx, member : discord.Member):
await ctx.send('test')
await ctx.send(f"PONG {member}")
#bot.event
async def on_ready():
print ("------------------------------------")
print ("Bot Name: " + bot.user.name)
print ("------------------------------------")
bot.run(os.getenv('TOKEN'))

You are getting that error because you are not passing any member argument. Your command should look like this !pong #member. The member should be mentioned in your message. I noticed that you have initializes a global variable member having the member's ID. If you want to mention that member and not pass a member object as an argument, you'll have to do it like this:
memberID = 548378867723665409
#bot.command()
async def pong(ctx):
member = await bot.fetch_user(memberID)
await ctx.send(f"PONG {member.mention}")

There is a simple way to do this if you just want to ping the author of a message. You will use {message.author.mention}
an example of how this can be used in code is:
if i == "hi":
await message.channel.send(f' Hello! {message.author.mention}')
Note: if you want to ping someone and say more, you will require an f string:
(f'{message.author.mention} hello')

Related

Discord.py switching from client to bot

I'm fairly new at making a discord bot. I have created a method to get a list of all the members in the server the bot is connected to. This method works fine. However, I am also trying to get the bot to accept commands. I am having a lot of trouble with this and I keep seeing that I should use commands.Bot for this instead of Client. I can't figure out how to make my original method work with commands.Bot though. Any help would be appreciated!
import os
import discord
import csv
from collections import defaultdict
from discord.ext import commands
intents = discord.Intents.all()
client = discord.Client(intents=intents)
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!')
#client.event
async def on_message(message):
if message == "!test":
await message.channel.send("you failed the test")
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)
count = 0
for guild in client.guilds:
for member in guild.members:
print(member)
if 'Bots' not in str(member.roles):
print(member.name, ' ')
outP.write(member.name)
outP.write("\n")
count += 1
print('Number of members: ' + str(count))
It is good to use commands.bot instead of Client because it is an extended version, as it inherits all the functionalities from Client.
I see you have tried to migrate from the use of Client to using commands.bot, but have a few things in mind:
You do not want to use both at the same time, so this is wrong:
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix='!')
You should keep only the bot one.
Apart from that, you gotta replace the client from the decorators and the calls inside the function. Your corrected code should look something like this:
import os
import discord
import csv
from collections import defaultdict
from discord.ext import commands
intents = discord.Intents.all()
outP = open("giveawayList.txt", "w")
bot = commands.Bot(command_prefix='!', intents=intents)
#bot.event
async def on_message(message):
if message == "!test":
await message.channel.send("you failed the test")
await bot.process_commands(message)
#bot.event
async def on_ready():
for guild in bot.guilds:
if guild.name == GUILD:
break
print(
f'{bot.user} is connected to the following guild: \n'
f'{guild.name} (id: {guild.id})'
)
count = 0
for guild in bot.guilds:
for member in guild.members:
print(member)
if 'Bots' not in str(member.roles):
print(member.name, ' ')
outP.write(member.name)
outP.write("\n")
count += 1
print('Number of members: ' + str(count))
So, regarding your questions, i am gonna answer them one by one :
1- client and bot are not diffrent at all, that means you shouldn't have any problem using the same functions on both(and neither are better than each other, meaning both will do the same stuff)
2- now to get your bot to receive/accept commands, you can use the await function, which basically waits for a command to be trigerred and will give out the output.
For example :
#Client.command()
async def botping(ctx):
await ctx.send(f"The bot's ping is {round(Client.latency*1000)} ms")
This command here (for example):
We are defining a command called "botping" and we are passing the ctx wich is the context the bot is writing, the you have the await wich will trigger the command when you type "prefix"botping in chat.
Hopefully this helped you a little bit about the concept of taking commands and that there is no diffrence between client and bot
For further information, i suggest that you check out the discord.py documentation:
https://discordpy.readthedocs.io/en/latest/index.html

How to get the ID of any user given the Username in Discord?

import discord
import os
flist = []
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('?ID'):
for i in ID.split()[1].split('#'):
flist.append(i)
print(flist)
print(discord.utils.get(client.get_all_members(), name = flist[0], discriminator = flist[1]).id)
client.run(TOKEN)
I want to have a bot get the id of a use just by entering the the name and the hashtag number but it returns an error that the final line has nonetype. How could I fix this so it shows the id as a string?
First of all let me recommend you to use the discord.ext.commands extension for discord.py, it will greatly help you to make commands.
You just need to get a discord.Member or discord.User in this case. Since these classes have built in converters, you can use them as typehints to create an instance:
from discord.ext import commands
bot = commands.Bot(command_prefix='?', ...)
#bot.command()
async def showid(ctx, member: discord.Member): # a Union could be used for extra 'coverage'
await ctx.send(f'ID: {member.id}')
Using a typehint in this case allows the command user to invoke the command like:
?showid <id>, ?showid <mention>, ?showid <name#tag>, ?showid <nickname> etc with the same result.
If you don't want to use discord.ext.commands, you can use discord.utils.get as well:
client = discord.Client(...)
#client.event
async def on_message(message):
...
if message.content.startswith('?ID'):
member = discord.utils.get(message.guild.members, name=message.content.split()[1])
await message.channel.send(member.id)
...

Discord.py get user returns none

I want ban any member from message with this code;
elif "$ban" in message.content:
msg = message.content
msgsplit = msg.split() #banid[-1] gets last index of message array
banidstr = msgsplit[-1]
banid = int(banidstr)
member = bot.get_user(int(banid))
print(member)
It look like spaghetti but for testing. "print (member)" line returns none but "print (banid)" returns user ID. What can i do?
I think this post might help.
I also recommend you to use discord.ext.commands.Bot(), where making commands is a lot easier:
from discord.ext import commands
bot = commands.Bot(command_prefix='?')
#bot.command()
async def echo(ctx, *, message):
await ctx.send(message)
# Command will be invoked by user like '?echo this is the bot speaking'
# And the bot will then send 'this is the bot speaking'
Yes guys it worked;
async def ceza(ctx, *, message):
cezaname=bot.get_user(int(message))
print(cezaname)
This, printing username by id properly.

How do i put more than one command leading to the same response in discord.py?

#client.event
async def on_message(message):
if message.content == "fase":
channel = (mychannel)
await message.channel.send("Fase 1")
I'm trying to make the message.content detect multiple words and send the same message.channel.send
I tried
if message.content.lower() == "fase", "estagio", "missao", "sala":
if message.content.lower() == ("fase", "estagio", "missao", "sala"):
if message.content.lower() == "fase" or "estagio" or "missao" or "sala":
if message.content.lower() == ("fase" or "estagio" or "missao" or "sala"):
I read this post: How do I allow for multiple possible responses in a discord.py command?
That is the same exact problem but in his case it was the CaseSensitiveProblem that i already fixed in my code
And the second code for multiple words was:
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#bot.command(aliases=['info', 'stats', 'status'])
async def about(self):
# your code here
And i did it and got a lot of errors that made the bot not even run (im using PyCharm with discord.py 1.4.1 and python 3.6):
#import and token things up here
bot = commands.Bot(command_prefix='i.')
#bot.command(aliases=['fase', 'estagio', 'missao', 'sala']) #'#' or 'def' expected
async def flame(self): #Unexpected indent // Unresolved reference 'self'
if message.content(self): #Unresolved reference 'message'
await message.send("Fase 1") #Unresolved reference 'message' // Statement expected, found Py:DEDENT
What can i do to fix it?
Here's how to use the Commands extension:
from discord.ext import commands
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#bot.command(aliases=['info', 'stats', 'status'])
async def about(ctx):
#Your code here
Every commands have the following in common:
They are created using the bot.command() decorator.
By default, the command name is the function name.
The decorator and the function definition must have the same indentation level.
ctx (the fist argument) will be a discord.Context object, which contains a lot of informations (message author, channel, and content, discord server, the command used, the aliase the command was invoked with, ...)
Then, ctx allows you to use some shortcuts:
message.channel.send() becomes ctx.send()
message.author becomes ctx.author
message.channel becomes ctx.channel
Command arguments are also easier to use:
from discord import Member
from discord.ext import commands
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#Command call example: !hello #Mr_Spaar
#Discord.py will transform the mention to a discord.Member object
#bot.command()
async def hello(ctx, member: Member):
await ctx.send(f'{ctx.author.mention} says hello to {member.mention}')
#Command call example: !announce A new version of my bot is available!
#"content" will contain everything after !announce (as a single string)
#bot.command()
async def announce(ctx, *, content):
await ctx.send(f'Announce from {ctx.author.mention}: \n{content}')
#Command call example: !sum 1 2 3 4 5 6
#"numbers" will contain everything after !sum (as a list of strings)
#bot.command()
async def sum(ctx, *numbers):
numbers = [int(x) for x in numbers]
await ctx.send(f'Result: {sum(numbers)}')

TypeError: on_message() missing 1 required positional argument: 'Member' discord.py

I am having a problem with the report command I am working on. Whenever I run the command on my discord server, the error "TypeError: on_message() missing 1 required positional argument: 'Member'" comes up. If anyone knows how to solve this problem can you tell me? Thank you for taking your time to read this.
# bot.py
from discord import Member
import os
import discord
from dotenv import load_dotenv
load_dotenv()
DISCORD_TOKEN = ''
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
#client.event
async def on_message(message, Member):
Member = discord.Member(Member)
if message.author == client.user:
return
if message.content.startswith('$report ', Member.mention):
Sends = 'That user has been reported!'
await message.channel.send(Sends)
client.run(DISCORD_TOKEN)
I can see that your new to discord.py. First of all, the on_message function does not take member. It only takes message. When you add an event to your code you always want to check the Event Reference and check the paramters for that event. You cannot add or remove paramaters. For this case your code would start like this
#client.event
async def on_message(message)
To get the member you would use:
member = message.author
Also I recommend you use commands
from discord.ext import commands
It will be a lot easier and you can add your own paramaters. I've made an example of what you're doing with your on_message function in a command
#client.command
async def report(ctx, member : discord.Member, reason=None)
await ctx.send(f"Reported {member.mention} for {reason}")
Also remember if you have an on_message function your commands wont work unless you use bot.process_commands at the end. Read the FAQ on how to do that. Heres the link:
Why does on_message make my commands stop working?
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Categories

Resources