我在这里具有使用文件夹名称作为命令的功能。现在唯一的文件夹是“拥抱”和“ headpat”。因此,当我将.hug
或.headpat
发送给Discord时,它将进入该文件夹,抓取随机图像并将其发布。
@client.event
async def on_message(message):
async def random_image(dir): #picks a random image from local directory
if message.content.lower() ==f".{dir}":
image = random.choice(os.listdir(dir))
embed = discord.Embed(color = 0xff9e00)
chosen_one = discord.File(f"{dir}\\{image}", filename = image)
embed.set_image(url = f"attachment://{image}")
await message.channel.send (embed = embed, file = chosen_one)
print(f"{image} sent in response to {(message.author)}.")
print(f"----------")
但是,我希望能够进行如下操作,但是我不确定从哪里开始。
@bot.command()
async def folder_name(ctx):
#code that does the same as the function above
答案 0 :(得分:0)
这是一个简单的机器人,可将您的代码作为命令实施。
import discord
from discord.ext import commands
TOKEN = "your token"
PREFIXES = ['.']
bot = commands.Bot(command_prefix=PREFIXES)
@bot.command(name='image')
async def random_image(ctx, dir):
image = random.choice(os.listdir(dir))
embed = discord.Embed(color = 0xff9e00)
chosen_one = discord.File(f"{dir}\\{image}", filename = image)
embed.set_image(url = f"attachment://{image}")
await ctx.send (embed = embed, file = chosen_one)
print(f"{image} sent in response to {(message.author)}.")
print(f"----------")
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name} in {len(bot.guilds)} guilds!")
@bot.event
async def on_message(message):
if message.author.bot: # I always do this to make sure it isn't a bot
return
await bot.process_commands(message)
if __name__ == '__main__':
try:
bot.run(TOKEN)
finally:
print("Logging Out")
答案 1 :(得分:0)
默认情况下,扩展名为commands
,该函数的名称为命令名称。但是,@commands.command
装饰器有一个aliases
参数,您可以将其与Context.invoked_with
一起使用:
@bot.command(aliases=['hug', 'headpat'])
async def folder_name(ctx):
image = random.choice(os.listdir(ctx.invoked_with))
embed = discord.Embed(color = 0xff9e00)
chosen_one = discord.File(f"{dir}\\{image}", filename=image)
embed.set_image(url = f"attachment://{image}")
await ctx.send (embed = embed, file=chosen_one)
print(f"{image} sent in response to {ctx.author}.")
print(f"----------")
请注意,如果有人键入!folder_name
,该命令将执行,因此您必须添加如下检查:
if ctx.invoked_with == ctx.command.name:
#Do stuff
答案 2 :(得分:0)
我实际上建议使用链接代替文件/文件夹,因为它们不可靠,并且经常产生问题和代码中的错误,只是与discord.py不能很好地配合使用。 这是您所需命令的示例。
import discord
from discord.ext import commands
from discord import Embed
import random
client = commands.Bot(command_prefix=".")
headpat_image_links = ['https://media.tenor.com/images/ad8357e58d35c1d63b570ab7e587f212/tenor.gif', 'https://image.myanimelist.net/ui/xUjg9eFRCjwANWb4t4P8QbyfbVhnxa2QGYxoE7Brq1sHDVak6YxhLP3ZbNkkHm6GX9x8FWB1tWCapNyEqSltXa8wxAdwIERdrHGhYPilHJ8']
secret_headpat = random.choice(headpat_image_links)
@client.command()
async def headpat(ctx):
e=Embed(title="Headpats!", description="whatever description you want")
e.set_image(url=secret_headpat)
await ctx.send(embed=e, content=None)
client.run('TOKEN')
我已经检查了它,它有效就像一个吊饰;) 抱抱命令的概念相同。只需选择随机链接即可嵌入:)
PS : 确保您不使用任何imgur链接,imgur的API和链接一直处于混乱状态,并且不再与discord.py配合使用。好吧,它不再适用于嵌入式消息。只是不要使用它。 :>