【问题标题】:Discord.py on_message isnt a @bot.event?Discord.py on_message 不是@bot.event?
【发布时间】:2020-12-03 22:23:37
【问题描述】:
import datetime
import discord
from discord import message, ActivityType
from discord.ext import commands
import asyncio

intents = discord.Intents().all()
bot = commands.Bot(command_prefix='.', intents=intents)
guild = 'SwiftNetwork'
time = datetime.date.today()
badword = ['bw', 'bw1', 'bw2', 'bw3']


@bot.event
async def on_ready():
    print('Wir sind als {1} auf {0} eingeloggt Vers: {2}'.format(guild, bot.user.name, discord.__version__))
    channel = bot.get_channel(id=697572461629407305)
    await channel.send('Heute ist der {0}.{1}.{2}'.format(time.day, time.month, time.year))
    # await bot.channel.send('Beep Boop Beep, Roboter Angriff wird gestartet..')
    bot.loop.create_task(status_task())


@bot.command()
async def hello(ctx):
    await ctx.channel.send(f"Hello! {ctx.author.mention}")


@bot.command()
async def ping(ctx):
    await ctx.channel.send(round(bot.latency * 1000))


@bot.command()
async def poll(ctx, message, *args):
    # print(len(args) + 1)
    emoji = '1️⃣'
    pollem = discord.Embed(title='', description=f'{message}')
    context = []

    reactionx = ['1️⃣', '2️⃣', '3️⃣', '4️⃣']

    if len(args) >= 2:

        for r in range(0, len(args), 2):
            reaction = ['1️⃣', '2️⃣', '3️⃣', '4️⃣']

        for x in range(1, len(args), 2):
            context.append(args[x])

        for a in range(0, len(args), 2):
            pollem.add_field(name=' {1} {0}'.format(args[a], reaction[int(r / 2)]), value=context[int(a / 2)],
                             inline=False)

        ms = await ctx.channel.send(embed=pollem)

    else:
        await ctx.channel.send('Check Args')


async def status_task():
    async def status_task():
        while True:
            await bot.change_presence(activity=discord.Game('Hello'), status=discord.Status.online)
            await asyncio.sleep(3)
            await bot.change_presence(activity=discord.Game('Moin'), status=discord.Status.online)
            await asyncio.sleep(3)
            await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"), status=discord.Status.online)
            await asyncio.sleep(3)


@bot.event
async def on_message(ctx):
    if ctx.author.bot:
        return
    for word in ctx.content.split(' '):
        if word in badword:
            await ctx.delete()


问题出在最后一个事件中。 如果我这样运行它,ctx.delete() 将起作用,但上面的所有命令都不起作用。 如果我删除 @bot.event,命令会起作用,但 ctx.delete() 不会。

我想说我确定这是@bot.event 的原因,但在on_ready 我也在使用它并且它工作正常。 所以现在我真的没有在代码中看到任何问题。

【问题讨论】:

  • 也许尝试在所有 @bot.command() async def 之前处理它(在 on_ready 之后),我实际上不知道为什么,但我没有看到任何不把它放在那里的机器人,并且可能有一个原因
  • 我试过这个。结果相同,只有删除有效。

标签: python discord discord.py


【解决方案1】:

当您使用on_message 事件时,它会阻止其他命令工作。为了解决这个问题,您需要使用await bot.process_commands(message)。您需要将其添加到 on_message 事件代码的末尾。

有关bot.process_commands的更多信息,您可以查看API references

【讨论】:

  • 感谢您的指正,也感谢您的指正。 - 有时英语有些问题。再问一个小问题,有什么联系方式吗?看起来你对 discord.py 很满意,我还有另一个问题,但我不能在接下来的 70 分钟内把它贴在这里..
猜你喜欢
  • 2020-09-16
  • 2021-01-16
  • 2018-06-10
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2021-02-12
相关资源
最近更新 更多