第一篇文章,我正在尝试升级我的 Discord 机器人!
目前机器人能够很好地执行 !decklist,但是当我出现在 if not message.attachments 中并尝试执行 !boh 命令时,机器人会使用 if 子句进行响应,并执行 !boh 命令.
在下面发布代码,非常感谢您的帮助!
import discord
from discord.ext import commands
from io import BytesIO
TOKEN = 'disc_token'
def run():
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents = intents)
#ON_READY
@bot.event
async def on_ready():
#SERVER = bot.get_guild(int(1156534000890953748))
bot_name = await bot.fetch_user(int(1155959555876991098))
await bot.tree.sync()
print(f'{bot_name}si è connesso a Discord')
#SENDS USER EMBED TO RESPOND WITH IMAGE.
@bot.command()
async def decklist (ctx):
"""Riceve l'immagine della Decklist e la invia per la conferma"""
embed = discord.Embed(
colour = discord.Colour.dark_magenta(),
title = ("Dreamborn"),
url = ("https://dreamborn.ink/")
)
embed.set_image(url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKCp5PneGn0VgoP-Hncj4AxiN-Ql2tqaKd6w&usqp=CAU")
await ctx.send(f"Mandami l'immagine della Decklist esportata da **Dreamborn!**", ephemeral=True)
await ctx.send(embed=embed)
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.author == bot.user:
return
if message.author.bot:
return
if not message.attachments:
#ignore (message=bot.command())
await message.author.send('Ciao! La tua lista non è **idonea**.\nMandami una **immagine!**')
pass
image_file = message.attachments[0]
image_bytes = await image_file.read()
liste = bot.get_channel(int(1156534533907288064))
istruzioni = bot.get_channel(int(1156626680794845234))
await liste.send('**' + message.author.display_name + '**')
await liste.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
await message.author.send('Oki doki, Decklist inviata! \nMetti la reazione al messaggio su' + istruzioni.mention + 'per essere Confermato! \nSe invece avevi già messo la reazione a Confermati, ignora!')
await bot.process_commands(message)
#REPLY WITH "HELLO"
@bot.command()
async def boh(ctx):
await ctx.send(f"ciao!")
bot.run(TOKEN)
run()
我只是想知道如何从 !decklist 命令中正确“退出”,以免在我使用其他命令时再次陷入 if 子句中。
我已经解决了,只需将
on_message
替换为 message = await bot.wait_for('message')
!
完整代码,以防您需要!你可以关闭这个话题了!
`import discord
from discord.ext import commands
from io import BytesIO
TOKEN = 'disc.token'
def run():
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents = intents)
#ON_READY
@bot.event
async def on_ready():
bot_name = await bot.fetch_user(int(bot_id))
await bot.tree.sync()
print(f'{bot_name}si è connesso a Discord')
#SENDS USER EMBED TO RESPOND WITH IMAGE.
@bot.command()
async def decklist (ctx):
"""Riceve l'immagine della Decklist e la invia per la conferma"""
embed = discord.Embed(
colour = discord.Colour.dark_magenta(),
title = ("Dreamborn"),
url = ("https://dreamborn.ink/")
)
embed.set_image(url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKCp5PneGn0VgoP-Hncj4AxiN-Ql2tqaKd6w&usqp=CAU")
await ctx.send(f"Mandami l'immagine della Decklist esportata da **Dreamborn!**", ephemeral=True)
await ctx.send(embed=embed)
#WAITS FOR USER INTERACTIONS, IF ATTACHMENTS IS TRUE, POSTS THE ATTACHMENT INTO A DEDICATED CHANNEL
message = await bot.wait_for('message')
if message.author == bot.user:
return
if message.author.bot:
return
if not message.attachments:
#ignore (message=bot.command())
await message.author.send('Ciao! La tua lista non è **idonea**.\nMandami una **immagine!**')
message = await bot.wait_for('message')
pass
image_file = message.attachments[0]
image_bytes = await image_file.read()
liste = bot.get_channel(int(1156534533907288064))
istruzioni = bot.get_channel(int(1156626680794845234))
await liste.send('**' + message.author.display_name + '**')
await liste.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
await message.author.send('Oki doki, Decklist inviata! \nMetti la reazione al messaggio su' + istruzioni.mention + 'per essere Confermato! \nSe invece avevi già messo la reazione a Confermati, ignora!')
bot.run(TOKEN)
run()`