Skip to main content
 首页 » 编程设计

python之discord.py send_message 用法

2025年02月15日16linjiqin

我已经开始从事一个项目来加速我对 Python 的学习。我正在尝试重新创建一个我经常使用的 discord 机器人,因为我已经习惯了它的功能。下面是我当前的代码

import discord 
from discord import User 
from discord.ext.commands import Bot 
 
import secrets 
 
pybot = Bot(command_prefix = "!") 
 
@pybot.event 
async def on_read(): 
    print("Client logged in") 
 
@pybot.command() 
async def hello(*args): 
    print(User.display_name) 
    return await pybot.say("Hello, world!") 
 
@pybot.command() 
async def truck(*args): 
    await pybot.send_message(message.user,'Watchout for that truck!') 
 
pybot.run(secrets.BOT_TOKEN) 

我想要实现的是当有人输入命令 !truck <mention user> 时它向提到的用户发送消息“小心那辆卡车!”。

我收到以下错误:

Command raised an exception: NameError: name 'message' is not defined

我已经尝试查找我正在尝试做的事情的例子,但没有找到太多,或者我不理解我应该做什么。希望这不是对类似问题的转贴

谢谢。

请您参考如下方法:

我相信卡车中的 *args 不再是有效的语法,对于 discord.py 的命令

@pybot.command(pass_context=True) 
async def truck(ctx): 
    await pybot.send_message(ctx.message.user, 'Watchout for that truck!') 

检查 Discord.py 的 github 存储库及其 examples