简体   繁体   中英

Not sure how to convert string to usable discord data

I am trying to make a system for my discord bot to store data about a message in a text file. To do this the message information turns into a string. The problem is I get an error when I am trying to use this data. For example, when I try to do message.remove_reaction I get this error message:

File "main.py", line 195, in on_raw_reaction_add
    await message.remove_reaction("✅", payload.member)
AttributeError: 'str' object has no attribute 'remove_reaction'

I am converting the text file into a list and converting it back to a variable. For example, message = file_data[5]. Here's the code that extracts the data from the text files.

for x in fileNames:
    f = open(x, "r+")
    messageid = f.readline()
    #creates list if message id match is found
    if (int(messageid) == int(payload.message_id)):
      p = open(x, "r+")
      global file_name
      file_name = p
      for x in p:
        file = x.rstrip('\n')
        file_data.append(file)
      break
  print(file_data)
  #assigning variables from list
  messageid = file_data[0]
  in_embed = file_data[2]
  game_actual = file_data[3]
  goal_actual = file_data[4]
  message = file_data[5]
  names = []
  #stores member information onto seperate list
  for x in range(6,len(file_data)):
    names.append(x)

The list before turning them into variables:

['814375931497021490', '811857283771662340', "<Message id=814375932051456000 channel=<TextChannel id=811857283771662340 name='general' position=0 nsfw=False news=False category_id=811857283771662338> type=<MessageType.default: 0> author=<Member id=810761971979911188 name='CockGobbler' discriminator='2337' bot=True nick=None guild=<Guild id=811857283771662337 name='Bot Test 2' shard_id=None chunked=False member_count=2>> flags=<MessageFlags value=0>>", 'game2', '2', "<Message id=814375931497021490 channel=<TextChannel id=811857283771662340 name='general' position=0 nsfw=False news=False category_id=811857283771662338> type=<MessageType.default: 0> author=<Member id=810761971979911188 name='CockGobbler' discriminator='2337' bot=True nick=None guild=<Guild id=811857283771662337 name='Bot Test 2' shard_id=None chunked=False member_count=2>> flags=<MessageFlags value=0>>"]

Your code does not show how you are converting the Message to string or what you are trying to do with it, but generally I would suggest you to not convert it to string.

A better approach would be to save the Message.content which is already a string of the real content of the message, in case you want to access the content. Or you could save the Message.id and when you need to get your Message object to use a function like:

await message.remove_reaction

What you would do is simply retrieving the Message object (or PartialMessage in this case):

my_message = get_partial_message(message_ID)
await my_message.remove_reaction("✅", payload.member)

You are storing and reading everything as strings, when they should really be read as specific classes as defined by discord.py . You have a few options to solve your issue.

  1. Pickle

Define a picklable class. Make a class that has attributes that align with whichever data you want to get from the message. You can then dump that object, meaning turn it into a file, and then load it, meaning turn it back into usable python code.

  1. Manually turn your file_data into the correct objects

Go through the Message documentation, and the Message source code , and check what kind of type every part of the message needs to be before trying to use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM