如何让服务器的创建者选择新用户加入服务器的机器人消息将发送到哪里?
How to make so that the creator of the server chose where bot messages that the new user has joined the server will be sent?
我有这个代码:
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 命令将频道名称输入数据库(如果不是 link 你写的频道 id,名称仍然会被输入,这是因为 discord.TextChannel )
打印命令应该从数据库中获取频道名称并向那里发送一条消息,但是如果您只写它就不会出现
await channel.send ( 'ok')
然后在console中显示tuple没有send属性,如果第一个元素是从cortege中选择的
await channel [0].send('ok')
who 报告 str 没有发送属性。
我的情况应该怎么办?
也许您应该使用其他一些 PostgreSQL 命令?
您只获取频道名称,因此您需要使用客户端(是机器人吗?)为您发送消息,参见the docs:
channel = client.get_channel(12324234183172)
await channel.send('hello')
这里的频道不是名字,而是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))
我有这个代码:
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 命令将频道名称输入数据库(如果不是 link 你写的频道 id,名称仍然会被输入,这是因为 discord.TextChannel )
打印命令应该从数据库中获取频道名称并向那里发送一条消息,但是如果您只写它就不会出现
await channel.send ( 'ok')
然后在console中显示tuple没有send属性,如果第一个元素是从cortege中选择的
await channel [0].send('ok')
who 报告 str 没有发送属性。
我的情况应该怎么办?
也许您应该使用其他一些 PostgreSQL 命令?
您只获取频道名称,因此您需要使用客户端(是机器人吗?)为您发送消息,参见the docs:
channel = client.get_channel(12324234183172)
await channel.send('hello')
这里的频道不是名字,而是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))