問題描述
我已經開始著手一個項目來加速我對 python 的學習.我正在嘗試重新創建一個我經常使用的不和諧機器人,因為我已經習慣了它的功能.以下是我當前的代碼
I've started working on a project to accelerate my learning of python. I'm trying to recreate a discord bot I use quite a bit since i'm already use to its features. Below is my current code
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>
時,它會向提到的用戶發送一條消息,其中包含小心那輛卡車!"的消息.
what im trying to get to happen is when someone types the command !truck <mention user>
it sends a message to that mentioned user with the message "Watch out for that truck!".
我收到以下錯誤:
命令引發異常:NameError: name 'message' is not defined
Command raised an exception: NameError: name 'message' is not defined
我嘗試查找我正在嘗試做的事情的示例,但沒有找到太多,或者我不明白我應該做什么.希望這不是類似問題的轉貼
I've tried looking up examples of what im trying to do but haven't found much, or am not understanding what I should be doing. Hopefully this isn't a repost of a similar question
謝謝.
推薦答案
我認為你的卡車中的 *args 不再是有效的語法,我相信使用 discord.py 的命令
The *args in your truck is no longer valid syntax I believe for the commands with 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 存儲庫例子
這篇關于discord.py send_message 用法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!