[英]How can I make my discord bot respond to the correct answer?
我正在編寫一個機器人,它在我的不和諧頻道中詢問一個瑣事問題。 我已經重寫了幾次,但無法讓它接受答案。 我可能錯過了一些可怕的東西。
我的代碼:
# bot.py
import os
import json
import discord
from discord.ext import commands
import requests
token=('*removed*')
bot = commands.Bot(command_prefix ='??')
os.chdir(r'C:\Users\thund\OneDrive\Documenten\Python programmeren\Discord - bot')
correct_answer = ''
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
@bot.event
async def on_member_join(member):
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, member)
with open('users.json', 'w') as f:
json.dump(users, f)
await member.create_dm()
await member.dm_channel.send(
f'Hello {member.name}, You have joined a Discord server where the Mr. Questionnaire is active!\n')
await member.dm_channel.send(
f'By default you will get questions which you can answer by preceding your answer by ??')
await member.dm_channel.send(
f'There are different prices you can win, try answering some questions and find out!')
@bot.command()
async def start(ctx):
global correct_answer
explanation = (
'So you want to play a game? We will ask questions, the first one to answer wins one point\n'
'I will keep firing questions, untill someone types ??stopplease'
)
await ctx.send(explanation)
r = requests.get (url = 'https://opentdb.com/api.php?amount=1')
data = r.json()
category = data['results'][0]['category']
difficulty = data['results'][0]['difficulty']
question = data['results'][0]['question']
answer = data['results'][0]['correct_answer']
correct_answer = answer.replace(' ','_')
await ctx.send('Category: %s\nDifficulty: %s\nQuestion: %s\n'
% (category, difficulty, question))
@bot.command()
async def correct(ctx):
global correct_answer
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, ctx.author)
await add_experience(users, ctx.author, 10)
await level_up(users, ctx.author, ctx.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
await ctx.send('{} has guessed correctly!'.format(ctx.author))
bot.command('start')
async def update_data(users, user):
if not user.id in users:
users[user.id] = {}
users[user.id]['experience'] = 0
users[user.id]['level'] = 1
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** (1/4))
if lvl_start < lvl_end:
await ctx.send(channel, '{} has leveled up to level {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
@bot.command(name='supersecret')
async def answer(ctx):
global correct_answer
await ctx.send('The Answer is: %s' % (correct_answer))
@bot.command(name='%s' % (correct_answer))
async def correct22(ctx):
global correct_answer
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, message.author)
await add_experience(users, message.author, 10)
await level_up(users, message.author, message.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
await ctx.send('{message.author} has guessed correctly!')
async def update_data(users, user):
if not user.id in users:
users[user.id] = {}
users[user.id]['experience'] = 0
users[user.id]['level'] = 1
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** (1/4))
if lvl_start < lvl_end:
await bot.send_message(channel, '{} has leveled up to level {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
@bot.command(name='stopplease', help='Stop it right now!')
async def stop_quiz(ctx):
await ctx.send('Thank you for playing our game!\n'
'If you like it, please consider a donation and/or using it on all the servers you know!')
@bot.event
async def on_error(event, *args, **kwargs):
with open('err.log', 'a') as f:
if event == 'on_message':
f.write(f'Unhandled message: {args[0]}\n')
elif event == 'command':
f.write(f'Unhandled command: {args[0]}\n')
else:
raise
bot.run(token)
我嘗試添加一些我希望它可以觸發的命令,但我完全遺漏了一些東西。
謝謝您的幫助!
你的問題是對裝飾者的誤解。 它們是一個相當復雜的話題,所以這里有一個基本資源。 對您來說,最重要的是以下幾點:裝飾器只運行一次,這意味着只要您運行文件,它就會監聽其中的任何內容。 現在你想在你的代碼中做什么
@bot.command(name='%s' % (correct_answer))
是在裝飾器中有一個動態值,所以機器人會響應正確的答案。 這不起作用! 裝飾器只運行一次。
那么我們如何解決這個問題呢? 我們需要稍微重寫你的正確 22 函數。 您的機器人可以通過arguments處理動態輸入。 幸運的是,這些在 bot 擴展中很容易做到。 即使有參數,我們仍然需要一個命令名稱,所以讓我們選擇“answer”。 因此,在您嘗試回答??2005
您現在回答??answer 2005
。 您可以嘗試按照最初的意圖進行操作,但在我看來,這會使您的代碼過於復雜,因為您必須處理消息處理程序。
潛在的解決方案:
@bot.command(name='answer')
async def correct22(ctx, answer):
global correct_answer
if correct_answer != answer:
return
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, message.author)
await add_experience(users, message.author, 10)
await level_up(users, message.author, message.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
await ctx.send('{message.author} has guessed correctly!')
我還沒有測試過像體驗這樣的用戶功能,但至少消息回復應該像這樣工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.