本文介紹了Python - DM 一個(gè)用戶 Discord 機(jī)器人的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在使用 Python 開發(fā) User Discord Bot.如果 bot 所有者鍵入 !DM @user
,那么 bot 將 DM 所有者提到的用戶.
I'm working on a User Discord Bot in Python .If the bot owner types !DM @user
then the bot will DM the user that was mentioned by the owner.
@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)
推薦答案
最簡(jiǎn)單的方法是使用 discord.ext.commands
擴(kuò)展.這里我們使用 converter 來獲取目標(biāo)用戶,和一個(gè) keyword-only 參數(shù)發(fā)送給他們的可選消息:
The easiest way to do this is with the discord.ext.commands
extension. Here we use a converter to get the target user, and a keyword-only argument as an optional message to send them:
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")
對(duì)于較新的 1.0+ 版本的 discord.py,您應(yīng)該使用 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")
這篇關(guān)于Python - DM 一個(gè)用戶 Discord 機(jī)器人的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!