如何使服务器的创建者选择将新用户已加入服务器的漫游器消息发送到何处?

问题描述

我有代码

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,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')

然后在控制台中,它显示元组不具有send属性,并且如果从提示中选择了第一个元素

await channel [0].send('ok')

谁报告str没有发送属性

我该怎么办?

也许您应该使用其他一些Postgresql命令?

in pgAdmin 4

解决方法

您仅获取频道名称,因此需要使用客户端(是机器人吗?)为您发送消息,请参见the docs

channel = client.get_channel(12324234183172)
await channel.send('hello')

在这里,通道不是名称,而是ID。如果可能的话,我会将通道的ID而不是名称存储在数据库中,因为名称很容易更改。 This answer描述了如何通过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))