首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我应该这样在discord.py中使用‘discord.py’吗?

我应该这样在discord.py中使用‘discord.py’吗?
EN

Stack Overflow用户
提问于 2021-02-16 15:29:00
回答 1查看 176关注 0票数 0

我计划制作一个机器人来运行基于PvP的TRPG游戏,因此,对于每一个新游戏,我都计划为此制造一个新的不和谐服务器。因为为它设置舞台太繁琐了,(创建5个新频道、4+(玩家数量)新角色,并设置它们按预期工作等等),我搜索了bot的操作方式,并发现'on_guild_join‘事件侦听器可能是有用的。

由于我并不完全了解discord.py,所以我成功地构建了我所知道的序列的一部分,但是有些部分缺失了。有人能帮我填一下我留下的部分作为评论吗?

代码语言:javascript
复制
@Cog.listener()
    async def on_guild_join(self, guild):
        db.execute('INSERT INTO war_guild (GuildID) VALUES (?)', self.guild.id) # Is self.guild.id in here can get GuildID?
        guild = db.record('SELECT GuildID FROM war_guild', *)
        channel_id = #get channel id from only text channel that is created when the new channel is formed.
        db.execute('INSERT INTO channels VALUES (staff-room, ?)', channel_id)
        channel_name = ['server-gateway', 'random-chatroom', 'gameplay-map', 'cemetary', 'log']
        for chan in channel_name:
            await ctx.create_text_channel(chan)
            channel_id = #channel id that has been created from the line before
            db.execute('INSERT INTO channels VALUES (?,?)', chan, channel_id)
        role_name = ['GM', 'Player', 'Ghost', 'Spectator']
        for rol in role_name:
            await ctx.create_role(name=rol)
            role_id = #role id that has been created from the line before
            db.execute('INSERT INTO roles VALUES (?,?)', rol, role_id)
        # Set role permission and channel permission 

而且,由于我不知道如何确切地使用'on_guild_join',我认为这个部分的位置不够好,不能让下面的代码正常工作。我会给我的代码的巴斯特林链接,我认为可能是更好的地方放置,如果标题是它的相应的位置,它会更好的地方?

我的代码pastebin链接:

lib\db\__init__.py:https://pastebin.com/H5rMyHkC

lib\db\db.py:https://pastebin.com/KKyQNf3R

lib\bot\__init__.py:https://pastebin.com/FYK14uHt

launcher.py:https://pastebin.com/arT4v6YJ

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-17 14:10:33

您应该查看权限类 (有关权限标志名称的列表)、以成员或角色为目标的discord.TextChannel 权限的coroutine方法以及权限。

所以看起来是这样:

代码语言:javascript
复制
    @Cog.listener()
    async def on_guild_join(self, guild):
        db.execute('INSERT INTO war_guild (GuildID) VALUES (?)', self.guild.id) # Is self.guild.id in here can get GuildID?
        guild = db.record('SELECT GuildID FROM war_guild', *)
        channel_id = #get channel id from only text channel that is created when the new channel is formed.
        db.execute('INSERT INTO channels VALUES (staff-room, ?)', channel_id)
        channel_name = ['server-gateway', 'random-chatroom', 'gameplay-map', 'cemetary', 'log']
        channels = []
        roles = []
        for chan in channel_name:
            channels.append(await ctx.create_text_channel(chan))
            db.execute('INSERT INTO channels VALUES (?,?)', chan, channels[-1].id)
        role_name = ['GM', 'Player', 'Ghost', 'Spectator']
        for rol in role_name:
            roles.append(await ctx.create_role(name=rol))
            await roles[-1].edit(permissions=Permissions(Permissions.read_messages, Permissions.send_messages)
            db.execute('INSERT INTO roles VALUES (?,?)', rol, roles[-1].id)
        for chan in channels:  # chan is a TextChannel object
            for rol in roles:
                await chan.set_permissions(rol, read_messages=True, send_messages=False)

创建角色、发送消息、创建通道等方法总是返回所创建的对象,这是非常有用的。对于我添加的最后一个循环,您应该更改read_messages=True,send_messages=False,.可以使用role.name区分for rol in roles循环中的所有角色,也可以通过channel.name对通道进行区分。

此外,一旦您拥有了创建的角色,就可以立即通过与channel_name或role_name列表相同顺序的权限列表将其权限设置为所需的权限。

我不认为更改每个角色的每个通道的权限的最后一个循环是必要的,您应该能够通过通过role.edit设置每个角色的权限来完成所需的一切。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66227328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档