3

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)

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!".

I'm getting the following error:

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

Thanks.

1 Answer 1

1

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!')

Checkout the github repository for Discord.py with their examples

2
  • thank you! I see that now. I was following a blog post that seems to be outdated. Thanks for clearing that up.
    – Milkbone
    Feb 5, 2017 at 6:21
  • Yeah I recommend you check out the discord api server in discord, it's got a lot of helpful people as well as the guy who coded the discord.py Feb 6, 2017 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.