Results 1 to 4 of 4

Thread: Discord Bot onMessage

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    67

    Discord Bot onMessage

    I would like the bot to reply to me only as PM.
    e.g. I send the bot as PM !say 1
    Then the bot should only reply in PM 2.

    But the bot only recognizes a message in the channel, not in the PM.

    Code:
        Private Async Function onMessage(message As SocketMessage) As Task
            Dim user = TryCast(message.Author, SocketGuildUser)
            If message.Source <> MessageSource.Bot And message.Content.StartsWith("!") Then
                If message.Content.StartsWith("!say 1") And DirectCast(message.Author, SocketGuildUser).Roles.ToList.Contains(DirectCast(message.Channel, IGuildChannel).Guild.GetRole(91586140315277)) Then
    
                    Await message.Channel.SendMessageAsync("2")
                    Await user.SendMessageAsync("2")
                End If
            End If
        End Function

  2. #2
    New Member
    Join Date
    Oct 2022
    Posts
    1

    Re: Discord Bot onMessage

    Quote Originally Posted by StikxX View Post
    I would like the bot to reply to me only as PM.
    e.g. I send the bot as PM !say 1
    Then the bot should only reply in PM 2.

    But the bot only recognizes a message in the channel, not in the PM.

    Code:
        Private Async Function onMessage(message As SocketMessage) As Task
            Dim user = TryCast(message.Author, SocketGuildUser)
            If message.Source <> MessageSource.Bot And message.Content.StartsWith("!") Then
                If message.Content.StartsWith("!say 1") And DirectCast(message.Author, SocketGuildUser).Roles.ToList.Contains(DirectCast(message.Channel, IGuildChannel).Guild.GetRole(91586140315277)) Then
    
                    Await message.Channel.SendMessageAsync("2")
                    Await user.SendMessageAsync("2")
                End If
            End If
        End Function
    What version of Discord.net you are using, may I see your entire source for it for references?

    And to answer your query, this is how I did mine.

    In my code, the bot listens to the command "!list <string>".
    Where String is recorded in mySql.
    The bot then searches the string and list all the data then save it as "text file" and send it via DM to the "use who invoked the command".

    The code is junkie but it works.

    Code:
    <Command("list")>
        Public Async Function GsList(GName As String) As Task
    
            Dim TimeLog2 = Date.Now.ToString("yyyy-MM-dd hh-mm-ss")
    
            Try
                Dim GMember As IRole = Context.Guild.Roles.FirstOrDefault(Function(y) y.Name = "Editor")
                Dim user As IGuildUser = Context.User
                Dim uID As String = user.Id
    
                '--------------------
                Dim db_uID As String = $"SELECT * FROM `{ Context.Guild.Id & "_PlayerList" }` WHERE CurrentGuild='" & GName & "';"
                Dim myID, myAp, myAAP, myDP, plClass, myAxe, myLvL, myImage, myCharName
    
                If user.RoleIds.Contains(GMember.Id) Then
    
                    Await ReplyAsync("Exporting data to text...I will message you once its completed.")
    
                    Try
                        Dim fileloc = AppDomain.CurrentDomain.BaseDirectory & "\" & TimeLog2 & "_Playerlist_" & GName & ".txt"
    
                        Dim sqlcommand As New MySqlCommand
                        Dim sqladapter As New MySqlDataAdapter
                        Dim sqldata As New DataTable
    
                        conn.Open()
                        sqlcommand.Connection = conn
                        sqlcommand.CommandText = db_uID
                        sqladapter.SelectCommand = sqlcommand
                        sqladapter.Fill(sqldata)
                        ' DataEntry.DataSource = sqldata
    
                        Dim txt As String = String.Empty
    
                        For Each column As DataColumn In sqldata.Columns
                            'Add the Header row for Text file.
                            txt += column.ColumnName & vbTab  '& vbTab & vbTab & vbTab
                        Next
    
                        txt += vbCrLf
    
                        For Each row As DataRow In sqldata.Rows
    
                            For Each column As DataColumn In sqldata.Columns
                                'Add the Data rows.
                                txt += row(column.ColumnName).ToString() & vbTab '& vbTab & vbTab & vbTab
                            Next
    
                            'Add new line.
                            txt += vbCrLf  'vbCr & vbLf
    
                        Next
    
                        If Not File.Exists(fileloc) Then
                            File.Delete(fileloc)
                            Thread.Sleep(500)
                            Using sw As StreamWriter = New StreamWriter(fileloc)
                                sw.WriteLine(txt)
                            End Using
    
                        Else
                            Using sw As StreamWriter = New StreamWriter(fileloc)
                                sw.WriteLine(txt)
                            End Using
                        End If
    
                        ' REPLY WITH THE DATA
                        ' Await ReplyAsync($"```{ txt }```")
    
                        'SEND PRIVATE MESSAGE TO INVOKER
                        Thread.Sleep(2000)
                        Await user.SendFileAsync(fileloc, "Here's the current data.")
    
                        ' Await Me.Context.Message.DeleteAsync()
    
                        'clean up, delete the generated file
                        File.Delete(fileloc)
    
                    Catch conError As Exception
    
                        Console.WriteLine(conError.Message)
    
                    End Try
    
                Else
                    Await ReplyAsync($":no_entry: {user.Mention}, you have no permission to use this command.")
                    ' Await Me.Context.Message.DeleteAsync()
                End If
    
    
    
            Catch ex As Exception
                Console.WriteLine("Player List: Export Module " & ex.Message)
            End Try
        End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    67

    Re: Discord Bot onMessage

    Thanks, but this is not a solution to my problem.
    The bot does not respond when I send it a PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    67

    Re: Discord Bot onMessage

    Code:
    If DirectCast(message.Author, SocketGuildUser).Roles.ToList.Contains(DirectCast(message.Channel, IGuildChannel).Guild.GetRole(Botroleid.Text)) Then
    System.InvalidCastException: "The object of type "Discord.WebSocket.SocketUnknownUser" cannot be converted to type "Discord.WebSocket.SocketGuildUser"."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width