【发布时间】:2020-04-07 04:47:07
【问题描述】:
我正在制作一个掷骰子 Discord bot qith Python 3。骰子编号在字典中查找一个短语并输出它。我可以很好地使用六面骰子,但我希望用户能够将其改为十面骰子。
掷骰子是“!y1”,将其设置为 10 面骰子是“!y d10”。我默认将其设置为 d6,但是当我现在尝试滚动时,它给了我一个变量未定义的错误。有人可以帮我指出我所缺少的吗?
import os
from random import randint
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
def ybna6_faces(x):
return{
1: 'Yes, And...',
2: 'Yes...',
3: 'Yes, But...',
4: 'No, But...',
5: 'No...',
6: 'No, And...'
}[x]
def danger6_faces(x):
return{
1: 'Blank',
2: 'Blank',
3: 'Blank',
4: 'Blank',
5: 'Skull!',
6: 'Two Skull!!'
}[x]
def ybna10_faces(x):
return{
1: 'Yes, And...',
2: 'Yes...',
3: 'Yes, But...',
4: 'No, But...',
5: 'No...',
6: 'No, And...',
7: '',
8: '',
9: '',
10: ''
}[x]
def danger10_faces(x):
return{
1: 'Blank',
2: 'Blank',
3: 'Blank',
4: 'Blank',
5: 'Skull!',
6: 'Two Skull!!',
7: '',
8: '',
9: '',
10: ''
}[x]
sides = 6
#active_ybna = 6
#active_danger = 6
w=""
#Successful server connection message
@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
#Makes sure bot doesn't respond to itself
@client.event
async def on_message(message):
if message.author == client.user:
return
#Choose die (6 or 10)
if message.content.startswith('!y d6'):
sides = 6
if message.content.startswith('!y d10'):
sides = 10
#Evaluate rolls
if message.content.startswith('!y'):
if int(message.content[2])>int(0):
z = int(message.content[2])
for x in range(z):
y = randint(1,int(sides))
active_ybna = 'ybna'+ sides + '_faces()'
response = 'Roll ' + str(x+1) + ': ' + active_ybna(y)
await message.channel.send(response)
if len(message.content)>=4:
if int(message.content[4])>int(0):
z = int(message.content[4])
for x in range(z):
y=randint(1,int(sides))
active_danger = 'danger'+ sides + '_faces()'
response = 'Danger ' + str(x+1) + ': ' + active_danger(y)
await message.channel.send(response)
client.run(TOKEN)
编辑: '!y1' 输入的回溯
YBNA Bot has connected to Discord!
Ignoring exception in on_message
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "/home/pi/Desktop/discord bots/ybnabot.py", line 92, in on_message
y = randint(1,int(sides))
UnboundLocalError: local variable 'sides' referenced before assignment
【问题讨论】:
-
您能否更新您的问题以包含回溯/错误?
-
我知道我忘记了一些东西....用回溯编辑了帖子。
标签: python python-3.x discord discord.py