Python 如何使服务器的创建者选择新用户加入服务器的bot消息的发送位置?
我有以下代码:Python 如何使服务器的创建者选择新用户加入服务器的bot消息的发送位置?,python,postgresql,discord,bots,discord.py-rewrite,Python,Postgresql,Discord,Bots,Discord.py Rewrite,我有以下代码: class member_greeting(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_guild_join(self, ctx): pass @commands.command() async def greet(self, ctx, channel: discord.TextChannel): guild_c
class member_greeting(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_guild_join(self, ctx):
pass
@commands.command()
async def greet(self, ctx, channel: discord.TextChannel):
guild_channel_id = ctx.message.guild.id
cursor.execute(f'UPDATE public."prefixDB" SET channel_for_greet=\'{channel}\' WHERE guild_id = \'{guild_channel_id}\';')
conn.commit()
@commands.command()
async def print(self, ctx, channel: discord.TextChannel = None):
guild_channel_id = ctx.message.guild.id
cursor.execute(f'SELECT channel_for_greet FROM public."prefixDB" WHERE guild_id = \'{guild_channel_id}\';')
channel = cursor.fetchone()
await channel[0].send('ok')
def setup(bot):
bot.add_cog(member_greeting(bot))
greet命令将频道的名称输入到数据库中(如果您写入的id不是指向频道的链接,而是名称,这是因为discord.TextChannel)
print命令应该从数据库中获取通道的名称并在那里发送消息,但是如果您只是简单地编写,它就不在那里
await channel.send ( 'ok')
然后在控制台中,它显示tuple没有send属性,并且如果第一个元素是从cortege中选择的
await channel [0].send('ok')
谁报告str没有send属性
在我的情况下该怎么办
也许您应该使用其他PostgreSQL命令
您只获取频道名称,因此需要使用客户端(是bot吗?)为您发送消息,请参阅:
channel=client.get_频道(12324234183172)
等待频道。发送('你好')
在这里,通道不是名称,而是ID。如果可能的话,我会在数据库中存储通道ID而不是名称,因为名称很容易更改。描述如何通过ID获取频道名称,但这实际上是一个解决方法。谢谢大家,我检查了代码,发现了错误,并更正了它们。 以下是正确的代码:
class member_greeting(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_member_join(self, ctx):
join_guild_id = ctx.guild.id
cursor.execute(f'SELECT channel_for_greeting FROM public."prefixDB" WHERE guild_id = \'{join_guild_id}\';')
chan = cursor.fetchone()
conn.commit()
channel = self.bot.get_channel(chan[0]) #the motorcade returns to us therefore it is necessary to choose a position
await channel.send('hi')
@commands.command()
async def greet(self, ctx, channel):
guildid = ctx.guild.id
cursor.execute(f'UPDATE public."prefixDB" SET channel_for_greeting = \'{channel}\' WHERE guild_id = \'{guildid}\';')
conn.commit()
def setup(bot):
bot.add_cog(member_greeting(bot))
您只获取通道名称,而不是将发送消息的bot或类。我建议您查看一下代码的整体结构,以了解哪些信息被发送和存储在哪里,因为您似乎在途中的某个地方迷路了。这当然很好,我们每个人都会遇到这种情况,但退一步思考一下你在做什么是很好的,但是如何获得频道或地狱的名称,以便以后可以使用它发送消息?
channel[0]
是频道的名称