discord.py:通过用户 DM 发送邀请

时间:2021-04-21 09:33:46

标签: python discord discord.py bots

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

它返回此错误:

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))

1 个答案:

答案 0 :(得分: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发送给会员并通过bot确认