英文:
How to Check discord member roles
问题
Here's the translated content:
我有一个问题涉及到我的Python代码的一部分,我创建了一个Discord机器人来向用户显示游戏余额,他们有三种不同的角色,也就是三种不同的Discord角色。
当我使用!payment命令时,我想检查他们的角色并显示他们的余额(现在有很多记录,我想要整理它)。
elif message.content.startswith('!payment'):
await message.add_reaction("✅")
if not any(role.name in ['Guild Master', 'Calculator', 'Programmer'] for role in message.author.roles):
await message.channel.send("您没有使用此命令的权限")
return
c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
data = c.fetchall()
async def send_embeds(message, data):
embed = discord.Embed(title="信息", description="**所有用户的详细信息。**", colour=discord.Colour(int(custom_color, 16)))
field_count = 0
for keys in data:
tag, balance, date_time = keys
f_p_c = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(name=':pencil:___________________________', value=f":id:{tag}\n ``` 余额 : {f_p_c}\n ```", inline=False)
field_count += 1
if field_count >= 25:
await message.channel.send(embed=embed)
embed = discord.Embed(title="信息", description="所有用户的详细信息。", colour=discord.Colour(int(custom_color, 16)))
field_count = 0
if field_count > 0:
await message.channel.send(embed=embed)
await send_embeds(message, data)
process_payment = False
我无法将他们的角色放入我的数据库,我想编写程序来提取他们的角色。
英文:
I'm have a problem with a part of my python code , I created a discord bot for showing the balances of a game to users they are 3 type of people I mean 3 different discord role
when i use !payment i want check their role and show their balances (right now there is alot of records i want to make it clean)
elif message.content.startswith('!payment'):
await message.add_reaction("✅")
if not any(role.name in['Guild Master','Calculator','Programmer'] for role in message.author.roles):
await message.channel.send("You don't have permission to use this command")
return
c.execute('SELECT Tag,Balance,Date_Time FROM dbr1')
data=c.fetchall()
async def send_embeds(message,data):
embed = discord.Embed(title="Information", description="**Details for all users.**", colour=discord.Colour(int(custom_color, 16)))
field_count = 0
for keys in data:
tag, balance, date_time = keys
f_p_c = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(name=f':pencil:___________________________', value=f":id:{tag}\n ``` Balance : {f_p_c}\n ```", inline=False)
field_count += 1
if field_count >= 25:
await message.channel.send(embed=embed)
embed = discord.Embed(title="Information", description="details for all users.", colour=discord.Colour(int(custom_color, 16)))
field_count=0
if field_count>0:
await message.channel.send(embed=embed)
await send_embeds(message, data)
process_payment=False
I cant put their roles in my database i want program extract their roles
答案1
得分: 0
以下是代码的翻译部分:
要提取 Discord 用户的角色并将它们包含在您的数据库中,您可以按如下方式修改您的代码:
elif message.content.startswith('!payment'):
await message.add_reaction("✅")
allowed_roles = ['Guild Master', 'Calculator', 'Programmer']
user_roles = [role.name for role in message.author.roles]
if not any(role in allowed_roles for role in user_roles):
await message.channel.send("您没有权限使用此命令")
return
c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
data = c.fetchall()
async def send_embeds(message, data):
embed = discord.Embed(title="信息", description="所有用户的详细信息。", color=discord.Colour(int(custom_color, 16)))
field_count = 0
for keys in data:
tag, balance, date_time = keys
f_p_c = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(name=':pencil:___________________________', value=f":id:{tag}\n 余额 : {f_p_c}\n", inline=False)
field_count += 1
if field_count >= 25:
await message.channel.send(embed=embed)
embed = discord.Embed(title="信息", description="所有用户的详细信息。", color=discord.Colour(int(custom_color, 16)))
field_count = 0
if field_count > 0:
await message.channel.send(embed=embed)
await send_embeds(message, data)
process_payment = False
请注意,上述代码是根据您提供的信息进行了翻译,其中一些内容可能需要根据您的实际情况进行调整。
英文:
To extract the roles of the Discord users and include them in your database, you can modify your code as follows:
elif message.content.startswith('!payment'):
await message.add_reaction("✅")
allowed_roles = ['Guild Master', 'Calculator', 'Programmer']
user_roles = [role.name for role in message.author.roles]
if not any(role in allowed_roles for role in user_roles):
await message.channel.send("You don't have permission to use this command")
return
c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
data = c.fetchall()
async def send_embeds(message, data):
embed = discord.Embed(title="Information", description="Details for all users.", colour=discord.Colour(int(custom_color, 16)))
field_count = 0
for keys in data:
tag, balance, date_time = keys
f_p_c = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(name=f':pencil:___________________________', value=f":id:{tag}\n Balance : {f_p_c}\n", inline=False)
field_count += 1
if field_count >= 25:
await message.channel.send(embed=embed)
embed = discord.Embed(title="Information", description="details for all users.", colour=discord.Colour(int(custom_color, 16)))
field_count = 0
if field_count > 0:
await message.channel.send(embed=embed)
await send_embeds(message, data)
process_payment = False
答案2
得分: 0
在修复格式并纠正一些语法错误后(基于@hossein-dahaei的答案),您可以看到已校正的代码。它使用了Discord中的多个嵌入功能,将多个嵌入捆绑在单个消息中。
if message.content.startswith('!payment'):
await message.add_reaction("✅")
allowed_roles = ['Guild Master', 'Calculator', 'Programmer']
user_roles = [role.name for role in message.author.roles]
if not any(role in allowed_roles for role in user_roles):
await message.channel.send("You don't have permission to use this command")
return
c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
data = c.fetchall()
embeds = []
embed = discord.Embed(
title="Information",
description="Details for all users.",
colour=discord.Colour(int(custom_color, 16))
)
embeds.append(embed)
field_count = 0
for keys in data:
tag, balance, date_time = keys
formatted_balance = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(
name=f':pencil:___________________________',
value=f":id:{tag}\n Balance : {formatted_balance}\n",
inline=False
)
field_count += 1
if field_count >= 25:
embeds.append(embed)
embed = discord.Embed(
title="Information",
description="details for all users.",
colour=discord.Colour(int(custom_color, 16))
)
field_count = 0
if len(embeds) >= 10:
await message.channel.send(embeds=embeds)
embeds = []
field_count = 0
# If we didn't reach 10 embeds, and the
# other send branch is not run, send the rest of the embeds
if len(embeds) > 0:
await message.channel.send(embeds=embeds)
process_payment = False
英文:
After fixing formatting and correcting some syntax (based on the answer by @hossein-dahaei), you can see the corrected code. It uses the multiple embeds functionality present within Discord to bundle multiple embeds together in a single message.
if message.content.startswith('!payment'):
await message.add_reaction("✅")
allowed_roles = ['Guild Master', 'Calculator', 'Programmer']
user_roles = [role.name for role in message.author.roles]
if not any(role in allowed_roles for role in user_roles):
await message.channel.send("You don't have permission to use this command")
return
c.execute('SELECT Tag, Balance, Date_Time FROM dbr1')
data = c.fetchall()
embeds = []
embed = discord.Embed(
title="Information",
description="Details for all users.",
colour=discord.Colour(int(custom_color, 16))
)
embeds.append(embed)
field_count = 0
for keys in data:
tag, balance, date_time = keys
formatted_balance = '{:,.0f}'.format(balance).replace(',', '.')
embed.add_field(
name=f':pencil:___________________________',
value=f":id:{tag}\n Balance : {formatted_balance}\n",
inline=False
)
field_count += 1
if field_count >= 25:
embeds.append(embed)
embed = discord.Embed(
title="Information",
description="details for all users.",
colour=discord.Colour(int(custom_color, 16))
)
field_count = 0
if len(embeds) >= 10:
await message.channel.send(embeds=embeds)
embeds = []
field_count = 0
# If we didn't reach 10 embeds, and the
# other send branch is not run, send the rest of the embeds
if len(embeds) > 0:
await message.channel.send(embeds=embeds)
process_payment = False
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论