Python 如何使discord bot适应其他服务器?
对于我的discord机器人,我已经硬编码了成员离开和成员加入的通道id。如果我想发出一个命令,使其适应其他服务器,例如,当他们这样做时-创建_join,它将在该频道中创建一个成员_join,我在哪里可以获得更多信息 我知道我要求的太多了,但请给我一个例子,说明我如何做到这一点?很多人都推荐我使用postgresql。 代码:Python 如何使discord bot适应其他服务器?,python,python-3.x,postgresql,discord,Python,Python 3.x,Postgresql,Discord,对于我的discord机器人,我已经硬编码了成员离开和成员加入的通道id。如果我想发出一个命令,使其适应其他服务器,例如,当他们这样做时-创建_join,它将在该频道中创建一个成员_join,我在哪里可以获得更多信息 我知道我要求的太多了,但请给我一个例子,说明我如何做到这一点?很多人都推荐我使用postgresql。 代码: 我建议您使用json文件存储所有通道ID,如下所示: { "guild_id_here": { "join_chan
我建议您使用json文件存储所有通道ID,如下所示:
{
"guild_id_here": {
"join_channel": joinchannel_id,
"leave_channel": leave_channel_id
}
}
您可以使用来读取和写入文档。最后,您需要的是一个数据库来存储这些通道id值。另一个答案是JSON,但如果您想添加bot并在许多服务器中使用此功能,最好使用像SQL这样的真实数据库 考虑到discord.py使用异步接口,最好的启动库是aiosqlite(访问) 在数据库中存储这些通道id的简单Python代码如下:
async def set_channel(self, ctx, channel:discord.TextChannel):
database = await aiosqlite.connect(database_path)
cursor = await database.cursor()
sql = ("UPDATE welcome SET welcome_channel_id = ? WHERE guild_id = ?")
val = (channel.id, ctx.guild.id)
await cursor.execute(sql, val)
await database.commit()
await cursor.close()
await database.close()
这个代码示例已经告诉您,您已经用创建的表设置了数据库,但是只要有一点点知识,您就应该能够修改这个细节。我需要下载什么?我需要下载sqlite吗?您需要安装aiosqlite库(py-m pip install aiosqlite)。如果您想在数据库中导航,可以使用DB Browser for SQLite()。第一步是创建数据库并添加要使用的表。这可以手动完成。在数据库浏览器中。
async def set_channel(self, ctx, channel:discord.TextChannel):
database = await aiosqlite.connect(database_path)
cursor = await database.cursor()
sql = ("UPDATE welcome SET welcome_channel_id = ? WHERE guild_id = ?")
val = (channel.id, ctx.guild.id)
await cursor.execute(sql, val)
await database.commit()
await cursor.close()
await database.close()