Mann5

test s55

Jun 22nd, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. #Ranks!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # pylint: disable=invalid-name
  4. """Any word in a sentence can trigger the command"""
  5. #---------------------------------------
  6. # Libraries and references
  7. #---------------------------------------
  8. from collections import deque
  9. import codecs
  10. import json
  11. import os
  12. import winsound
  13. import ctypes
  14. import re
  15.  
  16. #---------------------------------------
  17. # [Required] Script information
  18. #---------------------------------------
  19. ScriptName = "WT_Ranks"
  20. Website = "https://www.Twitch.tv/castorr91"
  21. Creator = "Castorr91"
  22. Version = "1.5"
  23. Contribution = "ChickenPossible"
  24. Description = "Word_Trigger: Rank questions will trigger a response from bot in chat"
  25. #---------------------------------------
  26. # Versions
  27. #---------------------------------------
  28. """
  29. 1.5 Fixed issues with sound commands audio playing
  30. 1.4 Words / phrases now splits with | allowing multiple phrases (by ChickenPossible)
  31. 1.3 Fixed userid showing instead of username
  32. 1.2 Fixed HasPermission & cooldown
  33. 1.1 Added Mixer support
  34. 1.0 Initial release
  35. """
  36. #---------------------------------------
  37. # Variables
  38. #---------------------------------------
  39. settingsFile = os.path.join(os.path.dirname(__file__), "settings.json")
  40. AudioFilesPath = os.path.join(os.path.dirname(__file__), "sounds")
  41. AudioPlaybackQueue = deque()
  42. MessageBox = ctypes.windll.user32.MessageBoxW
  43. MB_YES = 6
  44. #---------------------------------------
  45. # Classes
  46. #---------------------------------------
  47. class Settings:
  48. """" Loads settings from file if file is found if not uses default values"""
  49. #The 'default' variable names need to match UI_Config
  50. def __init__(self, settingsFile=None):
  51. if settingsFile and os.path.isfile(settingsFile):
  52. with codecs.open(settingsFile, encoding='utf-8-sig', mode='r') as f:
  53. self.__dict__ = json.load(f, encoding='utf-8-sig')
  54.  
  55. else: #set variables if no settings file is found
  56. self.OnlyLive = True
  57. self.Command = "Kappa"
  58. self.PS = False
  59. self.SF = "test.mp3"
  60. self.Permission = "Everyone"
  61. self.PermissionInfo = ""
  62. self.Usage = "Stream Chat"
  63. self.UseCD = True
  64. self.CasterCD = True
  65. self.Cooldown = 5
  66. self.OnCooldown = "{0} the command is still on cooldown for {1} seconds!"
  67. self.UserCooldown = 0
  68. self.OnUserCooldown = "{0} the command is still on user cooldown for {1} seconds!"
  69. self.BaseResponse = "{0} don't you dare being sarcastic here!"
  70. self.PermissionResp = "{0} -> only {1} ({2}) and higher can use trigger this secret"
  71. self.Count = 0
  72. self.Volume = 50
  73.  
  74. # Reload settings on save through UI
  75. def Reload(self, data):
  76. """Reload settings on save through UI"""
  77. self.__dict__ = json.loads(data, encoding='utf-8-sig')
  78. fullpath = os.path.join(AudioFilesPath, MySet.SF)
  79. if not (fullpath and os.path.isfile(fullpath)) and MySet.PS:
  80. MessageBox = ctypes.windll.user32.MessageBoxW
  81. returnValue = MessageBox(0, u"Couldn't find the specified soundfile."
  82. "\r\nMake sure the name is correct and that "
  83. "the file is located in the sounds folder"
  84. "\r\n\r\nDo you want to open the sounds folder now?"
  85. , u"File not found", 4)
  86. if returnValue == MB_YES:
  87. OpenSoundFolder()
  88.  
  89. def Save(self, settingsfile):
  90. """ Save settings contained within the .json and .js settings files. """
  91. try:
  92. with codecs.open(settingsfile, encoding="utf-8-sig", mode="w+") as f:
  93. json.dump(self.__dict__, f, encoding="utf-8", ensure_ascii=False)
  94. with codecs.open(settingsfile.replace("json", "js"), encoding="utf-8-sig", mode="w+") as f:
  95. f.write("var settings = {0};".format(json.dumps(self.__dict__,
  96. encoding='utf-8',
  97. ensure_ascii=False)))
  98. except ValueError:
  99. Parent.Log(ScriptName, "Failed to save settings to file.")
  100.  
  101. #---------------------------------------
  102. # [OPTIONAL] Settings functions
  103. #---------------------------------------
  104. def SetDefaults():
  105. """Set default settings function"""
  106. winsound.MessageBeep()
  107. returnValue = MessageBox(0, u"You are about to reset the settings, "
  108. "are you sure you want to contine?"
  109. , u"Reset settings file?", 4)
  110.  
  111. if returnValue == MB_YES:
  112. returnValue = MessageBox(0, u"Settings successfully restored to default values"
  113. , u"Reset complete!", 0)
  114.  
  115. MySet = Settings()
  116. MySet.Save(settingsFile)
  117.  
  118. def ReloadSettings(jsonData):
  119. """Reload settings on Save"""
  120. global MySet
  121. MySet.Reload(jsonData)
  122.  
  123. #---------------------------------------
  124. # [Required] functions
  125. #---------------------------------------
  126. def Init():
  127. """Data on Load, required function"""
  128. global MySet
  129. MySet = Settings(settingsFile)
  130.  
  131. def Tick():
  132. """Required tick function"""
  133. if AudioPlaybackQueue:
  134. if Parent.PlaySound(AudioPlaybackQueue[0], MySet.Volume*0.01):
  135. AudioPlaybackQueue.popleft()
  136.  
  137. def Execute(data):
  138. """Required Execute Data function"""
  139. if data.IsChatMessage():
  140. if not IsFromValidSource(data, MySet.Usage):
  141. return
  142.  
  143. if not MySet.Command.lower() in data.Message.lower() or not multiWords(data)
  144. return
  145.  
  146. if not HasPermission(data):
  147. return
  148.  
  149. if IsOnCooldown(data):
  150. return
  151.  
  152. if MySet.OnlyLive and Parent.IsLive() is False:
  153. return
  154.  
  155. RunCommand(data)
  156.  
  157. #---------------------------------------
  158. # [Optional] functions
  159. #---------------------------------------
  160. def SendResp(data, Usage, Message):
  161. """Sends message to Stream or discord chat depending on settings"""
  162. Message = Message.replace("$user", data.UserName)
  163. Message = Message.replace("$currencyname", Parent.GetCurrencyName())
  164. Message = Message.replace("$target", data.GetParam(1))
  165. Message = Message.replace("$permissioninfo", MySet.PermissionInfo)
  166. Message = Message.replace("$permission", MySet.Permission)
  167.  
  168.  
  169. l = ["Stream Chat", "Chat Both", "All", "Stream Both"]
  170. if not data.IsFromDiscord() and (Usage in l) and not data.IsWhisper():
  171. Parent.SendStreamMessage(Message)
  172.  
  173. l = ["Stream Whisper", "Whisper Both", "All", "Stream Both"]
  174. if not data.IsFromDiscord() and data.IsWhisper() and (Usage in l):
  175. Parent.SendStreamWhisper(data.User, Message)
  176.  
  177. l = ["Discord Chat", "Chat Both", "All", "Discord Both"]
  178. if data.IsFromDiscord() and not data.IsWhisper() and (Usage in l):
  179. Parent.SendDiscordMessage(Message)
  180.  
  181. l = ["Discord Whisper", "Whisper Both", "All", "Discord Both"]
  182. if data.IsFromDiscord() and data.IsWhisper() and (Usage in l):
  183. Parent.SendDiscordDM(data.User, Message)
  184.  
  185. def IsFromValidSource(data, Usage):
  186. """Return true or false depending on the message is sent from
  187. a source that's in the usage setting or not"""
  188. if not data.IsFromDiscord():
  189. l = ["Stream Chat", "Chat Both", "All", "Stream Both"]
  190. if not data.IsWhisper() and (Usage in l):
  191. return True
  192.  
  193. l = ["Stream Whisper", "Whisper Both", "All", "Stream Both"]
  194. if data.IsWhisper() and (Usage in l):
  195. return True
  196.  
  197. if data.IsFromDiscord():
  198. l = ["Discord Chat", "Chat Both", "All", "Discord Both"]
  199. if not data.IsWhisper() and (Usage in l):
  200. return True
  201.  
  202. l = ["Discord Whisper", "Whisper Both", "All", "Discord Both"]
  203. if data.IsWhisper() and (Usage in l):
  204. return True
  205. return False
  206.  
  207. def RunCommand(data):
  208. """Execute the command if triggered"""
  209. message = MySet.BaseResponse.format(data.UserName, MySet.Count)
  210. SendResp(data, MySet.Usage, message)
  211.  
  212. if MySet.PS:
  213. EnqueueAudioFile(MySet.SF)
  214.  
  215. Parent.AddUserCooldown(ScriptName, MySet.Command, data.User, MySet.UserCooldown)
  216. Parent.AddCooldown(ScriptName, MySet.Command, MySet.Cooldown)
  217.  
  218.  
  219.  
  220.  
  221.  
  222. def multiWords(data):
  223. """Check if one of the word is in the message"""
  224. words = MySet.Command.split('|')
  225. for word in words:
  226. if re.search(r'\b' + re.escape(word.lower()) + r'\b', data.Message.lower()):
  227. return True
  228. return False
  229.  
  230.  
  231.  
  232.  
  233.  
  234. def EnqueueAudioFile(audiofile):
  235. """ Adds an audio file from the audio folder to the play queue. """
  236. fullpath = os.path.join(AudioFilesPath, audiofile)
  237. AudioPlaybackQueue.append(fullpath)
  238.  
  239. def OpenSoundFolder():
  240. """Open specific sounds folder"""
  241. os.startfile(AudioFilesPath)
  242.  
  243. def HasPermission(data):
  244. """Returns true if user has permission and false if user doesn't"""
  245. if not Parent.HasPermission(data.User, MySet.Permission, MySet.PermissionInfo):
  246. message = MySet.PermissionResp.format(data.UserName, MySet.Permission, MySet.PermissionInfo)
  247. SendResp(data, MySet.Usage, message)
  248. return False
  249. return True
  250.  
  251. def IsOnCooldown(data):
  252. """Return true if command is on cooldown and send cooldown message if enabled"""
  253. cooldown = Parent.IsOnCooldown(ScriptName, MySet.Command)
  254. userCooldown = Parent.IsOnUserCooldown(ScriptName, MySet.Command, data.User)
  255. caster = (Parent.HasPermission(data.User, "Caster", "") and MySet.CasterCD)
  256.  
  257. if (cooldown or userCooldown) and caster is False:
  258.  
  259. if MySet.UseCD:
  260. cooldownDuration = Parent.GetCooldownDuration(ScriptName, MySet.Command)
  261. userCDD = Parent.GetUserCooldownDuration(ScriptName, MySet.Command, data.User)
  262.  
  263. if cooldownDuration > userCDD:
  264. m_CooldownRemaining = cooldownDuration
  265.  
  266. message = MySet.OnCooldown.format(data.UserName, m_CooldownRemaining)
  267. SendResp(data, MySet.Usage, message)
  268.  
  269. else:
  270. m_CooldownRemaining = userCDD
  271.  
  272. message = MySet.OnUserCooldown.format(data.UserName, m_CooldownRemaining)
  273. SendResp(data, MySet.Usage, message)
  274. return True
  275. return False
  276.  
  277. def TestSound():
  278. """Test sound & volume through UI button"""
  279. SoundsPath = os.path.join(AudioFilesPath, MySet.SF)
  280. Parent.PlaySound(SoundsPath, MySet.Volume*0.01)
  281.  
Add Comment
Please, Sign In to add comment