我的 Discord 機器人有一個start命令,我可以在其中檢查用戶是否已經在我的資料庫中,如果沒有(start第一次運行命令)我想將此用戶添加到我的資料庫中。
這是我嘗試過的想法:
client = commands.Bot(command_prefix = '/')
# define commands
@client.command()
async def start(ctx):
await start_command(ctx)
async def start_command(ctx):
user_id : int = ctx.message.author.id
result : int = does_user_exist(user_id) # returns 0 if no entry in database
if (result == 0):
print('Create user')
create_user(user_id)
await start_command(ctx)
else:
welcome_message = 'Welcome!'
await ctx.send(welcome_message)
我的想法是檢查用戶是否已經注冊(=我的資料庫中的條目)。如果是,則發送訊息。為了避免運行/start兩次,我嘗試使用遞回。
我的問題是在用戶不存在的情況下。用戶是使用 來創建的create_user(user_id),但是遞回沒有按我的預期作業。/start在這種情況下,我需要在機器人發送我的訊息之前第二次運行。
(這就是為什么我知道用戶已創建,否則/start第二次運行不會顯示我的訊息
我將如何使用遞回實作我的目標?
uj5u.com熱心網友回復:
不要使用遞回來實作回圈,句點。
async def start_command(ctx):
user_id : int = ctx.message.author.id
while True:
result : int = does_user_exist(user_id)
if result != 0:
break
print('Create user')
create_user(user_id)
welcome_message = 'Welcome!'
await ctx.send(welcome_message)
根據可靠性的不同create_user,您根本不需要回圈。
async def start_command(ctx):
user_id : int = ctx.message.author.id
result : int = does_user_exist(user_id)
if result == 0:
print('Create user')
create_user(user_id)
welcome_message = 'Welcome!'
await ctx.send(welcome_message)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/337428.html
下一篇:用遞回將字串字符加倍
