Send message to dms discord.py - python

I want to send message to dms using my discord bot and because the api has changed from client.send_message(user, message) to channel.send(message) I have no idea how to do that, also I dont want to make it dependent on the on_message(message) event. Any ideas?

If you have the user ID you can do:
user = client.get_user(user_id)
await user.send('Hello')
However If you already have the User object, you can do the following:
await message.author.send('Hey')
Where message is a Message object received from a on_message() event.
As for whether you can send private messages without first receiving an event unfortunately that is not be possible due to obvious spam-related issues.

Related

How can i force the Telegram Bot to write to the user first (aiogram)?

I'm trying to send welcome message to newcomers to the channel via the bot using Aiogram. However, my bot can't find the chat with the user if he has not started a conversation with the bot before. Are there any solutions of this problem?
I have already managed to handle newcomers and add ids to the db, but
`
#router.chat_member(ChatMemberUpdatedFilter(member_status_changed=(KICKED | LEFT | RESTRICTED)
>>
(ADMINISTRATOR | CREATOR | MEMBER)))
async def on_user_join(event: ChatMemberUpdated):
with grpc.insecure_channel('') as channel:
stub = sub_unsub_pb2_grpc.SubscribtionServiceStub(channel)
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(datetime.now(tz=timezone.utc))
request = sub_unsub_pb2.SubUnsubEvent(
id=str(event.new_chat_member.user.id),
channelId=str(event.chat.id),
time=timestamp,
subStatus='join'
)
response = sub_unsub_pb2.EventResponse()
stub.TriggerEvent(request)
`
But when I try to send a message, the error chat not found is thrown.
`
await support_bot.send_message(
user_id,
f'Welcome to the channel!'
)
`
Solutions to this problem:
Is to have the bot transfer a message to the user as soon as their ID
is added to the database. This way, the user will have started a
discussion with the bot, and the bot will be capable of finding the
chat when you try to send the welcome message.
Is to use the start command, so that new users can initiate the
discussion with the bot by sending /start command. Also, in the
start command handler, you can check whether the user is a new member
and send a welcome message.
Check the user's presence in the channel by using the
get_chat_member method of the bot, which returns a ChatMember
object. If the status attribute of this object
isChatMember.CHAT_MEMBER_STATUS_LEFT, then the user isn't a member
of the channel anymore and you shouldn't send the message.
Use the message event to listen for all messages transferred by the
user in the channel and check if the user is new or not and send the
welcome message accordingly

Getting content from a dm in discord.py

So I want to know if it is possible, that a bot gets the content sent to it in a dm and send that in a specifyed channel on a server.
So basically you dm the bot the word "test" and the bots sends the word in a channel of a server
Yes, it is possible for a bot to receive a direct message and then repost the message in a specified channel on a server. This can be done using the Discord API.
You can do the following:
Create a Discord bot and add it to your server. You can do this using the Discord developer portal.
Use the Discord API to listen for messages sent to the bot in a DM. You can do this using the message event and the DMChannel class in the Discord API.
When the bot receives a DM, use the Discord API to repost the message in the specified channel on the server. You can do this using the send method of the TextChannel class in the Discord API.
Yes, this is possible:
First, we use the on_message() event to detect when a message is sent.
We then check if the message is sent in a DM.
If so, we will send a message to a specific channel.
Here is one way you can implement it:
# import ...
bot = discord.Bot(...)
#bot.event
async def on_message(message):
# check if it's
if isinstance(message.channel, discord.DMChannel):
# get the channel from ID
channel = client.get_channel(CHANNEL_ID) # put channel ID here
await channel.send("test") # whatever you want to send
#bot.event:
async on_message(message):
if message.DMChannel:
# define channel and send message to channel.
You can also refer to DOCS.

Why am I author of message send by byt - python discord

I am trying to send discord message to channel using python bot, but when I print it's author, it's me and not the bot. So later I can't edit it because of the author.
How can I send message as the bot?
My function:
#bot.command(name="send")
async def send(ctx: Context) -> None:
message = "message"
await ctx.channel.send(message)
print(ctx.message.author)
Because ctx.message is the message that invoked the command, not the message sent by the bot... So ctx.message.author is the person that invoked the command, which is you.
print(ctx.message.author) # <- "print the author of the message that invoked this command"
When you use Messageable.send(), it returns the message it sent. Thus, ctx.channel.send() returns the message that it sends. The .author of that message is your bot, and that is the message you can edit.
message = await ctx.channel.send("something")
I should mention I'm using a wrapper called pycord which I highly recommend.
member.id (Shows the specified members id)
member.name (Shows the specified members name and #)
member.mention (Mentions the specified member)
Doing ctx.send will show only the bot's message but will show a error code at the same time, So use ctx.respond("whatever") instead.
ctx.channel.send will send to a channel is
channel.send will send to a specified channel

Python - Discord bot on ready send message to channel

I have problem creating simple python script which sends message to default channel when executed from terminal.
import discord
#bot.event
async def on_ready(ctx):
print('Online')
await ctx.send('Message sent!')
bot.run('MYTOKEN')
with this example I keep getting "ctx" is not defined
The issue here is that the on_ready event should not receive any parameters. See the Minimal Bot documentation here and the on_ready documentation in the Event Reference here. If you want to send a message when the bot connects. You must first get the channel object and then use the send method. You can find an example of getting a channel and sending a message in the FAQ section of the docs here

discord.py How to get logs from a DM channel

I'm trying to detect whether the last message my bot has sent to a user is identical to the one it needs to send (Python 3.5).
I've tried to use client.logs_from(channel,limit=1) but I'm not sure how to make it get the logs from a DM.
client.logs_from accepts a PrivateChannel instance to its channel argument. Assuming you know which user's PM channel you want to check already (and it sounds like you do), it's as simple as:
# PrivateChannel instance is privateCh
newMsg = 'your message here'
async for msg in client.log_from(privateCh, limit=1):
if newMsg != msg.content:
await client.send_message(privateCh, newMsg)

Categories

Resources