python - 如何获取公会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)
这是我的问题
- 第三行的'self.guild.id'是否适合获取公会ID?
- 我还没有弄清楚如何使用“公会频道”类,所以,有人可以通过更改第 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)
解决方案
如果我理解正确,那么您希望机器人将 Guild-ID、Channel-ID 和 Channel-Names 存储在您的数据库中。
on_guild_join事件具有参数guild。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))
推荐阅读
- google-cloud-data-fusion - 我可以在一个管道作业中运行两个独立的数据融合 cdap 管道吗?
- go - url中有端口号时无法获取私有go模块
- webpack - 如何在没有“~”的 sass 加载器中从 node_modules 导入样式表
- ios - Swift 中来自 String 和 Image 的数据有什么不同?
- cypress - 在柏树中计算总数
- php - Go和PHP中的sha1有不同的结果
- r - 在 dmetar 包中运行 gosh.diagnostics() 函数时发生错误
- r - 我可以使用 for 循环的并行版本并一起应用系列吗?
- android - Css溢出:滚动在移动设备上不起作用
- angular - 对 Amplify-Cognito 的请求是否绕过 Angular HTTP 拦截器