I try to make my bot to get message from DM using this syntax:
for wolf in wolf_list_id:
poll_message = await self.client.get_message(wolf, react_message.id)
The wolf contains the id of user, but the get_message syntax can't take the id from wolf. Any ideas?
like said the doc, the client.get_message methods must take as parameter an channel object and an id.
In case of DM channel, you can pass an user or member object.
For get an user by id, you can use client.get_user_info methods:
user = await client.get_user_info("123456789")
And after, with your userobject, you can get message with ID
message = await client.get_message(user, "135792468")
So, for fix your code, if wolf is a string id, you can use this following code:
for wolf in wolf_list_id:
user = await self.client.get_user_info(wolf)
poll_message = await self.client.get_message(user, react_message.id)
Related
So I need my bot to print in which channel it sents a message. This is my code:
#client.command()
async def test(ctx):
await ctx.reply('hello', mention_author=True)
channel = #what goes here????
print('i said hello in', (channel))
I've tried all of these:
user = bot.get_user(user_id)
message.author.username
username = client.get_user(user_id)
The things you tried are referencing user id. I have not used discord.py in a while, but you will have to reference the channel id and not the user id. Maybe try:
ctx.message.channel.id
I can't remember if this returns an object or a string so you may have to format further to get the string.
I am using discord.py and I am trying to get the member object from the command, and when a button is pressed, it edits the message and display's user mod log data. I got it working but in a sort of odd way, it pings the person in the message, and then the event checks to see if there was a ping in (interaction) I am wondering if there is a way to look inside the embed for a mention like instead of interaction.message.mentions is there something like that for embeds instead of message? Thanks in advance!
#client.command()
async def modlogs(ctx, member: discord.Member):
main=discord.Embed(title=" ", description=f"{ctx.author.mention} please use the buttons below to navigate {member.mention}'s modlogs.", color=0x76dba8)
main.set_author(name=f"{member}", icon_url = member.avatar_url)
ram_member = member
await ctx.send(f"{member.mention}",
embed = main,
components=[[
Button(style=ButtonStyle.blue, label="Warnings", custom_id= "blue"),Button(style=ButtonStyle.blue, label="Kicks", custom_id= "red"),Button(style=ButtonStyle.blue, label="Bans", custom_id= "green")
]],
)
#client.event
async def on_button_click(interaction):
print(interaction.message)
if interaction.message.mentions:
if (interaction.message.mentions.__len__() > 0):
for member in interaction.message.mentions:
if collection2.count_documents({"_id": member.id}) == 0:
embed1=discord.Embed(description=f"{member.mention} has no warnings!", color=0xff0000)
embed1.set_author(name="Error")
else:
Get a Message
Msg = await Bot.get_message(channel, id)
Get an Embed
Look at the first answer to [this question][1].
Emb = discord.Embed.from_data(Msg.embeds[0])
Get the fields
Look at the answer to [this question][2].
for field in Emb.fields:
print(field.name)
print(field.value)
Get the description
Look at the first answer to [this question][3].
Des = Emb.description
Search for the mention
>>> Member.mention in Des
True
Searching a mention
A mention begins with ```#!``` and ends with ```>```.
So you might use this fact to search a mention into a string (Embed.description returns a string-type object).
Like this:
Men = Des[Des.find("<#!"):Des.find(">")]
UserID = int(Men[3:])
Member = ctx.fetch_user(UserID)
Links
Discord.py get message embed
How to align fields in discord.py embedded messages
discord.py Check content of embed title
How do I mention a user using user's id in discord.py?
In conclusion
I guess it will work.
I'm quite new to discord.py so I'm posting this here. How do I make a command so that when the user says !embed (message here) #channelhere it turns the users message into an embed and then posts it into the specified channel? An explanation would be nice to so I can try to understand it.
Based on the 2 answers I see, you can send an embed to a specific channel whatever you want by converting the channel into the channel id.
Here's the code :
#client.command()
async def echoembed(ctx, channel : discord.TextChannel, *, Text : str): # full command (!echoembed #channelhere Title here | Description here)
Title, Description = Text.split(' | ') # You need to split the arguments using |, change to other symbols whatever you want
channel_id = channel.id # this will convert the channel name into the channel id
channel_send = client.get_channel(channel_id) # we will get the channel using the channel id
embed = discord.Embed(title = Title, description = Description, color = 0x2F3136)
embed.timestamp = datetime.datetime.utcnow() # this will tell the time today
embed.set_footer(text = "\u200b", icon_url = ctx.author.avatar_url) # the \u200b is an empty character
await channel_send.send(embed = embed) # and finally, we can send the embed
If you're fairly new to discord.py, I suggest you need to read the docs before going into complicated stuff.
The docs will be linked Here
Thank me later :D
Sample code I have:
Eg: I enter !embed hello / wow
#bot.command() #used before a command
async def embed(ctx, *, content: str): #when the person uses !embed it will do what is under. Ctx is needed to store the channel of the message, author of message and other properties. content is the argument, or what you put after the !embed (in this case hello / wow)- we state that it is a string(str) or plain text if that is more clear
title, description= content.split('/') #the title and description of the embed are separated by "/" In this cas, title is "hello",description is "wow"
embed = discord.Embed(title=title, description=description, color=0x00ff40) #making the embed (MUST HAVE TITLE AND DESCRIPTION)
await ctx.send(embed=embed) #sending the embed in THE SAME CHANNEL YOU SENT THE COMMAND IN (as shown via ctx)
await ctx.message.delete() #deleting the original message by you to clean the channel up(as shown by ctx.message)
In terms of specific channel that might be a bit more complicated: however this should work perfectly
Basiclly you have to create a variable called channel that contains the channel ID, for example: channel = bot.get_channel(636399538650742795). Then you create a command that takes an argument, this argument is the message to be embeded, you store the argument in a variable, make a variable called embed, this variable will be embed = discord.Embed(value=argument), wich we will send with await channel.send(embed=embed). The actual code looks like this:
#bot.command()
async def embed(ctx, argument: str):
channel = bot.get_channel(636399538650742795)
embed = discord.Embed()
embed.add_field(name="Message", value=argument)
await channel.send(embed=embed)
To get the channel ID, just right click the channel you want to get the ID of and click Copy ID. And considering that you're very new to discord.py, I am using #bot.command() because I am defining it with bot = commands.Bot(command_prefix="$")
I set up a very simple command for a Discord bot. This is supposed to, in response to the !positions command, check the author and channel, and if the author is me, and the channel is my DM channel with the bot, do something (like send a reply message).
The ID of my account is 285538805728149504. When the command !positions is run, it outputs this:
Author ID 285538805728149504
DM channel 814250853669666887
Current channel 814250853669666887
You can see that the author ID it prints out is exactly the same as the hardcoded user ID I'm comparing it to. You can also see that the two channel IDs compared are exactly the same.
However, it doesn't get past any of the if statements.
Here's the code:
#commands.command()
async def positions(self, ctx):
positions_file = Path("positions.json")
positions = json.loads(positions_file.read_text())
message_author = ctx.message.author.id
print("id " + str(message_author))
print("dm channel " + str(ctx.message.author.dm_channel.id))
print("Current channel " + str(ctx.message.channel.id))
my_id = "285538805728149504"
if message_author == my_id:
print("Author ID is my ID")
if ctx.message.author.dm_channel.id == ctx.message.channel.id:
print("Channel ID is my DM channel")
await ctx.message.author.dm_channel.send("reply")
Why can't I successfully compare these numbers?
It looks like you're comparing a str object and an int object in this line:
if message_author == my_id:
Note that an id by default is an integer (documentation on that here).
Either change your my_id to be an integer, or use str(message_author) in the previously mentioned line!
Change is to == and try it.
Also try putting the ctx.message.author.id into a var
How do I display the user's Name + Discord Tag? As in:
I know that;
f"Hello, <#{ctx.author.id}>"
will return the user, and being pinged.
(#user)
And that;
f"Hello, {ctx.author.name}"
will return the user's nickname, but WITHOUT the #XXXX after it.
(user)
But how do I get it to display the user's full name and tag?
(user#XXXX)
To get user#XXXX you can just do str(ctx.author) (or just put it in your f-string and it will automatically be converted to a string). You can also do ctx.author.discriminator to get their tag (XXXX).
Below I have listed most, if not all, of the ways you can use ctx.author, as well as what it gives.
await ctx.send(f"Hello {ctx.author}") # author + number discriminator
await ctx.send(f"Hello {ctx.author.display_name}") # nickname of author of message
await ctx.send(f"Hello {ctx.author.mention}") # mention the author (ping)
await ctx.send(f"Hello {ctx.author.id}") # author's user id
await ctx.send(f"Hello {ctx.author.discriminator}") # the numbers from author#0001