Python 如何加入服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37689289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:48:26  来源:igfitidea点击:

How to join a server?

pythondiscorddiscord.py

提问by Laxsnor

I'm trying to setup a discord bot with python. I have a pre-existing discord server that I would like the bot to join, but I'm having a hard time doing so.

我正在尝试用 python 设置一个不和谐的机器人。我有一个预先存在的不和谐服务器,我希望机器人加入,但我很难这样做。

import discord
import asyncio
import logging

logging.basicConfig(level=logging.INFO)

client = discord.Client()

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    print(client)


@client.event
async def on_message(message):
    print(message)
    if message.content.startswith('!test'):
        counter = 0
        tmp = await client.send_message(message.channel, 'Calculating messages...')
        async for log in client.logs_from(message.channel, limit=100):
            if log.author == message.author:
                counter += 1

        await client.edit_message(tmp, 'You have {} messages.'.format(counter))
    elif message.content.startswith('!sleep'):
        await asyncio.sleep(5)
        await client.send_message(message.channel, 'Done sleeping')

client.run('token')

This is essentially the basic discord.py script as given on the GitHub page. However, I can't seem to figure out how to have it actually join my server. When inserting this line into the on_readyfunction:

这本质上是 GitHub 页面上给出的基本 discord.py 脚本。但是,我似乎无法弄清楚如何让它真正加入我的服务器。将此行插入on_ready函数时:

server = await client.accept_invite('instant-invite-code')

with "instant-invite-code" replaced with my actual instant invite code (I tried both discord.gg/code and code), I get

将“即时邀请代码”替换为我实际的即时邀请代码(我尝试了 discord.gg/code 和代码),我得到

discord.errors.Forbidden: FORBIDDEN (status code: 403): Bots cannot use this endpoint

Logging does actually work; I get output with my username and id. My bot is registered with the discord API and I already have a token.

日志确实有效;我得到了我的用户名和 ID 的输出。我的机器人已在 Discord API 中注册,并且我已经有一个令牌。

回答by Elthan

I had some trouble with this as well. What you need to do is:

我也遇到了一些麻烦。你需要做的是:

  1. Go to the Discord developer pages(login if you haven't).
  2. Go to the application with the bot you want to add to your channel.
  3. Copy the Client/Application ID.
  4. Go to https://discordapp.com/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE&scope=bot&permissions=0< You can set permissions for the bot here. Permissions can be calculated here.
  5. Select server and click authorize.
  1. 转到Discord 开发人员页面(如果还没有,请登录)。
  2. 转到包含您要添加到频道的机器人的应用程序。
  3. 复制客户端/应用程序 ID。
  4. 转到https://discordapp.com/oauth2/authorize?client_id=CLIENT_ID_GOES_HERE&scope=bot&permissions=0< 您可以在此处设置机器人的权限。可以在此处计算权限
  5. 选择服务器并单击授权。

Your bot will now be a member of the server and will respond to commands you give it. Ex. !test in the code you have given.

您的机器人现在将成为服务器的成员,并将响应您给它的命令。前任。!test 在您提供的代码中。

EDIT: You can now use the permissions link (1) to generate the entire URL needed.

编辑:您现在可以使用权限链接 ( 1) 生成所需的整个 URL。

回答by Johnystar

I suggest editing the code like this:

我建议像这样编辑代码:

    @client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('Invite: https://discordapp.com/oauth2/authorize?client_id={}&scope=bot'.format(client.user.id))
    print('------')

I think this is the best and easiest solution. It works for me.

我认为这是最好和最简单的解决方案。这个对我有用。

EDIT: Discord actually made their own OAuth2 url generator, so use that: https://discordapp.com/developers/tools/oauth2-url-generator

编辑:Discord 实际上制作了自己的 OAuth2 url 生成器,因此请使用:https: //discordapp.com/developers/tools/oauth2-url-generator