因此,我试图让我的机器人在每次对消息做出反应时都在类别内创建一个文本通道,类似于票务机器人。
我当前收到错误AttributeError: 'int' object has no attribute 'id'
,因为当我从数据库中获取ID时,它变成了tuple
,而guild.create_text_channel('name' category=category)
(类别部分)仅接受整数。
这是该命令的全部代码:
emojis = ['\U0001F4E9', '\U000023EF']
class Changelog(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print('Application is loaded')
@commands.group(invoke_without_command=True)
async def application(self, ctx):
embed = discord.Embed(title="Application Commands",
description="Channel: <#channel>", color=0)
await ctx.send(embed=embed)
@application.command()
async def channel(self, ctx, channel: discord.TextChannel):
if ctx.message.author.guild_permissions.administrator:
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(
f'SELECT channel_id FROM application WHERE guild_id = {ctx.guild.id}')
result = cursor.fetchone()
if result is None:
sql = ('INSERT INTO application(guild_id, channel_id) VALUES(?,?)')
val = (ctx.guild.id, channel.id)
await ctx.send(f'Message has been sent and channel has been set to {channel.mention}')
elif result is not None:
sql = ('UPDATE application SET channel_id = ? WHERE guild_id = ?')
val = (channel.id, ctx.guild.id)
await ctx.send(f'Message has been sent and channel has been updated to {channel.mention}')
cursor.execute(sql, val)
db.commit()
youtube = ':play_pause:'
staff = ':envelope_with_arrow:'
embed = discord.Embed(title="ReefCraft Applications", color=0)
embed.add_field(
name="** **", value=f"{youtube} YouTube Application\n\n{staff} Staff Application", inline=False)
embed.add_field(name="\n\nInformation",
value="Reacting to one of the emotes will create a new text-channel, where you will write your applicaiton!")
reaction_message = await channel.send(embed=embed)
for emoji in emojis:
await reaction_message.add_reaction(emoji)
cursor.execute(
f'SELECT category_id FROM application WHERE guild_id = {ctx.guild.id}')
category_result = cursor.fetchone()
category = await ctx.guild.create_category('Applications')
if category_result is None:
sqql = (
'INSERT INTO application(guild_id, category_id) VALUES(?, ?)')
values = (ctx.guild.id, category.id)
await ctx.send(f'Category has been set to {category.id}')
if category_result is not None:
sqql = (
'UPDATE application SET category_id = ? WHERE guild_id = ?')
values = (category.id, ctx.guild.id)
await ctx.send(f'Category has been updated to {category.id}')
cursor.execute(sqql, values)
db.commit()
cursor.close()
db.close()
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
emoji = reaction.emoji
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f'SELECT channel_id FROM application WHERE 1')
channel = cursor.fetchone()
channel_id = self.client.get_channel(int(channel[0]))
cursor.execute('SELECT category_id FROM application WHERE 1')
category = cursor.fetchone()
category_id = int(category[0])
guild = user.guild
if user.bot:
return
if emoji == "\U0001F4E9":
await channel_id.send("You clicked the Staff Application")
await guild.create_text_channel("Staff", category=category_id)
elif emoji == "\U000023EF":
await channel_id.send("You clicked the Youtube Application")
else:
return
def setup(client):
client.add_cog(Changelog(client))