【问题标题】:How do I make a discord.py embed?如何制作 discord.py 嵌入?
【发布时间】:2021-07-28 16:22:59
【问题描述】:

代码本质上应该创建一个嵌入,但由于某种原因它给了我错误“'message' is not defined”(嵌入将被放入另一个机器人中)

import discord
from discord.ext import commands
import random
from discord.ext.commands import Bot



class MyClient(discord.Client):

    async def on_ready(self):
        print('Logged on as', self.user)
        channel = self.get_channel(717005397655027805)
        await channel.send("I am now online")

        messageContent = message.Content
        if message.author == self.user:
            return

        @client.event
        async def on_message(message):
            if message.content.startswith('!hello'):
                embedVar = discord.Embed(
                    title="Title", description="Desc", color=0x336EFF
                )
                embedVar.add_field(name="Field1", value="hi", inline=False)
                embedVar.add_field(name="Field2", value="hi2", inline=False)
                client.send_message(message.channel, embed=embedVar)

                PREFIX = ("$")
                bot = commands.Bot(command_prefix=PREFIX, description='Hi')



client = MyClient()
client.run('TOKEN')

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你没有在on_ready()函数中指定message.Content是什么,你还需要在on_ready()函数之外创建on_message()函数,你没有指定@client是什么装饰器是,您只是在代码末尾指定了它。

    总而言之,你的固定代码应该是这样的

    from discord.ext import commands
    
    TOKEN  = "Your token here"
    PREFIX = '$'
    bot    = commands.Bot(command_prefix=PREFIX)
    
    @bot.event
    async def on_ready():
        print('Logged on as', bot.user)
        channel = bot.get_channel(717005397655027805)
        await channel.send("I am now online")
    
    @bot.command() # Usage - $hello
    async def hello(ctx):
        if ctx.message.author == bot.user: return
        embedVar = discord.Embed(title="Title", description="Desc", color=0x336EFF)
        embedVar.add_field(name="Field1", value="hi", inline=False)
        embedVar.add_field(name="Field2", value="hi2", inline=False)
        await ctx.send(embed=embedVar)
    
    bot.run(TOKEN)
    

    【讨论】:

    • 你的代码还是有点问题。您的 message 未定义。
    • 伙计,他正在使用Classes and Objects,所以我认为您需要提供 OOP 类型的代码而不是这个。
    • 他没有使用 cogs,所以他不需要 oop
    • 您好,非常感谢您的贡献,我想让您知道代码不起作用并且错误代码显示“'self' is not defined”
    • 哎呀忘记从 self.user 更改为 bot.user
    【解决方案2】:

    当您为机器人使用CLASSES AND OBJECTSOOP 时,您也需要正确的语法。我真的帮不了你,但可以使用正常的方式。

    第 1 步:

    我们将导入库:

    import discord
    from discord.ext import commands
    import random
    

    第 2 步:

    我们将定义 Prefix 和 Bot 的变量。

    PREFIX = "$"
    bot = commands.Bot(command_prefix=PREFIX, description="Hi")
    

    第三步:

    我们现在将根据您的代码编写机器人的on_ready() 命令。

    @bot.event
    async def on_ready():
        print('Logged on as', self.user)
        channel = bot.get_channel(717005397655027805)
        await channel.send("I am now online")
    

    语句bot.get_channel()被用于获取发送消息的通道。

    第四步:

    我们现在将根据您的代码编写bot的on_message函数。

    @bot.event
    async def on_message(message):
        if message.content.startswith('!hello'):
            embedVar = discord.Embed(
            title="Title", description="Desc", color=0x336EFF
                    )
            embedVar.add_field(name="Field1", value="hi", inline=False)
            embedVar.add_field(name="Field2", value="hi2", inline=False)
            await message.channel.send(embed=embedVar)
    

    第五步:

    我们现在将创建一个语句来启动您的机器人。

    bot.run("TOKEN")
    

    在脚本底部添加此命令。


    编译的整个代码将是:

    import discord
    from discord.ext import commands
    import random
    
    PREFIX = "$"
    bot = commands.Bot(command_prefix=PREFIX, description="Hi")
    
    @bot.event
    async def on_ready():
        print('Logged on as', self.user)
        channel = bot.get_channel(717005397655027805)
        await channel.send("I am now online")
    
    @bot.event
    async def on_message(message):
        if message.content.startswith('!hello'):
            embedVar = discord.Embed(
            title="Title", description="Desc", color=0x336EFF
                    )
            embedVar.add_field(name="Field1", value="hi", inline=False)
            embedVar.add_field(name="Field2", value="hi2", inline=False)
            await message.channel.send(embed=embedVar)
    

    这将解决您的问题。如果还有问题,请在 cmets 中问我。 :)

    谢谢! :D

    【讨论】:

    • 嘿,我真的很感谢你的工作来帮助我,但它仍然说'消息'没有定义。
    • @Ethan 对不起。我看到了不对劲的地方。我现在已经相应地更改了代码。你可以按照步骤,看看怎么写。
    • @Ethan 如果我能提供帮助,请接受答案。 :D
    猜你喜欢
    • 2021-12-20
    • 2020-12-26
    • 2020-11-14
    • 2022-01-01
    • 2020-12-09
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多