如何在Python中为Discord服务器创建只能使用一次的特定数量的邀请链接

llmtgqce  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(188)

我是Discord服务器的新手,我想创建一个只有我邀请的用户才能加入的专用Discord服务器。我读到了一些实现这一点的方法,但没有一个是我真正想要的。我正在考虑创建一个Discord应用程序,生成特定数量的邀请链接到我的服务器,这些链接只能使用一次。
这意味着,如果我想邀请50个人到我的Discord服务器,我需要创建50个只能使用一次的邀请链接,这样我就可以确保只有我邀请的人才能加入。我想把所有这些链接放在一个外部文本文件中,这样我以后就可以使用它们,并最终通过电子邮件将它们发送给其他人。换句话说,我不需要创建机器人。而是使用Python和discord.py模块在Discord之外实现所有这些功能。
我在www.example.com文档上看到了这个discord.py,它看起来像我需要的东西,但我真的不明白它是如何工作的。
我几乎只能在Discord服务器上找到关于如何创建机器人的教程,但这并不是我所需要的。有人能帮我吗?
非常感谢你提前!

qv7cva1a

qv7cva1a1#

import discord

token = 'bot_token_goes_here'
client = discord.Client()
number_of_links = input('How many links do you want to create? ') 

@client.event 
async def on_ready():
    g = client.guilds[guild_number goes here] # Choose the guild/server you want to use 
    c = g.get_channel(channel_id_goes_here) # Get channel ID
    invites = await discord.abc.GuildChannel.invites(c) # list of all the invites in the server

    while len(invites) < int(number_of_links):
        print('CREATING INVITES')
        for i in range(int(number_of_links)): # Create as many links as needed
            i = await discord.abc.GuildChannel.create_invite(c, max_uses=1, max_age=0, unique=True) # Create the invite link
        break

    print('Finished. Exiting soon...')
    exit()

client.run(token)
dphi5xsq

dphi5xsq2#

我对FedeCuci的脚本做了一些快速修改,有以下不同之处:

  • 脚本会询问您想要的 * 新 * 邀请的总数,而不是取决于以前创建的邀请
  • 脚本将新的邀请输出到控制台
  • 不引发异常
  • 与新的Intents API兼容
  • 一些附加调试输出

对于任何一个脚本,您都需要安装discord.py,然后使用python3运行脚本。请不要忘记根据需要更新令牌和ID,并通过Discord's developer portal设置/加入bot到您的目标服务器。它需要Create Instant InviteRead Messages/View Channels的权限

from discord.utils import get
import discord

token = 'bot-token-goes-here' # <-- fill this in
intents = discord.Intents.default()
intents.invites = True
client = discord.Client(intents=intents)
guild_id = guild-id-goes-here # <-- fill this in
number_of_links = input('How many links do you want to create? ')

@client.event
async def on_ready():
    print('Bot is up and running.')
    print(f'Logged in as {client.user}')

    g = client.get_guild(int(guild_id))
    print(f'Guild: {g.name}')
    c = g.get_channel(channel-id-goes-here) # <-- fill this in
    print(f'Channel: #{c}')
    invites = []

    print('CREATING INVITES')

    for i in range(int(number_of_links)):
        invite = await g.text_channels[0].create_invite(max_uses=1, max_age=0, unique=True)
        print(f'{invite}')

    print('Finished. Exiting soon...')
    await client.close()

client.run(token)

相关问题