如何获取公会ID和该公会中所有频道的ID



我试图让我的机器人获得服务器和公会id加入并将其保存到数据库中,但有些事情对我来说并不清楚,所以我问了一些问题。

下面是我为实现我的目标而写的初始代码,我说:

@Cog.listener()
async def on_guild_join(self, guild):
db.execute('INSERT OR IGNORE INTO war_guild (GuildID) VALUES (?)', self.guild.id)
self.guild = self.get_guild(db.record('SELECT GuildID FROM war_guild', *))
existing_text_channel = [(GuildChannel.name, GuildChannel.id) for '(#text channels)' in self.guild]
existing_voice_channel = [(GuildChannel.name, GuildChannel.id) for '(#voice channels)' in self.guild]
db.multiexec('INSERT OR IGNORE INTO channels VALUES (?, ?, text)', (channel_name, channel_id) for chan in existing_text_channel)
db.multiexec('INSERT OR IGNORE INTO channels VALUES (?, ?, voice)', (channel_name, channel_id) for chan in existing_voice_channel)

这是我的问题

  1. 是"self.guild。在第三行的Id是获取公会Id的好位置?
  2. 我还没有弄清楚如何与'guildchannel'类一起工作,所以,有人能告诉我如何通过改变第5行和第6行来做到这一点吗?

由于我的代码有名为'execute', 'multiexec'和'record'的自定义函数,我将向您展示我定义这些函数的部分:

from os.path import isfile
from sqlite3 import connect
DB_PATH = "./data/db/database.db"
BUILD_PATH = "./data/db/build.sql"
cxn = connect(DB_PATH, check_same_thread=False)
cur = cxn.cursor()
def record(command, *values):
cur.execute(command, tuple(values))
return cur.fetchone()
def execute(command, *values):
cur.execute(command, tuple(values))
def multiexec(command, valueset):
cur.executemany(command, valueset)

如果我理解正确,那么您希望bot在数据库中存储行会id,通道id和通道名称。

on_guild_join事件有参数guild。公会本身有id、channels、voice_channels、text_channels等参数。

所以你有你需要的一切。

下面的代码示例应该可以工作。
@Cog.listener()
async def on_guild_join(self, guild):
db.execute('INSERT OR IGNORE INTO war_guild (GuildID) VALUES (?)', guild.id)
for channel in guild.text_channels:
db.multiexec('INSERT OR IGNORE INTO channels VALUES (?, ?, text)', (channel.name, channel.id))
for channel in guild.voice_channels:
db.multiexec('INSERT OR IGNORE INTO channels VALUES (?, ?, voice)', (channel.name, channel.id))

最新更新