내가 Discord Bot을 위해 작성한이 코드는 작동하지 않으며 작동하는 이유와 방법, 다른 방법을 알고 싶습니다.
def russian_roulette(author, message):
game_active = True
client.send_message(message.channel, "Russian Roulette game started.6 chambers. 1 loaded.\nType $spin to spin the chamber.\nType $pull to pull the trigger.")
while game_active == True:
if message.content.startswith('$spin'):
chamber = randint(1,6)
client.send_message(message.channel, "%s spins the chambers." % author)
if message.content.startswith('$pull'):
if chamber == 1:
client.send_message(message.channel, "%s pulled the trigger and was not lucky. R.I.P." % author)
game_active = False
else:
client.send_message(message.channel, "%s pulled the trigger and nothing happened." % author)
이 API 래퍼를 discord에 사용하고 있다고 가정합니다 .
이 경우 메시지에 제대로 응답 할 수 있도록 데코레이터 client.event를 사용하여 함수를 만들어야합니다. 이렇게 :
@client.event
async def on_message(message):
if message.content.startswith('$spin'):
chamber = randint(1,6)
client.send_message(message.channel, "%s spins the chambers." % author)
if message.content.startswith('$pull'):
if chamber == 1:
client.send_message(message.channel, "%s pulled the trigger and was not lucky. R.I.P." % message.author)
game_active = False
else:
client.send_message(message.channel, "%s pulled the trigger and nothing happened." % message.author)
이 함수는 함수가 한 번만 호출되는 코드가 아니라 봇이 새 메시지를받을 때마다 호출됩니다.
이전에이 API를 사용한 적이 없으며 단순히 문서와 예제를 읽고있는 중입니다. 이것은 올바르지 않을 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다