Python Discord.py获取通道名称和服务器名称时返回错误消息

Python Discord.py获取通道名称和服务器名称时返回错误消息,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我目前正在尝试创建一个discord机器人,当有人说一个命令时,它会说他们所在的服务器和频道。在过去,我能够做到这一点,但是在过去,我在消息(message)上使用和if message.content.startswith(“$hello”)。我最近开始使用@bot.command,我仍在努力适应它。我尝试使用message.guild.name和message.channel.notice,但收到一条错误消息未定义变量“message”我假设这是因为在我的旧设置中,在消息(message)中

我目前正在尝试创建一个discord机器人,当有人说一个命令时,它会说他们所在的服务器和频道。在过去,我能够做到这一点,但是在过去,我在消息(message)上使用
if message.content.startswith(“$hello”)
。我最近开始使用
@bot.command
,我仍在努力适应它。我尝试使用
message.guild.name
message.channel.notice
,但收到一条错误消息<代码>未定义变量“message”
我假设这是因为在我的旧设置中,在消息(message)中我定义了消息,但在我的当前代码中它似乎不起作用

import discord
import asyncio
from discord.ext import commands
botToken = token
bot = commands.Bot(command_prefix = '#')
client = discord.Client()
@bot.event
async def on_ready():
    print('Bot is online and ready.')
@bot.command()
async def whereAmI(ctx, *, messageContents):
    link = await ctx.channel.create_invite(max_age = 300)
    message = 'You are in {message.guild.name} in the {message.channel.mention} channel with an invite link of ' + link
    await ctx.message.author.send(message)
bot.run(botToken)
我知道它可能会对这个人进行DM,但是我目前正在开发我的测试机器人,然后才将命令转移到我的主机器人上。如果你有更好的主意,请告诉我。我之所以有变量,是因为我计划在最终产品中加入变量


如果有任何不合理的地方,请告诉我,我将对其进行编辑,希望能使其更加清晰,并在需要时提供更多上下文。

要获取消息对象,需要使用传入的“上下文”(ctx)。所以应该是
ctx.message.guild.name
ctx.message.channel.notice


另一方面,Bot是Client的一个子类。所以,无论客户端能做什么,Bot也能做什么。你不需要客户端和机器人,只需要机器人

这是一个应该是怎样的例子

@bot.command()
异步定义(ctx,*,messageContents):
link=wait ctx.channel.create\u invite(最大年龄=300)
message=f'您在{ctx.message.channel.notify}频道的{ctx.message.guild.name}中,邀请链接为'+link'
等待ctx.message.author.send(消息)

祝你有愉快的一天

使用当前的
async def wheremi(ctx,*,messageContents):
命令,我如何告诉它使用传入的上下文?我是否将
@bot.command
替换为
@bot.command(pass\u context=True)
?您已经在上下文中传递了。这是ctx参数。上下文自动随命令一起出现。所以不需要
pass_context
bit:)谢谢你的回答,但几个月前lmao就回答了这个问题。