Get message content from reply in DM - python

I started making a little discord bot to give a key to people who request it. However, requirements changed and now I need to get a valid email from people who want one. I'm not sure how to get a reply in a DM.
I saw Discord.py get message from DM
But I don't really have client.get_user_info() or something?
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context =True, no_pm=True, name='key', help="I give you a key. you're welcome", )
async def key_giver(ctx):
commandTime = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
##check if you're PMing bot
elif isinstance (ctx.channel, discord.DMChannel):
print(ctx.message)
await ctx.author.send(keyArray[0])
else:
response = "Hello my friend. I give you a key for a game. This is my purpose. Please enter your email!"
##Send key via PM
await ctx.author.send(response)
Printing ctx.message is
<Message id=620538695124254720 channel=<DMChannel id=614377525417738240 recipient=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>> type=<MessageType.default: 0> author=<User id=605223566371192852 name='aon' discriminator='5906' bot=False>>
I'm really not sure. I'm just stupid, please don't yell at me.

You can use Client.wait_for to wait for the message and use its results. You already have the User object (ctx.author), so you can get the channel to listen for from User.dm_channel. I'm re-using the message_check function from this previous answer to generate the check
# Send them a message in a DM
await ctx.author.send("Please give an email address")
# Wait for a response
response = await bot.wait_for('message', check=message_check(channel=ctx.author.dm_channel))

Related

How to check the reply to a message guilded.py

I'm making a command that requires checking what the user is going to reply with to a bot's message. Here's some sample code:
async def create_channel(message):
await message.reply("Reply to this with the channel name that you want to create")
How can I get the user's reply to this message? Have done the research, but didn't find it.

Welcome message not being sent in discord.py

So I made a new server and made a custom bot for it recently, but the welcome message doesn't really seem to work... It is also supposed to give a particular role to the member who joined but I removed that code and the current code still doesn't work.
Sadly, the command prompt gives no errors. I have no idea what to do now.
Here is the code -
#commands.Cog.listener()
async def on_member_join(self, member):
#GETTING THE GUILD, CHANNEL AND ROLE
channel = self.client.get_channel(828481599057166356)
name = member.name
#CREATING THE WELCOME EMBED
welcomeem = discord.Embed(title = f"Hey there {member.name}!", description = f"Welcome to {chanenl.guild.name}! Have a fun time here in TigerNinja's server!")
welcomeem.add_field(name="1. Rules", value = f"{name}, before you start having fun here, make sure to check <#752474442650878002> and read all rules as they will come in handy in the server!", inline=False)
welcomeem.add_field(name="2. Self-roles", value = f"{name}, be sure to check out <#776293478594379797> and get all roles you want!", inline=False)
welcomeem.add_field(name="2. Help", value = f"{name}, need help with this bot then type `t.help`. If you need help relating to something else, contact the mods via dms, but don't ping them!!", inline=False)
#SENDING THE PING, EMBDE AND ADDING ROLE
await channel.send(f"Welcome, {member.mention}!")
await channel.send(embed=welcomeem)
(I am using cogs btw)
Did you set up this command as bot event? Before adding such command you should add
#bot.event (or whatever name you gave to discord.Client in your code + .event, it might be useful to share more of your code, just be careful not to include your token or any private information).
For more info, look into this post, I believe it is the answer you are looking for:
Discord.py on_member_join not working, no error message
Following the dicord.py docs:
channel = guild.get_channel(828481599057166356)
should be:
channel = client.get_channel(828481599057166356)
EDIT:
I found something else:
welcomeem = discord.Embed(title = f"Hey there {member.name}!", description = f"Welcome to {chanenl.guild.name}! Have a fun time here in TigerNinja's server!")
you misspelled "channel" in {chanenl.guild.name}

discord py - Send a different message if a user replies to my bot

I want to send a specific message if anyone replies to a message from my discord bot, I tried many things, but can't find a solution. Aside from that, it is new in 1.6 and I can't find it in the docs from discord. I found "reference" but don't understand how I can use that.
What I tried:
if len(message.replies) >= 0:
await ctx.send('thanks for reply dude')
but sadly, that doesn't work.
It's not message.replies, it's message.reference
if message.reference is not None:
await ctx.send("Thanks for the reply dude")
if message.reference is not None:
if message.reference.cached_message is None:
# Fetching the message
channel = bot.get_channel(message.reference.channel_id)
msg = await channel.fetch_message(message.reference.message_id)
else:
msg = message.reference.cached_message
Note that this still has some flaws, the message_id attribute can also be None
Reference:
Message.reference
MessageReference.channel_id
MessageReference.messageid

Check if the message channel is in that category

Can someone help me? I'm trying to make a modmail every thing is working fine, user sends a DM, bot creates a channel under "category" and if someone message inside that "category" it will be delivered to the user via DM. However I get this annoying error every time someone replies inside that "category" or user DMs the bot. I'm trying to make a category check to only do something if it's the mod mail category. Thank you in advance!
Here's my code:
async def on_message(self, message):
if isinstance(message.channel, discord.DMChannel):
# User DM the bot and the bot will make a textchannel under "category" and will send his DMs there. #
if message.channel.category_id == 123456789101112:
if isinstance(message.channel, discord.TextChannel):
# Message the user who DMed the bot using the textchannel under "category", your replies will be sent to the user by the bot via DM. #
Everything is working, but I'm getting this error every time someone replies inside that textchannel "category" or user DMs the bot
Error:
if message.channel.category_id == 123456789101112:
AttributeError: 'DMChannel' object has no attribute 'category_id'
Your if-statement doesn't make much sense, you should first check for the type of the channel and then for it's category and compare it.
async def on_message(message):
# Checking if the channel is a dm or a text one
if isinstance(message.channel, discord.DMChannel):
# do your thing
elif isinstance(message.channel, discord.TextChannel):
if message.channel.category is not None:
if message.channel.category.id == your_id:
# do your thing
You should format your code properly.
Also you can check whether message.author.id is your bots id client.user.id or a user.
Read the documentation for further information.

DM commands to discord bot

I recently had the idea of DMing my bot commands. For example, a command which would unban me from every server that the bot is on.
Unfortunately I don't have any starting point for the command because I'm not even sure DMing commands is possible.
Keywords like discord.py, command or DM are so common in google that finding any good info on the topic is very hard.
I'm looking for a way for the bot to receive DMs as commands and only accept them from me (my ID is stored in the variable ownerID if anyone wants to share any code).
While I'm mostly looking for the above, some code for the DM unban command would also be very helpful.
EDIT: I was asked to show some example code from my bot. Here is the code for the command number which generates a random number and sends it in a message. I hope this gives you an idea of how my bot is made:
#BSL.command(pass_context = True)
async def number(ctx, minInt, maxInt):
if ctx.message.author.server_permissions.send_messages or ctx.message.author.id == ownerID:
maxInt = int(maxInt)
minInt = int(minInt)
randomInt = random.randint(minInt, maxInt)
randomInt = str(randomInt)
await BSL.send_message(ctx.message.channel, 'Your random number is: ' + randomInt)
else:
await BSL.send_message(ctx.message.channel, 'Sorry, you do not have the permissions to do that #{}!'.format(ctx.message.author))
You can send commands in private messages. Something like
#BSL.command(pass_context=True)
async def unban(ctx):
if ctx.message.channel.is_private and ctx.message.author.id == ownerID:
owner = await BSL.get_user_info(ownerID)
await BSL.say('Ok {}, unbanning you.'.format(owner.name))
for server in BSL.servers:
try:
await BSL.unban(server, owner) # I think having the same name should be fine. If you see weird errors this may be why.
await BSL.say('Unbanned from {}'.format(server.name))
except discord.HTTPException as e:
await BSL.say('Unbanning failed in {} because\n{}'.format(server.name, e.text))
except discord.Forbidden:
await BSL.say('Forbidden to unban in {}'.format(server.name))
else:
if ctx.message.author.id != ownerID:
await BSL.say('You are not my owner')
if not ctx.message.channel.is_private:
await BSL.say('This is a public channel')
should work. I'm not sure what happens if you try to unban a user who is not banned, that may be what raises HTTPException

Categories