Python - DM 用户 Discord 机器人
Python - DM a User Discord Bot
我正在 Python 中开发一个用户 Discord 机器人。如果机器人所有者输入 !DM @user
,那么机器人将 DM 所有者提到的用户。
@client.event
async def on_message(message):
if message.content.startswith('!DM'):
msg = 'This Message is send in DM'
await client.send_message(message.author, msg)
最简单的方法是使用 discord.ext.commands
扩展。在这里,我们使用 converter to get the target user, and a keyword-only argument 作为可选消息来发送它们:
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await bot.send_message(user, message)
bot.run("TOKEN")
对于较新的 1.0+ 版本 discord.py,您应该使用 send
而不是 send_message
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
bot.run("TOKEN")
自从大迁移到 v1.0 后,send_message
不再存在。
相反,他们在每个端点(成员、行会等)上 migrated to .send()
。
v1.0 的示例为:
async def on_message(self, message):
if message.content == '!verify':
await message.author.send("Your message goes here")
!verify
的发件人将收到 DM。明智的做法是:
for guild in client.guilds:
for channel in guild.channels:
channel.send("Hey yall!")
如果您想向您的所有服务器和机器人所在的所有频道发送一条“hi yall”消息。
由于可能还不是很清楚 (根据评论判断),棘手的部分可能是从 client/session 获取用户身份句柄。如果您需要向 尚未 发送消息的用户发送消息,并且 on_message
事件之外。您将必须:
- 循环遍历您的频道并根据某些条件获取句柄
- 存储用户 handles/entities 并使用内部标识符访问它们
但是发送给用户的唯一方法是通过客户端身份句柄,在 on_message
中驻留在 message.author
中,或者在 guild.channels[index].members[index]
中的频道中。为了更好地理解这一点,我建议阅读 how to send a DM?.
上的官方文档
我过去使用过这个命令,我认为它最适合我:
@bot.command(pass_context=True)
async def mall(ctx, *, message):
await ctx.message.delete()
for user in ctx.guild.members:
try:
await user.send(message)
print(f"Successfully DMed users!")
except:
print(f"Unsuccessfully DMed users, try again later.")
@bot.command()
async def dm(ctx, user: discord.User, *, message=None):
if message == None:
message = "Hi!"
embed = make_embed(title=f"Sent by {user}", desc=message)
await user.send(embed=embed)
await ctx.send("Message sent!")```
我注意到我放入我的代码行的每个代码都不能完全工作,所以我添加了我的 自己的位 并且 bam 它工作了!将此添加到您的机器人代码时,不要忘记在 此处显示机器人名称 的地方添加机器人名称。它只会 DM 发送它的人,但您可以每天更改它说的内容,以便为使用该命令的每个人带来惊喜。它每次都对我有效。
@client.command()
async def botdm(ctx):
await ctx.message.author.send('hi my name is *bot name here* and i am a bot!')
我正在 Python 中开发一个用户 Discord 机器人。如果机器人所有者输入 !DM @user
,那么机器人将 DM 所有者提到的用户。
@client.event
async def on_message(message):
if message.content.startswith('!DM'):
msg = 'This Message is send in DM'
await client.send_message(message.author, msg)
最简单的方法是使用 discord.ext.commands
扩展。在这里,我们使用 converter to get the target user, and a keyword-only argument 作为可选消息来发送它们:
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await bot.send_message(user, message)
bot.run("TOKEN")
对于较新的 1.0+ 版本 discord.py,您应该使用 send
而不是 send_message
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
@bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
bot.run("TOKEN")
自从大迁移到 v1.0 后,send_message
不再存在。
相反,他们在每个端点(成员、行会等)上 migrated to .send()
。
v1.0 的示例为:
async def on_message(self, message):
if message.content == '!verify':
await message.author.send("Your message goes here")
!verify
的发件人将收到 DM。明智的做法是:
for guild in client.guilds:
for channel in guild.channels:
channel.send("Hey yall!")
如果您想向您的所有服务器和机器人所在的所有频道发送一条“hi yall”消息。
由于可能还不是很清楚 (根据评论判断),棘手的部分可能是从 client/session 获取用户身份句柄。如果您需要向 尚未 发送消息的用户发送消息,并且 on_message
事件之外。您将必须:
- 循环遍历您的频道并根据某些条件获取句柄
- 存储用户 handles/entities 并使用内部标识符访问它们
但是发送给用户的唯一方法是通过客户端身份句柄,在 on_message
中驻留在 message.author
中,或者在 guild.channels[index].members[index]
中的频道中。为了更好地理解这一点,我建议阅读 how to send a DM?.
我过去使用过这个命令,我认为它最适合我:
@bot.command(pass_context=True)
async def mall(ctx, *, message):
await ctx.message.delete()
for user in ctx.guild.members:
try:
await user.send(message)
print(f"Successfully DMed users!")
except:
print(f"Unsuccessfully DMed users, try again later.")
@bot.command()
async def dm(ctx, user: discord.User, *, message=None):
if message == None:
message = "Hi!"
embed = make_embed(title=f"Sent by {user}", desc=message)
await user.send(embed=embed)
await ctx.send("Message sent!")```
我注意到我放入我的代码行的每个代码都不能完全工作,所以我添加了我的 自己的位 并且 bam 它工作了!将此添加到您的机器人代码时,不要忘记在 此处显示机器人名称 的地方添加机器人名称。它只会 DM 发送它的人,但您可以每天更改它说的内容,以便为使用该命令的每个人带来惊喜。它每次都对我有效。
@client.command()
async def botdm(ctx):
await ctx.message.author.send('hi my name is *bot name here* and i am a bot!')