【问题标题】:Discord delete message whitout picture attached to the message?不和谐删除没有图片附加到消息的消息?
【发布时间】:2020-10-24 17:26:07
【问题描述】:

我使用此代码删除消息,只允许在特定频道发布图片。 现在我正在考虑在消息中添加图片时允许使用文本,以便人们可以描述他们需要帮助的内容。

@client.event
async def on_message(message):
    if str(message.channel) == "DPS" and message.content != "":
        await message.channel.purge(limit=1)

这段代码还有一个奇怪的错误,我无法使用机器人中的任何命令

马库斯

【问题讨论】:

  • 您遇到了什么错误?你应该提供更多细节
  • 奇怪的是我没有收到任何错误

标签: python discord discord.py


【解决方案1】:

您可以使用message.attachments,它将返回一个列表:

@client.event
async def on_message(message):
    formats = ['jpg', 'png', 'gif', 'svg']
    attachments = [f for f in message.attachments if f.filename.split('.')[-1] in formats]
    if message.channel.name == 'DPS' and not attachments:
        await message.delete()

formatsattachments 用于确保用户只能发送图像。
如果您只使用message.attachments,用户可以发送其他文件(.txt、.pdf、.py、... )。

【讨论】:

  • 谢谢,我试试看
【解决方案2】:

所以我现在得到了这个代码,但我不能使用!lotto (num),所有命令都一样,这是一个 strande。而且完全没有错误。

# Importera Discord
import asyncio
import random

import discord
from discord import Embed
from discord.ext import commands

prefix = "!"
bot = commands.Bot(command_prefix = prefix)
token = 'token nr, deleted now'

#On Startup


@bot.event
async def on_ready():
    print ("Online")



#Events
#Random Dragning 1 till skrivet nummer efter kommando


@bot.command()
async def test(ctx):
    embed=discord.Embed(title="Lotto Kommandon", description="Välj kommando följt av antal deltagare, tex !lotto 500", color=0xeff542)
    embed.add_field(name="En Vinnare", value="!lotto", inline=False)
    embed.add_field(name="Tre Vinnare", value="!lotto3", inline=False)
    embed.add_field(name="Fem Vinnare", value="!lotto5", inline=False)
    embed.set_thumbnail(url= "http://www.livgardet.se/ext/planetstyles/h2o/store/livgardet-nya-utan-esologga.png")
    await ctx.send(embed=embed)


###########################


@bot.command()
async def lotto(ctx, num):
    embed = discord.Embed(title="Livgardets Lotteri", color=0xeff542)
    try:
        arg = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare", value=(arg), inline=True)
        embed.set_thumbnail(url= "http://www.livgardet.se/ext/planetstyles/h2o/store/livgardet-nya-utan-esologga.png")
    except ValueError:
        return await ctx.send("Endast hela nummer")
    else:
        return await ctx.send(embed=embed)


#########################


@bot.command()
async def lotto3(ctx, num):
    embed = discord.Embed(title="Livgardets Lotteri", color=0xeff542)
    try:
        arg1 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 1", value=(arg1), inline=False)
        arg2 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 2", value=(arg2), inline=False)
        arg3 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 3", value=(arg3), inline=False)
        embed.set_thumbnail(url= "http://www.livgardet.se/ext/planetstyles/h2o/store/livgardet-nya-utan-esologga.png")
    except ValueError:
        return await ctx.send("Endast hela nummer")
    else:
        return await ctx.send(embed=embed)


######################

@bot.command()
async def lotto5(ctx, num):
    embed = discord.Embed(title="Livgardets Lotteri", color=0xeff542)
    try:
        arg1 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 1", value=(arg1), inline=True)
        arg2 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 2", value=(arg2), inline=True)
        arg3 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 3", value=(arg3), inline=True)
        arg4 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 4", value=(arg4), inline=True)
        arg5 = random.randint(1, int(num)) 
        embed.add_field(name="Vinnare 5", value=(arg5), inline=True)
        embed.set_thumbnail(url= "http://www.livgardet.se/ext/planetstyles/h2o/store/livgardet-nya-utan-esologga.png")
    except ValueError:
        return await ctx.send("Endast hela nummer")
    else:
        return await ctx.send(embed=embed)


####################################################################################################################################



@bot.event
async def on_message(message):
    formats = ['jpg', 'png', 'gif', 'svg']
    attachments = [f for f in message.attachments if f.filename.split('.')[-1] in formats]
    if message.channel.name == 'bild' and not attachments:
        await bot.process_commands(message)


bot.run(token)

【讨论】:

  • @bot.event async def on_message(message): formats = ['jpg', 'png', 'gif', 'svg'] attachments = [f for f in message.attachments if f.filename.split('.')[-1] in formats] if message.channel.name == 'bild' and not attachments: await message.delete() await bot.process_commands(message) 工作!
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 2018-05-02
  • 2012-07-20
  • 2020-12-15
  • 2021-09-20
  • 2021-05-29
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多