是否可以将 .random.choice 用于 url(特别是 gif)?
Is it possible to use .random.choice for urls (specifically gifs)?
我正在尝试创建一个机器人,它将根据命令发送随机 gif(从我选择的几个选项中)。
然而,它总是返回错误,说找不到命令,即使我输入了 ?hug & attribute 错误:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "hug" is not found
Ignoring exception in command hug:
Ignoring exception in command hug:
Traceback (most recent call last):
File "C:\Users\lemaia\Pictures\Discord\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\lemaia\Documents\DiscordBot\!givebot\v4.0.0.py", line 16, in hug
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
@client.command()
async def hug(ctx):
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
await ctx.channel.send(embed=embed)
client.run('TOKEN')
您试图使用 Embed
对象的属性 random
,而不是调用 random.choice
。
要在嵌入中设置图像,您需要使用 discord.Embed.set_image
:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
@client.command()
async def hug(ctx):
image = random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135']
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.set_image(url=image))
await ctx.send(embed=embed)
client.run('TOKEN')
Context
对象有一个 send
属性,它基本上是 Context.channel.send
的别名
我正在尝试创建一个机器人,它将根据命令发送随机 gif(从我选择的几个选项中)。
然而,它总是返回错误,说找不到命令,即使我输入了 ?hug & attribute 错误:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "hug" is not found
Ignoring exception in command hug:
Ignoring exception in command hug:
Traceback (most recent call last):
File "C:\Users\lemaia\Pictures\Discord\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\lemaia\Documents\DiscordBot\!givebot\v4.0.0.py", line 16, in hug
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
@client.command()
async def hug(ctx):
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
await ctx.channel.send(embed=embed)
client.run('TOKEN')
您试图使用 Embed
对象的属性 random
,而不是调用 random.choice
。
要在嵌入中设置图像,您需要使用 discord.Embed.set_image
:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
@client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
@client.command()
async def hug(ctx):
image = random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135']
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.set_image(url=image))
await ctx.send(embed=embed)
client.run('TOKEN')
Context
对象有一个 send
属性,它基本上是 Context.channel.send