我试图让我的机器人在加入时获取服务器和公会 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)
这是我的问题
由于我的代码具有名为“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)
答案 0 :(得分:0)
如果我理解正确,那么您希望机器人将 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))