discord.py - No DM sent to the user

2024/10/5 19:10:04

I am making a discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel.

Here is my code:

import discord, asyncioapp = discord.Client()@app.event
async def on_message(message):if message.content.startswith('!DM'):usernotsending = []content = message.contentmsg = await message.channel.send('메시지를 보내고 있습니다!')i = app.user# 봇의 모든 유저를 for문으로 적용for i in app.user:try:if i == app.user:return# 해당 유저의 DM을 염await i.create_dm()# 내용전송await app.dmchannel.send(content)# DiscordAPI 에서 오류가 발생했을경우except discord.HTTPException:# DM을 보내지 못한 유저 태그 list에 저장usernotsending.append(f'{i.name}#{i.discriminator}')messageing = """아래 유저들에게 메시지를 전송하지 못했습니다!직접 보내주시거나, 따로 전달을 해드려야됩니다!"""for msg in usernotsending:# message 에 유저 태그 추가messageing += msg# 메시지 전송await msg.edit(content=messageing)
Answer

Context is only part of a commands.Bot instance. Your code and your explanation don't seem to match. Assuming you want to DM the author:

import discordapp = discord.Client()@app.event
async def on_message(message):if message.content.startswith('!DM'):try:await message.author.send(...)except discord.HTTPException:...

If you want to DM everyone the bot can see:

import discordapp = discord.Client()@app.event
async def on_message(message):if message.content.startswith('!DM'):for user in app.users:try:await user.send(...)except discord.HTTPException:...
https://en.xdnf.cn/q/119549.html

Related Q&A

Improve CPU time of conditional statement

I have written an if-elif statement, which I believe not be very efficient:first_number = 1000 second_number = 700 switch = {upperRight: False,upperLeft: False,lowerRight: False,lowerLeft: False,middle…

Why no colon in forming a list from loop in one line in Python?

From this website, there is a way to form a list in Python from loop in one line squares = [i**2 for i in range(10)]My question is, typically, after a loop, there is a colon, e.g., squares = [] for i i…

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…

Python decode unknown character

Im trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors likeascii codec cant decode byte 0xae in position 8: ordinal not…

UnboundLocalError: TRY EXCEPT STATEMENTS

I am currently creating a menu with try except tools. Im trying to create it so if a user enters nothing (presses ENTER) to output:You have not entered anything, please enter a number between 1 and 4Th…

Cant load music into pygame

please help if you can. Cant seem to be able to upload music into my game in progress. It comes up with the error of "cant load"... Would be great if someone got back to me quick, This is a m…

C# Socket: how to keep it open?

I am creating a simple server (C#) and client (python) that communicate using sockets. The server create a var listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp)then …

Python Selenium - how to get confirmation after submit

I have a follow up question on this post, I want to get any confirmation text after I hit submit button. Either the code works or not. html - invalid example <div class="serialModalArea js-seri…

Assigning a input on a line to its line number?

I was having a go at the following problem from the AIO (Australian Informatics Olympiad) Training Problems Site (Question in Italics and specifics in bold, my attempt below): The Problem Encyclopaedia…

Create a new list according to item value

I have a list like below. [T46, T43, R45, R44, B46, B43, L45, L44, C46, C45]where I want to group according to int value:[id][ , , , , ] # AREA PATTERN [Top, Right, Bottom, Left, Center][46][1,0,1…