我正在尝试创建一个 python 程序来嵌入一个 Twitch.tv 聊天机器人,当在聊天中说某些事情或完成一个事件(新订阅者、关注者等)时,它将播放一个预制的视频。我确实有一些问题,但会提到迄今为止我遇到的最重要的事情。我隐藏了秘密信息。Coder program = Sublime3 由于主文件中有引号,因此主文件中的引用有时会失败。对不起。运行时,我得到以下输出:
Python-dotenv could not parse statement starting at line 1
Python-dotenv could not parse statement starting at line 2
Python-dotenv could not parse statement starting at line 3
Python-dotenv could not parse statement starting at line 4
Traceback (most recent call last):
File "frame1.py", line 149, in <module>
main()
文件“frame1.py”,第 146 行,在主 FrameClass(None) 文件“frame1.py”,第 11 行,在init irc_token = os.environ['TMI_TOKEN'] 文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation. Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\os.py",第 675 行,在getitem 中从 None KeyError中 引发 KeyError(key):'TMI_TOKEN'
导入:gui 是用 wxpython python 3.8.5 python-dotenv twitchio 编写的,命令 os
.env
TMI_TOKEN='secret code',
CLIENT_ID='secret code',
BOT_NICK='Bot name',
BOT_PREFIX='!',
CHANNEL='Stream channel name'
我的主要代码文件:
#frame1.py
import wx, wx.media import os from twitchio.ext import commands from dotenv import load_dotenv load_dotenv() class FrameClass (wx.Frame): def init (self, parent): super(FrameClass, self)。init (parent, title = "Super Bot", size = (750, 400))
irc_token = os.environ['TMI_TOKEN']
client_id = os.environ['CLIENT_ID']
nick = os.environ['BOT_NICK']
prefic = os.environ['BOT_PREFIX']
initial_channels = os.environ['CHANNEL']
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
connectMenu = wx.Menu()
helpMenu = wx.Menu()
openItem = fileMenu.Append(wx.ID_OPEN, 'Open')
saveItem = fileMenu.Append(wx.ID_SAVE, 'Save As')
exitItem = fileMenu.Append(wx.ID_EXIT, 'EXIT')
helpItem = helpMenu.Append(wx.ID_HELP, 'Help!')
menuBar.Append(fileMenu, '&File')
menuBar.Append(connectMenu, '&Connect')
menuBar.Append(helpMenu, '&About')
self.Bind(wx.EVT_MENU, self.OnOpen, openItem)
self.Bind(wx.EVT_MENU, self.SaveAs, saveItem)
self.Bind(wx.EVT_MENU, self.Quit, exitItem)
self.Bind(wx.EVT_MENU, self.OnHelp, helpItem)
self.SetMenuBar(menuBar)
self.CreateStatusBar()
self.SetStatusText("Welcome to the new Bot!")
vsplitter = wx.SplitterWindow(self)
left = LeftPanel(vsplitter)
right = RightPanel(vsplitter)
vsplitter.SplitVertically(left, right)
vsplitter.SetMinimumPaneSize(200)
bot = ChatBot()
self.Show(True)
def OnOpen(self, event):
with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return
pathname = fileDialog.GetPath()
try:
with open(pathname, 'r') as file:
self.doLoadDataOrWhatever(file)
except IOError:
wx.LogError("Cannot open file '%s'." % newfile)
def SaveAs(self, event):
with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files (*.xyz)|*.xyz", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# save the current contents in the file
pathname = fileDialog.GetPath()
try:
with open(pathname, 'w') as file:
self.doSaveData(file)
except IOError:
wx.LogError("Cannot save current data in file '%s'." % pathname)
def Quit(self, e):
self.Close()
def OnHelp(self, e):
"""Display an help Dialog"""
wx.MessageBox("This is where future help will be placed. Dont Forget!", "Help", wx.OK|wx.ICON_INFORMATION)
类 LeftPanel (wx.Panel): def init (self, parent): wx.Panel。初始化(自我,父母=父母)
wx.Button(self, -1, "Test Idle", pos = (10, 10))
类 RightPanel (wx.Panel): def init (self, parent): wx.Panel。初始化(自我,父母=父母)
self.SetBackgroundColour("grey")
self.testMedia = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER, szBackend = wx.media.MEDIABACKEND_WMP10)
self.media = 'C:\\Wiggle_Bot\\Python (Offline)\\JustDoIt.mp4'
self.testMedia.Bind(wx.media.EVT_MEDIA_LOADED, self.play)
self.testMedia.SetVolume(0.5)
if self.testMedia.Load(self.media):
pass
else:
print("Media not found")
self.quit(None)
def play(self, event):
self.testMedia.Play()
类 ChatBot (commands.Bot): def init (self): super()。初始化(os.environ['TMI_TOKEN'],os.environ['CLIENT_ID'],os.environ['BOT_NICK'],os.environ['BOT_PREFIX'],os.environ['CHANNEL'])
# Events don't need decorators when subclassed
async def event_ready(self):
print(f'Ready | {self.nick}')
async def event_message(self, message):
print(message.content)
await self.handle_commands(message)
# Commands use a decorator...
@commands.command(name='test')
async def my_command(self, ctx):
await ctx.send(f'Hello {ctx.author.name}!')
def main(): App = wx.App() FrameClass(None) App.MainLoop()
主要的()