【问题标题】:discord.py: Sending invite through user DMsdiscord.py:通过用户 DM 发送邀请
【发布时间】:2021-07-15 11:39:27
【问题描述】:

所以我写了这个命令,我认为它会向被标记的成员发送邀请,但显然我错过了一些东西。

它返回此错误:

Traceback (most recent call last):
 
  File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 902, in invoke
     await ctx.command.invoke(ctx)
  File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 864, in invoke
     await injected(*ctx.args, **ctx.kwargs)
   File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
     raise CommandInvokeError(exc) from exc
: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Invite' object has no attribute 'ctx' 

谢谢。

import discord
from discord.ext    import commands

class Invite(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(pass_context=True)
    async def invite(self, ctx, member: discord.Member, *argument):
        link = await ctx.channel.create_invite(max_uses=1, unique=True)
        await member.send(link)
        await ctx.send(f'{member.name} has received an invite.')
 

def setup(client):
    client.add_cog(Invite(client))

【问题讨论】:

  • 您的问题到底是什么。它不做它应该做的事吗?它会抛出错误吗?请详细说明。
  • 我已经编辑了我的问题,很抱歉不清楚
  • 请包含完整的回溯
  • @mousetail 这可以吗,或者您希望我从自己的计算机上运行它并发布完整的回溯,因为它在 Heroku 上运行?
  • 这看起来不像是完整的回溯,尝试运行heroku logs -n 1500 来获取整个内容,或者尝试调用命令并获取完整的回溯

标签: python discord discord.py bots


【解决方案1】:

我猜你必须以其他方式请求邀请。

试试以下方法:

@commands.command(pass_context=True)
async def invite(self, ctx, member: discord.Member, total: int = None):
    guild = self.client.get_channel(ctx.channel.id)
    link = await guild.create_invite(max_uses=1 if total is not None else total, unique=True)
    await member.send(link)
    await ctx.send(f'{member.name} has received an invite.')

我们做了什么?

  • 获取channel.id并将其定义为guild
  • 创建一个到 guild 的邀请,最大。 1 用途和属性为unique
  • 发送link给会员,机器人确认

【讨论】:

  • 非常感谢!抱歉回复晚了。这解决了问题,再次感谢。
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 2021-04-16
  • 2021-12-24
  • 2019-06-04
  • 2021-04-26
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多