Skip to content

What is the best way to get all the messages in a channel? My current method is consistently getting me rate limited every other message. #129

Answered by swarley
quinton-oglesby asked this question in Q&A
Discussion options

You must be logged in to vote

Answered in discord

bot = Discordrb::Bot.new(token: ENV['INOSUKE_TOKEN']);
after_id = 0
message_store = []

while (messages = bot.channel(345687437722386433).history(100, nil, after_id)).count == 100
  after_id = messages.map(&:id).max  
  puts "new after #{after_id}"  
  message_store += messages # or do whatever you want to do with the messages to avoid keeping them in memory 
end

# and you'll need to handle the last set of messages again out here, or use a flag set inside of the loop for continuing instead

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by swarley
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants