首页 文章

使用Discord Bot创建服务器

提问于
浏览
2

如果我们查看discord dev文档中的 Guild Create 事件,我们会看到以下内容:
enter image description here

我有几个问题 . 首先,我不确定何时可以使用机器人帐户创建服务器 . 在"when a user is initially connecting"部分之后,我尝试将服务器创建放入 on_ready 函数中,如下所示:

import discord
import asyncio

import bot.client.getkey as _getkey
from bot.utils import get_owner

class ImABot(discord.Client):
    async def on_ready(self):
        ts = await self.create_server("test")
        inv = await self.create_invite(ts.default_channel)
        await self.send_message(get_owner()) #get owner simply gets the user who owns the bot, me.
Bot = ImABot()


Bot.run(_getkey.key())

但是,我收到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "/Users/edl/Desktop/ /Programming/Github/Democracy-Bot/demobot/client/client.py", line 22, in on_ready
    inv = await self.create_invite(ts.default_channel)
  File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 2628, in create_invite
    data = yield from self.http.create_invite(destination.id, **options)
AttributeError: 'NoneType' object has no attribute 'id'

我认为这意味着没有创建服务器 . 我希望有人可以告诉我有关何时创建服务器的信息,以及首先是否可行 . 谢谢!

1 回答

  • 0

    公会不再拥有默认 Channels ,因此最好避免使用它们 .

    要获得服务器的第一个创建 Channels ,最好的办法是使用

    discord.utils.get(new_server.channels, type=discord.ChannelType.text)
    

    或者如果你使用discord.py重写,那么

    new_server.text_channels[0]
    

    然后,您可以从该 Channels 创建邀请 .

相关问题