Module: ChatgptAssistant::Bots::Discord::Actions

Included in:
DiscordBot
Defined in:
lib/chatgpt_assistant/bots/discord/actions.rb

Instance Method Summary collapse

Instance Method Details

#create_chat_actionObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 76

def create_chat_action
  title = evnt.message.content.split[1..].join(" ")
  mode = nil
  if title.include? ":"
    mode = title.split(":").last.to_i
    title = title.split(":").first
  end
  actors = AwesomeChatgptActors::CastControl.actors
  return send_message "invalid mode" unless (mode.to_i >= 1 && mode.to_i <= actors.size + 1) || mode.nil?
  return send_message "invalid chat title" if title.nil? || title.empty?
  return send_message "chat title already exists" if user.chat_by_title(title)

  actor_name = actors[mode.to_i - 1] if mode
  actor = AwesomeChatgptActors::Actor.new(role: actor_name, language: config.language) if actor_name
  chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
  chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
  return send_message "Something went wrong", msg.chat.id unless chat

  chat.save ? chat_created_message(chat) : send_message(error_messages[:chat_creation])
end

#discord_created(chat) ⇒ Object



97
98
99
100
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 97

def discord_created(chat)
  evnt.respond "Intructions sended to actor:\n#{chat.prompt}" unless chat.actor.nil?
  evnt.respond success_messages[:chat_created] if evnt.present?
end

#help_actionObject



46
47
48
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 46

def help_action
  start_action
end

#hist_actionObject



63
64
65
66
67
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 63

def hist_action
  messages = Message.where(chat_id: chat.id).order(:created_at)
  messages.each { |m| send_message "#{m.role} - #{m.content}\n#{m.created_at.strftime("%d/%m/%Y %H:%M")}" }
  "This is the end of the chat history"
end

#list_actionObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 50

def list_action
  chats = user.chats

  embed = Discordrb::Webhooks::Embed.new(
    title: "Hello, #{evnt.user.name}!",
    description: "Your chats:",
    color: "00FF00",
    fields: chats.map { |field| { name: field.title, value: field.actor || "No prompt" } }
  )

  evnt.respond nil, false, embed
end

#login_actionObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 7

def 
  user_email, user_password = message.split(":")
  case discord_user_auth(user_email, user_password, evnt.user.id)
  when "user not found"
    send_message error_messages[:user_not_found]
  when "wrong password"
    puts "wrong password"
    send_message error_messages[:wrong_password]
  when find_user(email: user_email).email
    send_message success_messages[:user_logged_in]
  end
end

#sl_chat_action(chat_to_select) ⇒ Object



69
70
71
72
73
74
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 69

def sl_chat_action(chat_to_select)
  @chat = user.chat_by_title(chat_to_select)
  send_message error_messages[:chat_not_found] if chat.nil?
  user.update(current_chat_id: chat.id) if chat
  send_message success_messages[:chat_selected] if chat
end

#start_actionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chatgpt_assistant/bots/discord/actions.rb', line 20

def start_action
  fields = %w[login start help new_chat sl_chat ask list hist connect disconnect speak]

  values_hash = {
    login: "Account login, use: #{discord_prefix}login email:password",
    start: "Start the bot, use: #{discord_prefix}start",
    help: "Show this message, use: #{discord_prefix}help",
    new_chat: "Create a new chat, use: #{discord_prefix}new_chat chat_name",
    sl_chat: "Select a chat, use: #{discord_prefix}sl_chat chat_name",
    ask: "Ask a question, use: #{discord_prefix}ask question",
    list: "List all chats, use: #{discord_prefix}list",
    hist: "Show chat history, use: #{discord_prefix}hist",
    connect: "Connect to voice channel, use: #{discord_prefix}connect",
    disconnect: "Disconnect from voice channel, use: #{discord_prefix}disconnect",
    speak: "Speak in voice channel, use: #{discord_prefix}speak question"
  }

  embed = Discordrb::Webhooks::Embed.new(
    title: "Hello, #{evnt.user.name}!",
    description: "I'm ChatGPT Assistant, see my commands below:",
    color: "00FF00",
    fields: fields.map { |field| { name: field, value: values_hash[field.to_sym] } }
  )
  evnt.respond nil, false, embed
end