使用 .VBS 发送 Discord 消息时出现语法错误

Syntax error when sending a Discord message using .VBS

我最近想创建一个脚本,允许我从我的不同帐户发送消息,而无需在 Discord 上记录每个帐户的 in/out(我知道 Discord 上有多个帐户容量,但你知道。 ..挑战¯\(ツ)/¯ ) 所以,我最初找到了一个非常基本的脚本,每次你想发送消息时都需要编辑它。所以我想尝试做一个UI。我只是在我的代码中粘贴了发送功能。这是我想出的(它是为法国用户设计的,所以不要介意显示的文本;)):

On Error Resume Next

'Token selector dialogue box
account=InputBox("Merci d'indiquer le nom du compte ci-dessous","Discord CMD","")

'If user cancels, exit the program
if IsEmpty(account) Then
    WScript.Quit
end if

'Check if token is registered
Const ForReading = 1
Dim File, content, FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Function FileExists(FilePath)
    If fso.FileExists(FilePath) Then
        FileExists=CBool(1)
    Else
        FileExists=CBool(0)
    End If
End Function
If FileExists("database\accounts\"+account+".bin") Then
    'If the file exist, do nothing and continue the script
    WScript.Sleep(500)
Else
    'If the file doesn't exist, restart the script and display an error
    x=msgbox("This account has not be registered in the database. Please try again.", 0+16, "Error - Does not exist")
    CreateObject("WScript.Shell").Run "Chat.vbs"
    WScript.Quit
End If

'Get the token related the account asked
Set File = fso.OpenTextFile("database\accounts\"+account+".bin", ForReading)
Token = File.ReadAll


'Loop
Do
    'Choose channel
    Echannel=InputBox("Merci d'indiquer l'identifiant du channel ci-dessous. Si il est identique, inscrivez 'Channel'","Discord CMD - " + account,"Channel")
    If (Echannel="Channel") Then
        'If the channel is the same as before, then we don't change it in the code
        WScript.Sleep(500)
    Else
        'If the channel is different, we change to the new value
        channels = Echannel
    End If

    'If user cancels, restart the program
    if IsEmpty(account) Then
        CreateObject("WScript.Shell").Run "Chat.vbs"
        WScript.Quit
    end if


    'The user enters his message
    text=InputBox("Merci d'indiquer le texte ci-dessous, faites OK pour l'envoyer","Discord CMD - " + account + " - Writing...","")

    'If user cancels, restart the program
    if IsEmpty(account) Then
        CreateObject("WScript.Shell").Run "Chat.vbs"
        WScript.Quit
    end if

    'We send the message
    Call commandeexercuter(channels,Token,text)
    Function commandeexercuter(channels,Token,text)
        Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP") 
        sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
        objXmlHttp.open "POST","https://discordapp.com/api/v6/channels/" & channels &"/messages", False 
        objXmlHttp.setRequestHeader "Authorization", Token
        objXmlHttp.setRequestHeader "Content-Type", "application/json"
        objXmlHttp.send(sRequest)
    End Function
'We restart the loop
Loop

唯一的问题是,当我启动脚本时,它 returns 出现错误,最后一个函数(之前工作的函数)说语法不正确...

有人可以帮我吗? 祝你有美好的一天 ;)

您不能在 Do 循环中定义函数。您只需将函数声明移到循环外,就在最后一行之后:

    ' We send the message
    Call commandeexercuter(channels, Token, text)

'We restart the loop
Loop

Function commandeexercuter(channels, Token, text)
    Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
    sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
    objXmlHttp.open "POST", "https://discordapp.com/api/v6/channels/" & channels & "/messages", False
    objXmlHttp.setRequestHeader "Authorization", Token
    objXmlHttp.setRequestHeader "Content-Type", "application/json"
    objXmlHttp.send (sRequest)
End Function

我建议您在脚本的开头或结尾将所有函数组合在一起。

您也可以直接将 commandeexercuter 代码放在循环中,因为您没有在其他任何地方使用该函数:

    'If user cancels, restart the program
    If IsEmpty(account) Then
        CreateObject("WScript.Shell").Run "Chat.vbs"
        WScript.Quit
    End If

    Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
    sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
    objXmlHttp.open "POST", "https://discordapp.com/api/v6/channels/" & channels & "/messages", False
    objXmlHttp.setRequestHeader "Authorization", Token
    objXmlHttp.setRequestHeader "Content-Type", "application/json"
    objXmlHttp.send (sRequest)

'We restart the loop
Loop