【问题标题】:Discord Bot in python creating an invite link and dming it to a userpython中的Discord Bot创建邀请链接并将其发送给用户
【发布时间】:2020-12-27 23:45:27
【问题描述】:

我正在 Python(3.7.3) 中制作 Discord Bot,我想向用户发送 dm,并带有服务器的邀请链接。这是我的代码:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix=config.get_config, description='Thou Holy Bot')

@bot.command(name='dm',pass_context=True)
async def dm(ctx, *, argument):
    <code here>

我有一个 discord.member.Member() 类。我想创建一个频道的邀请链接(如果需要,有一个频道对象)并将其发送给用户。任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 您的问题是什么?请让您的问题显而易见,以便提供帮助。确保您阅读了How to ask?
  • 你想如何指定用户?您的机器人只能向与其共享至少一个允许成员互相发送消息的公会的人发送消息。
  • @YoussofH 我有一个随机踢命令,我想向被踢的随机人发送带有邀请链接的 dm。感谢 Patrick Haugh,我现在意识到这是不可能的。感谢您的帮助!
  • @ScottBot10 你能不能重新排序过程并发送链接然后踢?所以你仍然可以在用户被踢之前 PM 用户?还是您希望这些操作是分开的?
  • @Antry 可以,但我仍然不确定如何创建邀请链接。

标签: python discord discord.py


【解决方案1】:

感谢所有帮助我的人,我找到了解决方案。

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!', description='Bot')

@bot.command(name='dm',pass_context=True)
async def dm(ctx, *argument):
    #creating invite link
    invitelink = await ctx.channel.create_invite(max_uses=1,unique=True)
    #dming it to the person
    await ctx.author.send(invitelink)

这里是文档create_invite documentation

【讨论】:

    【解决方案2】:

    您可以只手动创建一个永不过期的邀请链接,然后在机器人执行踢之前在您发送给被踢的人的 DM 中使用所述邀请链接。

    【讨论】:

    • 我每次都想要一个独一无二的,这样人们就无法重复使用它们。我找到了解决方案,不过还是谢谢。我已经回答了我的问题。
    【解决方案3】:

    如果您想在 cog 中执行此操作:

         import discord
         from discord.ext import commands
         class Information(commands.Cog):
    
         def __init__(self,bot):
         self.bot = bot
    
         @commands.command(brief='A one time server invite',pass_context=True)
         async def serverinvite(self, ctx):
         invitelink = await ctx.channel.create_invite(max_age = 90, max_uses=1, unique=True)
         await ctx.send(invitelink)
    

    【讨论】:

    • 感谢您的回答。包括解释 cmets 中发生的事情的信息和/或代码之外的叙述可能对提问者有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 2021-12-17
    • 2019-10-19
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2020-07-13
    相关资源
    最近更新 更多