使用函数名作为命令
use function name as a command
我这里有这个功能,它使用文件夹名称作为命令。现在仅有的文件夹是“hug”和“headpat”。因此,当我将 .hug
或 .headpat
发送到 Discord 时,它将进入该文件夹,抓取一张随机图像并 post 它。
@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
这是一个将您的代码作为命令执行的简单机器人。
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")
默认使用 commands
extension, the function's name is the command name. However, the @commands.command
decorator have a aliases
argument that you can use along with 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
我实际上建议使用 links 而不是 files/folders 因为他们 不可靠 并且经常 会在您的代码中产生问题 和错误,并且它们不能很好地与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')
我已经检查过了,效果很好 ;)
这与 hug 命令的概念相同。只需选择一个 random link 嵌入 :)
PS:
确保你没有使用任何 imgur links,imgur 的 API 和 links 已经过时了,不再适用于 discord.py。好吧,它不再适用于嵌入式消息。只是不要使用它。 :>
我这里有这个功能,它使用文件夹名称作为命令。现在仅有的文件夹是“hug”和“headpat”。因此,当我将 .hug
或 .headpat
发送到 Discord 时,它将进入该文件夹,抓取一张随机图像并 post 它。
@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
这是一个将您的代码作为命令执行的简单机器人。
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")
默认使用 commands
extension, the function's name is the command name. However, the @commands.command
decorator have a aliases
argument that you can use along with 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
我实际上建议使用 links 而不是 files/folders 因为他们 不可靠 并且经常 会在您的代码中产生问题 和错误,并且它们不能很好地与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')
我已经检查过了,效果很好 ;) 这与 hug 命令的概念相同。只需选择一个 random link 嵌入 :)
PS: 确保你没有使用任何 imgur links,imgur 的 API 和 links 已经过时了,不再适用于 discord.py。好吧,它不再适用于嵌入式消息。只是不要使用它。 :>