Would you like to react to this message? Create an account in a few clicks or log in to continue.

[Relase] StealthBot Trivia

2 posters

 :: Bots :: StealthBot

Go down

[Relase] StealthBot Trivia Empty [Relase] StealthBot Trivia

Post  GRAPHIXX Mon Nov 08, 2010 7:10 pm

[You must be registered and logged in to see this image.]

Anything that has (Trivia) after it is trivia question lists
tbstart
tbstop
tbuselist
tbgetlist

If you dont know how to add a list go to the bottom of this script

Code:
'trivia
'1.21
'
'Trivia script written by SoCxFiftyToo 07/09/2005
'SoCxFiftyToo

'// Trivia script
'//  written by SoCxFiftyToo
'//    07/09/2005

'//////////////////////////////////////////////
'// Nothing in this file needs to be edited. //
'// All settings can be changed by using the //
'// pluginsetings.ini located in the plugins //
'// folder.                                  //
'//////////////////////////////////////////////

'////////////////////////////////////////////////////////////////////////////////////////
'// ChangeLog                                                                          //
'// ---------                                                                          //
'// v1.0                                                                              //
'//  - Initial Release                                                                //
'//                                                                                    //
'// v1.1                                                                              //
'//  - Started this changelog                                                          //
'//  - Logged correct and incorrect answers (Not doing anything with them yet!!)      //
'//                                                                                    //
'// v1.2                                                                              //
'//  - Fixed a bug with the .tbtop command. No longer disconnects.                    //
'//  - Usernames are now only added to the database if they get a correct answer.      //
'//                                                                                    //
'// v1.21                                                                              //
'//  -just added stuff at the top and closed in this comment box ~rush                //
'//                                                                                    //
'//                                                                                    //
'////////////////////////////////////////////////////////////////////////////////////////

'// Current Version
Public Const trivia_ver = "1.1"

'// Public Vars
Public trivia_TC
Public trivia_MSGBOX_TITLE
Public trivia_INPUTBOX_TITLE
Public trivia_flood
Public trivia_round_counter

Public trivia_round_time
Public trivia_running
Public trivia_flood_time
Public trivia_access
Public trivia_trigger
Public trivia_default_list
Public trivia_start_command
Public trivia_stop_command
Public trivia_score_command
Public trivia_rank_command
Public trivia_top_command
Public trivia_help_command
Public trivia_uselist_command
Public trivia_getlist_command

'/////////////////////////////////////////////// EVENTS /////////////////////////////////////////////////

Sub trivia_Event_Load()
  '// Create an instance of our TriviaClass object
  Set trivia_TC = New TriviaClass

  '// Set MsgBox and InputBox titles
  trivia_MSGBOX_TITLE = "TriviaBot v" & trivia_ver
  trivia_INPUTBOX_TITLE = "TriviaBot v" & trivia_ver

  '// Default program settings
  SetSetting "trivia", "round_time", 40, "Total Length of each round (in seconds). Must be a multiple of 2.", False
  SetSetting "trivia", "flood_time", 5, "Set this to the length of the flood protection measured in seconds (INTEGERS ONLY).", False
  SetSetting "trivia", "access", 50, "Set this to the desired access required to start/stop the bot.", False
  SetSetting "trivia", "trigger", BotVars.Trigger, "Set this to the trigger you want to use.", False
  SetSetting "trivia", "default_list", "", "Default list alias to use.", False


  '// Default chat commands
  SetSetting "trivia", "start_command", "tbstart", "Command to start the trivia.", False
  SetSetting "trivia", "stop_command", "tbstop", "Command to stop the trivia.", False
  SetSetting "trivia", "score_command", "tbscore", "Command to get the score of a player.", False
  SetSetting "trivia", "rank_command", "tbrank", "Command to get the rank of a player.", False
  SetSetting "trivia", "top_command", "tbtop", "Command to get the Top 5 ranked players.", False
  SetSetting "trivia", "help_command", "tbhelp", "Command to get a list of commands for the script.", False
  SetSetting "trivia", "uselist_command", "tbuselist", "Command to get a list of question list aliases.", False
  SetSetting "trivia", "getlist_command", "tbgetlist", "Command to use a question list.", False
 

  '// Set public variables
  trivia_round_time = CLng(GetSetting("trivia", "round_time"))
  trivia_flood_time = CLng(GetSetting("trivia", "flood_time"))
  trivia_access = CLng(GetSetting("trivia", "access"))
  trivia_trigger = CStr(GetSetting("trivia", "trigger"))
  trivia_default_list = CStr(GetSetting("trivia", "default_list"))
  trivia_start_command = CStr(GetSetting("trivia", "start_command"))
  trivia_stop_command = CStr(GetSetting("trivia", "stop_command"))
  trivia_score_command = CStr(GetSetting("trivia", "score_command"))
  trivia_rank_command = CStr(GetSetting("trivia", "rank_command"))
  trivia_top_command = CStr(GetSetting("trivia", "top_command"))
  trivia_help_command = CStr(GetSetting("trivia", "help_command"))
  trivia_uselist_command = CStr(GetSetting("trivia", "uselist_command"))
  trivia_getlist_command = CStr(GetSetting("trivia", "getlist_command"))
  trivia_flood = False
  trivia_running = False

  '// Create our timers
  CreateTimer "trivia", "Info"
  TimerInterval "trivia", "Info", 600
  CreateTimer "trivia", "Flood"
  TimerInterval "trivia", "Flood", GetSetting("trivia", "flood_time")
  CreateTimer "trivia", "Round"
  TimerInterval "trivia", "Round", (GetSetting("trivia", "round_time") / 2)
  CreateTimer "trivia", "Countdown"
  TimerInterval "trivia", "Countdown", 10

  '// Use Default List
  If Trim(trivia_default_list) <> "" Then
    If trivia_TC.AliasExists(trivia_default_list) Then
      trivia_TC.UseList trivia_default_list
    Else
      SetSetting "trivia", "default_list", "", "Default list alias to use.", True
      Dsp 4, "Default list alias is no longer available. Default list has been reset.", BotVars.Username, vbRed
    End If
  End If
End Sub

Sub trivia_Event_UserTalk(Username, Flags, Message, Ping)
  '// Not a command, but a possible answer
  If Left(Message, 1) <> trivia_trigger Then
    If trivia_running Then
      trivia_check_answer Message, Username
    End If
    Exit Sub
  End If
  If Trim(Message) = trivia_trigger Then Exit Sub '// Fixes error with command parsing
  cmd = Split(Mid(LCase(Trim(Message)), 2), " ") '// Get the command and arguments
  '// Call the proper sub
  Select Case cmd(0)
    Case trivia_start_command:       trivia_start_cmd cmd, Username
    Case trivia_stop_command:       trivia_stop_cmd cmd, Username
    Case trivia_score_command:      trivia_score_cmd cmd, Username
    Case trivia_rank_command:      trivia_rank_cmd cmd, Username
    Case trivia_top_command:      trivia_top_cmd cmd, Username
    Case trivia_uselist_command:   trivia_uselist_cmd cmd, Username
    Case trivia_getlist_command:   trivia_getlist_cmd cmd, Username
    Case trivia_help_command:      trivia_help_cmd cmd, Username
  End Select

End Sub


Sub trivia_Event_PressedEnter(Text)
  '// Set Default List
  If LCase(Text) = trivia_trigger & "defaultlist" Then
    VetoThisMessage
    sAlias = InputBox("Type the alias of the list that you want the bot to use at start up.",trivia_INPUTBOX_TITLE)
    If trivia_TC.AliasExists(sAlias) Then
      '// Alias exists
      SetSetting "trivia", "default_list", sAlias, "Default list alias to use.", True
      Dsp 4, "Default list alias is now " & sAlias & ".", BotVars.Username, vbGreen
    Else
      '// Alias does not exist
      Dsp 4, "There is no question list with the alias '" & sAlias & "'", BotVars.Username, vbRed
      Exit Sub
    End If
    Exit Sub
  End If

  '// Add list to database
  If LCase(Text) = trivia_trigger & "addlist" Then
    VetoThisMessage
    On Error Resume Next
    '// Get Question/Answer File
    Set objCD = CreateObject("MSComDlg.CommonDialog")
    If Err.number = 0 Then '// No Error
      '// Common Dialog is available
      objCD.MaxFileSize = 260 'Initialize buffer
      objCD.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
      objCD.InitDir = BotPath()
      objCD.CancelError = True
      objCD.ShowOpen
      If Err.Number <> 0 Then '// Error... booo...
        '// Cancelled
        On Error Goto 0
        Exit Sub
      End if
      sFilePath = objCD.FileName
    Else
      sFilePath = InputBox("Enter the name to the question's file. This file MUST be located in the StealthBot folder. ",trivia_INPUTBOX_TITLE)
      If Not trivia_TC.objFSO.FileExists(BotPath & sFilePath) Then
        MsgBox "File not found.",vbCritical,trivia_MSGBOX_TITLE
        Exit Sub
      End If
    End If
    On Error Goto 0
    '// Get Question List Alias
    sAlias = ""
    Do While sAlias = "" And InStr(sAlias," ") = 0
      sAlias = InputBox("Enter an alias for this question list. This will be the name you reference the list " & _
                        "as. There must not be any spaces in this word.",trivia_INPUTBOX_TITLE)
      If sAlias = "" Then
        MsgBox "Cancelled.",vbInformation,trivia_MSGBOX_TITLE
        Exit Sub
      End If
      If trivia_TC.AliasExists(sAlias) Then
        '// Alias already exists
        MsgBox sAlias & " already exists. Please choose another alias for this list.", vbInformation,trivia_MSGBOX_TITLE
        sAlias = ""
      End If
    Loop
    '// Get Question List Title
    sTitle = ""
    Do While sTitle = ""
      sTitle = InputBox("Enter a title for this question list. This will be shown when you choose a list.",trivia_INPUTBOX_TITLE)
    Loop
    '// Get Question List Description
    sDesc = InputBox("Enter a description for this question list. This will be shown when you choose a list. " & _
                    "You may leave this blank if you wish.",trivia_INPUTBOX_TITLE)
    sMsg = "List is ready to be added to the database. The time this process will take depends " & _
          "on how many questions are in the list. On larger lists, the script may exceed the " & _
          "time limit and you will be given the opportunity to end the script. YOU MUST CLICK " & _
          "CONTINUE TO AVOID ANY ERRORS!!" & vbNewLine & vbNewLine & _
          "If you no longer wish to add this list, press Cancel now. Otherwise, press OK now."

    iReply = MsgBox(sMsg,vbOkCancel,trivia_MSGBOX_TITLE)
    If iReply = vbCancel Then
      Dsp 4, "List addition was cancelled.", BotVars.Username, vbYellow
    Else
      Dsp 4, "Adding list to database. Please stand by...", BotVars.Username, vbGreen
      trivia_TC.AddQuestions sAlias, sTitle, sDesc, sFilePath
      Dsp 4, "Successfully added " & sFilePath & " to the database with the alias of " & sAlias & ".", BotVars.Username, vbGreen
    End If
    Exit Sub
  End If

  '// Display Question List's
  If LCase(Text) = trivia_trigger & "showlists" Then
    VetoThisMessage
    Dsp 4, "Here is a list of the available trivia question lists:", BotVars.Username, vbYellow
    trivia_TC.EnumLists vbGreen
    Exit Sub
  End If

  '// Import scores from raylu's trivia script
  If LCase(Text) = trivia_trigger & "importscores" Then
    VetoThisMessage
    If trivia_TC.objFSO.FileExists(BotPath() & "scores.txt") Then
      iReply = MsgBox("scores.txt was found in the " & BotPath() & " folder. Is this correct?", vbYesNoCancel, trivia_MSGBOX_TITLE)
      If iReply = vbYes Then sFilePath = BotPath() & "scores.txt"
    End If
    If sFilePath = "" Then
      On Error Resume Next
      '// Get Question/Answer File
      Set objCD = CreateObject("MSComDlg.CommonDialog")
      If Err.number = 0 Then '// No Error
        '// Common Dialog is available
        objCD.MaxFileSize = 260 'Initialize buffer
        objCD.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        objCD.InitDir = BotPath()
        objCD.CancelError = True
        objCD.ShowOpen
        If Err.Number <> 0 Then '// Error... booo...
          '// Cancelled
          On Error Goto 0
          Exit Sub
        End if
        sFilePath = objCD.FileName
      Else
        sFilePath = InputBox("Enter the full path to raylu's score file. For example..." & vbNewline & vbNewline & "C:\Program Files\StealthBot\my_scores.txt",trivia_INPUTBOX_TITLE)
        If Not trivia_TC.objFSO.FileExists(sFilePath) Then
          MsgBox "File not found.",vbCritical,trivia_MSGBOX_TITLE
          Exit Sub
        End If
      End If
      On Error Goto 0
    End If
    If trivia_TC.objFSO.FileExists(sFilePath) Then
      iReply = MsgBox("This will clear your current user list and import from the text file. Are you sure this is what you want?",vbYesNoCancel,trivia_MSGBOX_TITLE)
      If iReply = vbYes Then
        If trivia_TC.ImportScores(sFilePath,"|") Then
          Dsp 4, "Imported user list.", BotVars.Username, vbGreen
        Else
          Dsp 4, "Error importing user list.", BotVars.Username, vbRed
        End If
      End If
    Else
      MsgBox "File not found.",vbCritical,trivia_INPUTBOX_TITLE
    End If

    Exit Sub
  End If

  If LCase(Text) = trivia_trigger & "deletelist" Then
    VetoThisMessage
    '// Check if Trivia is in progress
    sAlias = InputBox("Enter the alias for the question list that you want to delete. You can use the " & _
                      trivia_trigger & "showlists command to get a list of the question lists and their aliases.",trivia_INPUTBOX_TITLE)
    If trivia_TC.AliasExists(sAlias) Then
      '// Cannot delete a list thats in use. Oh well...
      If trivia_TC.ListAlias = sAlias Then
        MsgBox "You cannot delete this list because it is currently in use. Use  " & trivia_trigger & trivia_uselist_command & _
              " to change lists. If this is your only list, it cannot be deleted.",vbCritical,trivia_MSGBOX_TITLE
        Exit Sub
      End If
      '// Alias exists
      trivia_TC.DeleteList sAlias
      Dsp 4, "Deleted the question list with the alias '" & sAlias & "'", BotVars.Username, vbGreen
    Else
      '// Alias does not exist
      Dsp 4, "There is no question list with the alias '" & sAlias & "'", BotVars.Username, vbRed
      Exit Sub
    End If
    Exit Sub
  End If

  trivia_Event_UserTalk BotVars.Username, "", Text, 0

End Sub


'/////////////////////////////////////////////////// Timer Subs ///////////////////////////

Sub trivia_Info_Timer
  If trivia_running Then
    Dsp 2, "*** TriviaBot " & trivia_ver,0,0
    Dsp 2, "*** Written by SoCxFiftyToo",0,0
    Dsp 2, "*** Type '" & trivia_trigger & trivia_help_command & "' for a list of commands!!",0,0
  End If
End Sub

Sub trivia_Flood_Timer
  trivia_flood = False
  TimerEnabled "trivia", "Flood", False
End Sub

Sub trivia_Round_Timer
  If trivia_running = True Then
    Select Case trivia_round_counter
      Case 1
        Dsp 2, "*** HINT - " & trivia_TC.GetHint,0,0
        trivia_round_counter = 2
      Case 2
        Dsp 2, "*** Time is up!! The answer was " & trivia_TC.Answer & ".",0,0
        trivia_TC.QuestionStatus False '// Register the incorrect answer
        trivia_TC.ResetRound
        TimerEnabled "trivia", "Round", False
        TimerEnabled "trivia", "Countdown", True
    End Select
  End If
End Sub

Sub trivia_Countdown_Timer
  TimerEnabled "trivia", "Countdown", False
  TimerEnabled "trivia", "Round", True
  trivia_TC.GetRandomQuestion
  Dsp 2, "*** " & trivia_TC.Question(),0,0
  trivia_round_counter = 1
End Sub

'/////////////////////////////////////////////////// CUSTOM SUBS/FUNCTIONS ///////////////////////////

Sub trivia_check_answer(Message, Username)
  '// Checks to see if someone typed the correct answer
  If UCase(Message) = UCase(trivia_TC.Answer) Then
    '// Check to see if that persons name is in the database.
    If trivia_TC.UserExists(Username) = False Then
      trivia_TC.AddName Username
    End If
    If trivia_round_counter = 1 Then '// Before hint
      lPoints = 2
    Else
      lPoints = 1
    End If   
    lScore = trivia_TC.AddPoints(Username, lPoints)
    lStreak = trivia_TC.AddStreak(Username)
    lRank = trivia_TC.GetRank(Username)
    sAnswer = trivia_TC.Answer
    trivia_TC.QuestionStatus True '// Register the correct answer
    trivia_TC.ResetRound
    Dsp 2, "*** " & sAnswer & " - Answered by " & Username & " for " & lPoints & " points.",0,0
    Dsp 2, "*** Score: " & lScore & " | Streak: " & lStreak & " | Rank: " & lRank,0,0
    TimerEnabled "trivia", "Round", False
    TimerEnabled "trivia", "Countdown", True
  End If
End Sub

Sub trivia_uselist_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  '// Get the access level of the user
  GetDBEntry Username, al, af
  If trivia_running = False And al >= trivia_access Then
    If UBound(cmd) = 0 Then
      Dsp 2, "*** Proper usage: " & trivia_trigger & trivia_uselist_command & " 'alias'",0,0
      Exit Sub
    End If
    sAlias = cmd(1)
    If Not trivia_TC.AliasExists(sAlias) Then
      Dsp 2, "*** " & sAlias & " is an invalid list alias. Use " & trivia_trigger & trivia_getlist_command & " to get a valid alias.",0,0
      Exit Sub
    End If
    trivia_TC.UseList(sAlias)
    sTitle = trivia_TC.Title
    Dsp 2, "*** " & sTitle & " question list has been selected.",0,0
  Else
    Dsp 2, "*** You must stop the TriviaBot before you can change lists.",0,0
  End If
End Sub

Sub trivia_getlist_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  '// Get the access level of the user
  GetDBEntry Username, al, af
  If al >= trivia_access Then
    Dsp 2, "*** Here is a list of the available trivia question lists:",0,0
    trivia_TC.EnumLists -2
  End If
End Sub


Sub trivia_start_cmd(cmd, Username)
  '// Get the access level of the user
  GetDBEntry Username, al, af
  '// Check if the bot is stopped and if the user has enough access to start it
  If trivia_running = False And al >= trivia_access Then
    If trivia_TC.ListAlias = "" Then
      Dsp 2, "*** No list selected!! Use " & trivia_trigger & trivia_uselist_command & " to select a list.",0,0
    Else
      Dsp 2, "*** Starting the TriviaBot!!",0,0
      Dsp 2, "*** Using list '" & trivia_TC.Title & "'.",0,0
      Dsp 2, "*** Get ready for the first question!!",0,0
      trivia_running = True
      TimerEnabled "trivia", "Countdown", True
      TimerEnabled "trivia", "Round", False
      TimerEnabled "trivia", "Info", True
    End If
  End If
End Sub


Sub trivia_stop_cmd(cmd, Username)
  '// Get the access level of the user
  GetDBEntry Username, al, af
  '// Check if the bot is running and if the user has enough access to stop it
  If trivia_running = True AND al >= trivia_access Then
    Dsp 2, "*** Stopping the TriviaBot!!",0,0
    trivia_running = False
    TimerEnabled "trivia", "Countdown", False
    TimerEnabled "trivia", "Round", False
    TimerEnabled "trivia", "Info", False
  End If
End Sub


Sub trivia_help_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  Dsp 3, trivia_trigger & trivia_score_command & " = Your current score",Username,0
  Dsp 3, trivia_trigger & trivia_score_command & " USERNAME = score of USERNAME",Username,0
  Dsp 3, trivia_trigger & trivia_rank_command & " = Your current rank",Username,0
  Dsp 3, trivia_trigger & trivia_rank_command & " USERNAME = rank of USERNAME",Username,0
  Dsp 3, trivia_trigger & trivia_top_command & " = top 5 scores",Username,0
  Dsp 3, trivia_trigger & trivia_uselist_command & " ALIAS = use question list ALIAS",Username,0
  Dsp 3, trivia_trigger & trivia_getlist_command & " = Display question list aliases.",Username,0
  Dsp 3, trivia_trigger & trivia_help_command & " = this help menu",Username,0
End Sub


Sub trivia_top_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  '// Get the top 5 players in the database
  strTop = trivia_TC.GetTop
  msg = "*** "
  arrPlayers = Split(strTop, ";")
  For Each strData In arrPlayers
    arrData = Split(strData,":")
    msg = msg & arrData(0) & "(" & arrData(1) & ") --- "
  Next
  Dsp 2,msg,0,0
End Sub


Sub trivia_rank_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  If UBound(cmd) = 0 Then
    '// The user wants their own rank
    name = Username
  Else
    '// The user wants rank of the specified username
    name = cmd(1)
  End If
  rank = trivia_TC.GetRank(name)
  If rank = False Then
    'Name not found
    Dsp 2, "*** '" & name & "' is not in the database.",0,0
  Else
    Dsp 2, "*** Current Rank for " & name & ": " & rank,0,0
  End If
End Sub


Sub trivia_score_cmd(cmd, Username)
  '// Flood Protection
  If trivia_flood Then
    '// Too many requests...
    Exit Sub
  End If
  trivia_flood = True
  TimerEnabled "trivia", "Flood", True
  If UBound(cmd) = 0 Then
    '// The user wants their own points
    sName = Username
  Else
    '// The user wants points of the specified username
    sName = cmd(1)
  End If
  lScore = trivia_TC.GetScore(sName)
  If lScore = False Then
    '// Name not found
    Dsp 2, "*** '" & sName & "' is not in the database.",0,0
  Else
    Dsp 2, "*** Current Score for " & sName & ": " & lScore,0,0
  End If
End Sub



'////////////////////////// UNUSED EVENTS!!! ///////////////////////////////

'// Fires when the server sends a blue INFO-type message. (Includes ban and kick messages.)
Sub trivia_Event_ServerInfo(Message)
End Sub
'// Fires when the server sends a red ERROR-type message. (Includes "That user is not logged on." etc.)
Sub trivia_Event_ServerError(Message)
End Sub
'// Fires when a user speaks with /emote.
Sub trivia_Event_UserEmote(Username, Flags, Message)
End Sub
'// Fires when a user joins the channel.
'// Level will contain 0 for no-level Warcraft III players or non-Warcraft III products.
'// Message contains the user's PARSED statstring.
'// OriginalStatstring contains the user's UNPARSED statstring.
Sub trivia_Event_UserJoins(Username, Flags, Message, Ping, Product, Level, OriginalStatstring)
End Sub
'// Fires when a user leaves the channel.
Sub trivia_Event_UserLeaves(Username, Flags)
End Sub
'// Fires when Battle.net updates a user's flags in the channel.
Sub trivia_Event_FlagUpdate(Username, NewFlags, Ping)
End Sub
'// Fires after a successful login.
Sub trivia_Event_LoggedOn(Username, Product)
End Sub
'// Fires once for each user in the channel upon joining a channel.
Sub trivia_Event_UserInChannel(Username, Flags, Message, Ping, Product)
End Sub
'// Flags in this case stores the channel's flags.
Sub trivia_Event_ChannelJoin(ChannelName, Flags)
End Sub
'// Executes when the bot recieves a Profile return from the server. KeyName will be one of the following:
   ' Profile\Sex
   ' Profile\Location
   ' Profile\Description
'// KeyValue will contain the value of that profile key as a string.
Sub trivia_Event_KeyReturn(KeyName, KeyValue)
End Sub
'// Executes when the bot is closed. You can use this sub to write things to disk before the bot shuts down.
Sub trivia_Event_Close()
End Sub
'// Executes every X milliseconds, as set by using its .Interval property.
Sub trivia_scriptTimer_Timer()
End Sub
'// Fires when the bot executes.
Sub trivia_Event_LoadDisplay()
End Sub
'// Fires when a whisper is recieved.
Sub trivia_Event_WhisperFromUser(Username, Flags, Message)
End Sub

'/////////////////////////////////////////////////// CUSTOM CLASSES ///////////////////////////

Class TriviaClass

  '// Class Variables
  Dim objConn, objFSO, objRS_questions, objRS, sAnswer, sHint, sQuestion, lID, lTotalQuestions, lQuestionCounter, sAlias, sTitle

'////////////// Public Class Properties //////////////////

  Public Function Answer
    Answer = me.sAnswer
  End Function

  Public Function Question
    Question = me.sQuestion
  End Function

  Public Function Hint
    Hint = me.sHint
  End Function

  Public Function ListAlias
    ListAlias = me.sAlias
  End Function

  Public Function Title
    Title = me.sTitle
  End Function
'////////////// Public Class Methods //////////////////

  Public Sub UseList(sAlias)
    '// Create our words recordset
    Set objRS_questions = objConn.Execute("SELECT `ID`, `question`, `answer` FROM `" & sAlias & "`")
    '// Set lTotalQuestions with the total number of questions in the question list
    lTotalQuestions = objRS_questions.RecordCount
    '// Set lQuestionCounter to 1 to start at the begining
    lQuestionCounter = 1
    me.sAlias = sAlias
    '// Get our list title
    sql = "SELECT `title` FROM `question_list` WHERE `alias` = '" & sAlias & "'"
    Set objRS = objConn.Execute(sql)
    me.sTitle = objRS.Fields(0)
  End Sub

  Public Function GetNextQuestion
    rec_num = lQuestionCounter
    objRS_questions.Move rec_num, 1 '// adBookmarkFirst (offset from begining)
    lID = objRS_questions.Fields(0)
    sQuestion = objRS_questions.Fields(1)
    sAnswer = objRS_questions.Fields(2)
    GetNextQuestion = sAnswer
    lQuestionCounter = lQuestionCounter + 1
  End Function

  Public Function GetRandomQuestion
    '// Get a random number between 1 and the number of available questions
    rec_num = RanNum(1, lTotalQuestions) - 1
    objRS_questions.Move rec_num, 1 '// adBookmarkFirst (offset from begining)
    lID = objRS_questions.Fields(0)
    sQuestion = objRS_questions.Fields(1)
    sAnswer = objRS_questions.Fields(2)
    GetRandomQuestion = sAnswer
  End Function

  '// Call this function after every question.
  '// pass True if the question was correct, or False if it was incorrect
  Public Sub QuestionStatus(bCorrect)
    If bCorrect Then sField = "correct"
    If Not bCorrect Then sField = "incorrect"
    sql = "UPDATE `" & sALias & "` SET `" & sField & "` = `" & sField & "` + 1 WHERE `ID` = " & lID
    objConn.Execute sql
  End Sub

  Public Function AliasExists(sAlias)
    AliasExists = False
    sql = "SELECT COUNT(*) FROM `question_list` WHERE `alias` = '" & sAlias & "'"
    Set objRS = objConn.Execute(sql)
    If objRS.Fields(0) > 0 Then
      '// Alias already exists
      AliasExists = True
    End If
  End Function

  Public Function UserExists(sName)
    '// Build our SQL query
    sql = "SELECT COUNT(*) FROM `scores` WHERE `username` = '" & sName & "'"
    Set objRS = objConn.Execute(sql)
    If objRS.Fields(0) = 1 Then
      UserExists = True
    Else
      UserExists = False
    End If
  End Function

  Public Function AddName(sName)
    '// Build our SQL query
    sql = "INSERT INTO `scores` (`username`, `score`, `streak` ) VALUES ('" & sName & "', '0', '0')"
    objConn.Execute(sql)
  End Function

  Public Function RemoveName(sName)
    '// Build our SQL query
    sql = "DELETE FROM `scores` WHERE `name` = '" & sName & "'"
    Set objRS = objConn.Execute(sql)
  End Function

  Public Function GetScore(sName)
    '// Build our SQL query
    sql = "SELECT `score` FROM `scores` WHERE `username` = '" & sName & "'"
    Set objRS = objConn.Execute(sql)
    If objRS.BOF = True AND objRS.EOF = True Then
      '// Name not found
      GetScore = False
    Else
      GetScore = objRS.Fields(0)
    End If
  End Function


  Public Function GetRank(sName)
    '// Build our SQL query
    sql = "SELECT `score` FROM `scores` WHERE `username` = '" & sName & "'"
    Set objRS = objConn.Execute(sql)
    If objRS.BOF = True AND objRS.EOF = True Then
      'Name not found
      GetRank = False
    Else
      lScore = objRS.Fields(0)
      sql = "SELECT COUNT(*) FROM `scores` WHERE `score` > " & lScore
      Set objRS = objConn.execute(sql)
      GetRank = objRS.Fields(0) + 1
    End If
  End Function

  Public Function GetStreak(sName)
    '// Build our SQL query
    sql = "SELECT `streak` FROM `scores` WHERE `username` = '" & sName & "'"
    Set objRS = objConn.Execute(sql)
    If objRS.BOF = True AND objRS.EOF = True Then
      'Name not found
      GetStreak = False
    Else
      GetStreak = objRS.Fields(0)
    End If
  End Function


  Public Function AddPoints(sName, lPoints)
    sql = "UPDATE `scores` SET `score` = `score` + " & lPoints & " WHERE `username` = '" & sName & "'"
    objConn.Execute sql
    AddPoints = me.GetScore(sName)
  End Function


  Public Function AddStreak(sName)
    sql = "UPDATE `scores` SET `streak` = 0 WHERE `username` <> '" & sName & "'"
    objConn.Execute sql
    sql = "UPDATE `scores` SET `streak` = `streak` + 1 WHERE `username` = '" & sName & "'"
    objConn.Execute sql
    AddStreak = me.GetStreak(sName)
  End Function

  '// Little complicated here. Returns a string in the format NAME:SCORE;NAME:SCORE
  Public Function GetTop
    '// init our counter (bugfix)
    iCounter = 0
    '// Build our SQL query
    sql = "SELECT TOP 5 `username`, `score` FROM `scores` ORDER BY `score` DESC"
    Set objRS = objConn.Execute(sql)
    strReturn = ""
    Do While Not objRS.EOF And iCounter <> 5
      iCounter = iCounter + 1
      strReturn = strReturn & objRS.Fields(0) & ":" & objRS.Fields(1) & ";"
      objRS.MoveNext
    Loop
    GetTop = Mid(strReturn, 1, Len(strReturn) - 1)
  End Function


  Public Sub ResetRound
    me.sAnswer = ""
    me.sHint = ""
  End Sub

  Public Function GetHint
    GetHint = me.sAnswer
    X = 0
    Do While X < (Len(GetHint)/1.5)
      pos = RanNum(1, Len(GetHint))
      c = "?"
      b = Mid(GetHint,1,pos-1)
      e = Mid(GetHint,pos+1)
      GetHint = b & c & e
      X = X + 1
    Loop
    me.sHint = GetHint
  End Function

  Public Sub DeleteList(sAlias)
    sql = "DELETE FROM `question_list` WHERE `alias` = '" & sAlias & "'"
    objConn.Execute sql
    sql = "DROP TABLE `" & sAlias & "`"
    objConn.Execute sql
  End Sub

  Public Sub EnumLists(lColor)
    '// Pass a vbColor constant for AddChat, -1 for AddQ, -2 for Emote
    sql = "SELECT `title`, `alias` FROM `question_list` ORDER BY `alias`"
    Set objRS = objConn.Execute(sql)
    Do While Not objRS.EOF
      If lColor = -1 Then
        Dsp 1, objRS.Fields(0) & ": " & objRS.Fields(1), BotVars.Username, 0
      ElseIf lColor = -2 Then
        Dsp 2, objRS.Fields(0) & ": " & objRS.Fields(1), BotVars.Username, 0
      Else
        Dsp 4, objRS.Fields(0) & ": " & objRS.Fields(1), BotVars.Username, lColor
      End If
      objRS.MoveNext
    Loop
  End Sub

  '// Import scores from a textfile - returns true on success and false on error
  Public Function ImportScores(sPath,sDelimiter)
    ImportScores = False
    '// Get Highest UserID - used for rollback
    sql = "SELECT TOP 1 `ID` FROM `scores` ORDER BY `ID` DESC"
    Set objRS = objConn.Execute(sql)
    lLastID = objRS.Fields(0)
    '// Read file
    Set objTS = objFSO.OpenTextFile(sPath,1,False)
    Do While Not objTS.AtEndofStream
      iLineCounter = iLineCounter + 1
      sLine = objTS.ReadLine
      If Trim(sLine) <> "" Then
        aLine = Split(sLine,sDelimiter)
        If UBound(aLine) > 0 Then
          sName = Replace(aLine(0),"'",""): sScore = Replace(aLine(1),"'","")
          sql = "INSERT INTO `scores` (`username`,`score`,`streak`) VALUES ('" & CStr(sName) & "','" & CLng(sScore) & "','0')"
          objConn.Execute sql
        Else '// No delimiter found, ignore and log error
          Dsp 4, "Missing " & sDelimiter & " on line " & iLineCounter & ".", BotVars.Username, vbRed
        End If
      End If
    Loop
    '// Delete old records since the new scores are recorded
    sql = "DELETE FROM `scores` WHERE `ID` <= " & lLastID
    objConn.Execute sql
    ImportScores = True
  End Function

  '// Creates the database
  Public Sub CreateDatabase()

    '// Create the database
    Set Catalog = CreateObject("ADOX.Catalog")
    Catalog.Create "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=" & BotPath() & "plugins\trivia.mdb"

    '// Create Database Connection
    sDSN = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & BotPath() & "plugins\trivia.mdb"
    Set objConn = CreateObject("ADODB.Connection")
    objConn.ConnectionString = sDSN
    objConn.CursorLocation = 3 '// adUseClient
    objConn.Open

    '// Create scores Table
    sql = "CREATE TABLE `scores` (`ID` COUNTER, `username` varchar(32) NOT NULL, `score` int NULL, `streak` int NULL)"
    objConn.Execute sql

    '// Create question_list  Table
    sql = "CREATE TABLE `question_list` (`ID` COUNTER, `alias` varchar(32) NOT NULL,`title` varchar(64) NOT NULL, `description` text NULL, `times_used` int NULL)"
    objConn.Execute sql

  End Sub

  '// Create a table for the questions
  Public Sub AddQuestions(sAlias,sTitle,sDesc,sPath)

    '// Add to `question_list` table
    sql = "INSERT INTO `question_list` (`alias`, `title`, `description`, `times_used`) VALUES ('" & sAlias & "', '" & sTitle & "', '" & sDescription & "', '0')"
    objConn.Execute sql

    '// Create the question table
    sql = "CREATE TABLE `" & sAlias & "` (`ID` COUNTER, `question` varchar(255) NOT NULL, `answer` varchar(255) NOT NULL, `correct` int NULL, `incorrect` int NULL)"
    objConn.Execute sql

    '// Read data file and add questions into the database
    iLineCounter = 0
    Set objTS = Me.objFSO.OpenTextFile(sPath, 1, False)
    Do While Not objTS.AtEndofStream
      iLineCounter = iLineCounter + 1
      sLine = objTS.ReadLine
      If Trim(sLine) <> "" Then '//
        aLine = Split(sLine,"*")
        If UBound(aLine) > 0 Then '// Fields too big?
          If Len(aLine(0)) > 255 Or Len(aLine(1)) > 255 Then
            Dsp 4, "Question/Answer length exceeded on line " & iLineCounter & ".", BotVars.Username, vbRed
          Else '// Everything is fine here.
            sQuestion = Replace(aLine(0),"'",""): sAnswer = Replace(aLine(1),"'","")
            sql = "INSERT INTO `" & sAlias & "` (`question`,`answer`,`correct`,`incorrect`) VALUES ('" & sQuestion & "','" & sAnswer & "','0','0')"
            objConn.Execute sql
          End If
        Else '// No * found, ignore and log error
          Dsp 4, "Missing * on line " & iLineCounter & ".", BotVars.Username, vbRed
        End If
      End If
    Loop

  End Sub

'////////////// Private Class Methods //////////////////

  '// Used to get a random number between minimum and maximum.
  Private Function RanNum(minimum, maximum)
    RanNum = Int(((maximum + 1) - (minimum)) * Rnd + (minimum))
  End Function


'////////////// Class Init and Terminate Methods //////////////////

  Private Sub Class_Initialize
    '// Start RNG
    Randomize
    '// Create FSO object
    Set Me.objFSO = CreateObject("Scripting.FileSystemObject")

    '// Check for existing Database
    If Not Me.objFSO.FileExists(BotPath() & "plugins\trivia.mdb") Then
      '// Doesnt exist, lets create it
      CreateDatabase
    Else
      '// Create Database Connection
      sDSN = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & BotPath() & "plugins\trivia.mdb"
      Set objConn = CreateObject("ADODB.Connection")
      objConn.ConnectionString = sDSN
      objConn.CursorLocation = 3 '// adUseClient
      objConn.Open
    End If
  End Sub

  Private Sub Class_Terminate
    '// Bye
  End Sub

End Class


Go in your bot screen and say .addlist say the name of the list in your bot folder

MUST HAVE THE .TXT


Then put in what you want the .uselist <name> to be.

Now you can leave the last one blank or add a description of what the list is about.
GRAPHIXX
GRAPHIXX
Admin

Posts : 15
Join date : 2010-11-03

http://w3bzm.mess.tv

Back to top Go down

[Relase] StealthBot Trivia Empty Re: [Relase] StealthBot Trivia

Post  GruntRusheR Mon Nov 08, 2010 7:23 pm

I just wanted to add, if you want questions for trvia go straight to http://www.freewebs.com/stealthbothelp/trivia.htm its like 5.000.000 questions there.

Great tutorial tho thanks.
GruntRusheR
GruntRusheR

Posts : 1
Join date : 2010-11-08
Age : 38

Back to top Go down

Back to top

- Similar topics

 :: Bots :: StealthBot

 
Permissions in this forum:
You cannot reply to topics in this forum