【问题标题】:discord bot : undefined name channeldiscord bot:未定义的名称通道
【发布时间】:2021-03-26 13:55:04
【问题描述】:
import os
import discord

import random
from prettytable import PrettyTable
from PIL import Image


client = discord.client()

    
@client.event
async def on_ready():
    print("We have logged in as {0.user}".format(client))
  

@client.event
async def on_message(message):
    if message.author == client.user:
        return


    if message.content.startswith("$picture"):
       
        await channel.send(file=discord.File("images.jpg"))
    


client.run(os.getenv('TOKEN'))

您好,这是我第一次使用堆栈溢出,如果我的帖子有一些错误,我提前道歉, 所以当我在聊天框中输入“$picture”时,我试图让我的不和谐上传一张图片,但是我在await channel.send(file=discord.File("images.jpg")) 说“未定义的命名为'频道'”的行上得到一个红色下划线我试着调查它但找不到太多。有任何想法吗?谢谢。

【问题讨论】:

  • 这意味着您的变量没有在任何地方定义。 @GGberry 有正确的解决方案

标签: python discord


【解决方案1】:

您需要使用message.channel.send() 而不是channel.send()

所以不是

@client.event
async def on_message(message):
    if message.author == client.user:
        return


    if message.content.startswith("$picture"):
       
        await channel.send(file=discord.File("images.jpg"))

你可以的

@client.event
async def on_message(message):
    if message.author == client.user:
        return


    if message.content.startswith("$picture"):
       
        await message.channel.send(file=discord.File("images.jpg"))

【讨论】:

  • 非常感谢!
  • 没问题!提示:人们通常会接受能解决您问题的答案,这样就不会有其他人在解决问题时看到这个问题。
猜你喜欢
  • 2020-02-16
  • 2021-04-16
  • 1970-01-01
  • 2019-03-25
  • 2017-05-21
  • 1970-01-01
  • 2022-01-13
  • 2021-10-28
  • 2020-12-27
相关资源
最近更新 更多