Reputation: 35
I got a question regarding discord.py. I am currently making a mod-mail for my server. The idea for the bot is to create a channel every time it get's a message. This all works. It creates a channel names after the authors name in lower cases and replaces spaces with -. For example BOB IS MAD would become bob-is-mad
.
Now I am working on enhancing the response system. From just mentioning the user you want to send the message to, to the bot getting the member you want to send it to based on the channel you are sending it in. So for example if I would send a message in the bob-is-mad
channel, it would automatically send it to BOB IS MAD. Here is where I am walking into an issue though.
The issue I am walking into is that the bot is looking for lower cases but some Discord users have capital letters in their name. How can I make it so the bot finds the member even though the name could be both lower case and upper case.
Here is my code:
(I know that this isn't in the reply command, I was just using this as a testing command if I could find a way to grab the id of the user)
# this commands is grabs the id of the user the channel is from
@bot.command()
async def id(ctx):
# getting the channels name
channel = ctx.channel
current = str(channel)
guild = ctx.guild
# replacing the - for spaces
user = current.replace("-", " ")
# testing the output
print (user)
# finding the member in the server
member = discord.utils.get(guild.members, name=f"{user}")
# printing if it found the member
print (member)
I tried making it member = discord.utils.get(guild.members, name=f"{user.upper()}")
but this of course didn't work. And I couldn't find any information on this
Upvotes: 0
Views: 1418
Reputation: 15689
Instead of using utils.get
I'd use utils.find
@bot.command()
async def id(ctx):
channel_name = ctx.channel.name
user_name = channel_name.replace("-", " ")
predicate = lambda member: member.name.lower() == user_name
member = discord.utils.find(predicate, ctx.guild.members)
print(member)
Though using member names is a bad idea (multiple members can have the same name), I'd put the member ID in the channel's topic and grab it from there.
Upvotes: 1